Lexical Rules

3 min read

3 min read

3 min read

3 min read

3 min read

3 min read

3 min read

4 min read

3 min read

Slice supports block comments, delimited by /* and */, and line comments, which start with // and extend to the end of the line:

Slice
/*
* Block comment.
*/
// Line comment.

Slice also supports doc comments, which document the definition that follows them: line comments that start with /// and block comments that start with /**. See Comment Syntax.

Slice uses a number of keywords, which must be spelled in lowercase, such as class and dictionary. The keywords Object and Value are the exceptions: they must be capitalized as shown.

An identifier begins with an ASCII letter, followed by any number of ASCII letters, digits, and underscores. An identifier cannot begin or end with an underscore, or contain two consecutive underscores: get_account_name is legal, but _account, account_, and get__account are not. Non-ASCII letters, such as Å, cannot appear in an identifier.

An identifier cannot end with Helper, Holder, Prx, or Ptr: the Slice compilers reserve these suffixes.

Identifiers that differ only in capitalization are the same identifier: you cannot declare both TimeOfDay and TIMEOFDAY in the same scope. After you declare an identifier, you must refer to it with the same capitalization; otherwise, the compiler rejects the reference.

You can use a Slice keyword as an identifier by prefixing it with a backslash:

Slice
struct Value // Error!
{
// ...
}
struct \Value // OK
{
// ...
}

The backslash lets an existing definition keep a name that a later version of Slice makes a keyword. In new definitions, avoid keywords as identifiers. A backslash before an identifier that is not a keyword has no effect.

A Slice identifier maps to an identical C++ identifier. For example, the Slice identifier Clock becomes the C++ identifier Clock. A single Slice identifier often results in several C++ identifiers: for a Slice interface named Greeter, the generated C++ code uses the identifiers Greeter, GreeterPrx, GreeterPtr, AsyncGreeter, and AsyncGreeterPtr.

A Slice identifier that is a C++ keyword keeps its name in the generated C++ code, which then does not compile. Give this identifier a C++ name of your choice with the cpp:identifier metadata directive. For example, template is a C++ keyword but a valid Slice identifier:

Slice
struct Document
{
["cpp:identifier:documentTemplate"]
string template;
}

In the generated C++ code, the field template becomes documentTemplate.

A Slice identifier maps to an identical C# identifier. For example, the Slice identifier Clock becomes the C# identifier Clock. A single Slice identifier often results in several C# identifiers: for a Slice interface named Greeter, the generated C# code uses the identifiers Greeter, GreeterPrx, GreeterPrxHelper, GreeterDisp_, AsyncGreeter, and AsyncGreeterDisp_.

A Slice identifier that is a C# keyword keeps its name in the generated C# code, which then does not compile. Give this identifier a C# name of your choice with the cs:identifier metadata directive. For example, delegate is a C# keyword but a valid Slice identifier:

Slice
struct Meeting
{
["cs:identifier:delegateName"]
string delegate;
}

In the generated C# code, the field delegate becomes delegateName.

A Slice identifier maps to an identical Java identifier. For example, the Slice identifier Clock becomes the Java identifier Clock. A single Slice identifier often results in several Java identifiers: for a Slice interface named Greeter, the generated Java code uses the identifiers Greeter, GreeterPrx, and AsyncGreeter.

A Slice identifier that is a Java keyword keeps its name in the generated Java code, which then does not compile. Give this identifier a Java name of your choice with the java:identifier metadata directive. For example, synchronized is a Java keyword but a valid Slice identifier:

Slice
struct FileStatus
{
["java:identifier:isSynchronized"]
bool synchronized;
}

In the generated Java code, the field synchronized becomes isSynchronized.

A Slice identifier maps to an identical JavaScript identifier. For example, the Slice identifier Clock becomes the JavaScript identifier Clock. A single Slice identifier often results in several JavaScript identifiers: for a Slice interface named Greeter, the generated JavaScript code uses the identifiers Greeter and GreeterPrx.

A Slice identifier that is a JavaScript keyword keeps its name in the generated JavaScript code, which is then invalid. Give this identifier a JavaScript name of your choice with the js:identifier metadata directive. For example, export is a JavaScript keyword but a valid Slice identifier:

Slice
struct Settings
{
["js:identifier:exportEnabled"]
bool export;
}

In the generated JavaScript code, the field export becomes exportEnabled.

A Slice identifier maps to an identical MATLAB identifier, or a MATLAB identifier derived from this Slice identifier. For example, Slice interface Greeter is mapped to the MATLAB class GreeterPrx.

A Slice identifier that is a MATLAB keyword keeps its name in the generated MATLAB code, which is then invalid. Give this identifier a MATLAB name of your choice with the matlab:identifier metadata directive. For example, end is a MATLAB keyword but a valid Slice identifier:

Slice
struct Range
{
["matlab:identifier:last"]
int end;
}

In the generated MATLAB code, the field end becomes last.

A Slice identifier maps to an identical PHP identifier, or a PHP identifier derived from this Slice identifier. For example, the Slice-to-PHP compiler generates the PHP class GreeterPrxHelper for Slice interface Greeter.

A Slice identifier that is a PHP keyword keeps its name in the generated PHP code, which is then invalid when this identifier names a PHP class, such as the class of a Slice structure. PHP keywords are case-insensitive, so the Slice structure List conflicts with the PHP keyword list. Give this identifier a PHP name of your choice with the php:identifier metadata directive:

Slice
["php:identifier:ItemList"]
struct List
{
string name;
}

In the generated PHP code, the class for List is named ItemList.

A Slice identifier maps to an identical Python identifier. For example, the Slice identifier Clock becomes the Python identifier Clock. A single Slice identifier often results in several Python identifiers: for a Slice interface named Greeter, the generated Python code uses the identifiers Greeter and GreeterPrx.

A Slice identifier that is a Python keyword keeps its name in the generated Python code, which is then invalid. Give this identifier a Python name of your choice with the python:identifier metadata directive. For example, raise is a Python keyword but a valid Slice identifier:

Slice
struct Salary
{
["python:identifier:raiseAmount"]
double raise;
}

In the generated Python code, the field raise becomes raiseAmount.

A Slice identifier maps to a Ruby identifier derived from it. For example, the Slice interface Greeter maps to the Ruby class GreeterPrx.

The mapping changes the case of the first letter of some identifiers:

  • It capitalizes the names of modules, interfaces, classes, exceptions, structures, enumerations, enumerators, and constants, as Ruby requires for constant, class, and module names. For example, the Slice enumerator red maps to the Ruby constant Red.
  • It lowercases the first letter of parameter names.

Operation and field names keep their case.

A Slice identifier that is a Ruby keyword keeps its name in the generated Ruby code, which is then invalid. Give this identifier a Ruby name of your choice with the ruby:identifier metadata directive. For example, retry is a Ruby keyword but a valid Slice identifier:

Slice
struct Policy
{
["ruby:identifier:retryCount"]
int retry;
}

In the generated Ruby code, the field retry becomes retryCount. The mapping uses an identifier set with ruby:identifier as is.

A Slice identifier maps to an identical Swift identifier. For example, the Slice identifier Clock becomes the Swift identifier Clock. A single Slice identifier often results in several Swift identifiers: for a Slice interface named Greeter, the generated Swift code uses the identifiers Greeter, GreeterPrx, and GreeterTraits.

A Slice identifier that is a Swift keyword keeps its name in the generated Swift code, which then does not compile. Give this identifier a Swift name of your choice with the swift:identifier metadata directive. For example, protocol is a Swift keyword but a valid Slice identifier:

Slice
struct Endpoint
{
["swift:identifier:protocolName"]
string protocol;
}

In the generated Swift code, the field protocol becomes protocolName.