Lexical Rules
Slice’s lexical rules are very similar to those of C++ and Java, except for some differences for identifiers.
Slice 的词法规则与 C++ 和 Java 的非常相似,除了标识符的一些差异之外。
Comments
Slice definitions permit both the C and the C++ style of writing comments:
Slice定义允许使用 C 和 C++ 风格编写注释:
1 | /* |
Keywords
Slice uses a number of keywords, which must be spelled in lowercase. For example, class and dictionary are keywords and must be spelled as shown. There are three exceptions to this lowercase rule: LocalObject, Object and Value are keywords and must be capitalized as shown.
Slice 使用许多关键字,这些关键字必须以小写形式拼写。 例如,class 和dictionary 是关键字,必须按所示拼写。 此小写规则有三个例外:LocalObject、Object 和 Value 是关键字,必须大写,如图所示。
Identifiers
Identifiers begin with an alphabetic character followed by any number of alphabetic characters or digits. Underscores are also permitted in identifiers with the following limitations:
an identifier cannot begin or end with an underscore
an identifier cannot contain multiple consecutive underscores
Given these rules, the identifier get_account_name is legal but not account, account, or get__account.
Slice identifiers are restricted to the ASCII range of alphabetic characters and cannot contain non-English letters, such as Å. (Supporting non-ASCII identifiers would make it very difficult to map Slice to target languages that lack support for this feature.)
标识符以字母字符开头,后跟任意数量的字母字符或数字。 标识符中也允许使用下划线,但有以下限制:
标识符不能以下划线开头或结尾
标识符不能包含多个连续的下划线
根据这些规则,标识符 get_account_name 合法,但 account、account 或 get__account 不合法。
Slice标识符仅限于 ASCII 范围内的字母字符,不能包含非英文字母,例如 Å。 (支持非 ASCII 标识符将使将 Slice 映射到不支持此功能的目标语言变得非常困难。)
Case Sensitivity
Identifiers are case-insensitive but must be capitalized consistently. For example, TimeOfDay and TIMEOFDAY are considered the same identifier within a naming scope. However, Slice enforces consistent capitalization. After you have introduced an identifier, you must capitalize it consistently throughout; otherwise, the compiler will reject it as illegal. This rule exists to permit mappings of Slice to languages that ignore case in identifiers as well as to languages that treat differently capitalized identifiers as distinct.
标识符不区分大小写,但大小写必须一致。 例如,TimeOfDay 和 TIMEOFDAY 在命名范围内被视为相同的标识符。 然而,Slice 强制使用一致的大小写。 引入标识符后,必须始终将其大写; 否则,编译器将认为它是非法的而拒绝它。 存在此规则是为了允许将 Slice 映射到忽略标识符大小写的语言以及将不同大写标识符视为不同的语言。
Identifiers That Are Keywords
You can define Slice identifiers that are keywords in one or more implementation languages. For example, switch is a perfectly good Slice identifier but is a C++ and Java keyword. Each language mapping defines rules for dealing with such identifiers. The solution typically involves using a prefix to map away from the keyword. For example, the Slice identifier switch is mapped to _cpp_switch in C++ and _switch in Java.
The rules for dealing with keywords can result in hard-to-read source code. Identifiers such as native, throw, or export will clash with C++ or Java keywords (or both). To make life easier for yourself and others, try to avoid Slice identifiers that are implementation language keywords. Keep in mind that mappings for new languages may be added to Ice in the future. While it is not reasonable to expect you to compile a list of all keywords in all popular programming languages, you should make an attempt to avoid at least common keywords. Slice identifiers such as self, import, and while are definitely not a good idea.
您可以定义作为一种或多种实现语言中的关键字的Slice标识符。 例如,switch 是一个非常好的 Slice 标识符,但却是一个 C++ 和 Java 关键字。 每种语言映射都定义了处理此类标识符的规则。 该解决方案通常涉及使用前缀来映射关键字。 例如,Slice 标识符 switch 在 C++ 中映射为 _cpp_switch,在 Java 中映射为 _switch。
处理关键字的规则可能会导致源代码难以阅读。 诸如native、 throw 或export 之类的标识符将与C++ 或Java 关键字(或两者)发生冲突。 为了让您和他人的生活更轻松,请尽量避免使用作为实现语言关键字的Slice标识符。 请记住,将来可能会将新语言的映射添加到 Ice 中。 虽然期望您编译所有流行编程语言中的所有关键字的列表是不合理的,但您应该尝试至少避免常见关键字。 self、import 和 while 等Slice标识符绝对不是一个好主意。
Escaped Identifiers
It is possible to use a Slice keyword as an identifier by prefixing the keyword with a backslash, for example:
可以通过在关键字前面加上反斜杠来使用 Slice 关键字作为标识符,例如:
1 | struct dictionary // Error! |
The backslash escapes the usual meaning of a keyword; in the preceding example, \dictionary is treated as the identifier dictionary. The escape mechanism exists to permit keywords to be added to the Slice language over time with minimal disruption to existing specifications: if a pre-existing specification happens to use a newly-introduced keyword, that specification can be fixed by simply prepending a backslash to the new keyword. Note that, as a matter of style, you should avoid using Slice keywords as identifiers (even though the backslash escapes allow you to do this).
It is legal (though redundant) to precede an identifier that is not a keyword with a backslash — the backslash is ignored in that case.
反斜杠转义了关键字的通常含义; 在前面的示例中,\dictionary 被视为标识符dictionary 。 转义机制的存在是为了允许关键字随着时间的推移添加到 Slice 语言中,同时对现有规范的干扰最小:如果预先存在的规范碰巧使用了新引入的关键字,则只需在该规范前面添加一个反斜杠即可修复该规范。 新的关键字。 请注意,就风格而言,您应该避免使用 Slice 关键字作为标识符(即使反斜杠转义允许您这样做)。
在不是关键字的标识符前面加上反斜杠是合法的(尽管是多余的)——在这种情况下,反斜杠将被忽略。
Reserved Identifiers
Slice reserves the identifier Ice and all identifiers beginning with Ice (in any capitalization) for the Ice implementation. For example, if you try to define a type named Icecream, the Slice compiler will issue an error message.
Slice 为 Ice 实现保留了标识符 Ice 和所有以 Ice 开头的标识符(任何大小写)。 例如,如果您尝试定义名为 Icecream 的类型,Slice 编译器将发出错误消息。
You can suppress this behavior by using the ice-prefix Slice metadata directive, which enables definition of identifiers beginning with Ice. However, do not use this directive unless you are compiling the Slice definitions for the Ice run time itself.
您可以使用ice-prefix Slice元数据指令来抑制此行为,该指令允许定义以Ice开头的标识符。 但是,除非您正在为 Ice 运行时本身编译 Slice 定义,否则不要使用此指令。
Slice identifiers ending in any of the suffixes Async, Disp, Helper, Holder, Prx, and Ptr are also reserved. These endings are used by the various language mappings and are reserved to prevent name clashes in the generated code.
以任何后缀 Async、Disp、Helper、Holder、Prx 和 Ptr 结尾的Slice标识符也被保留。 这些结尾由各种语言映射使用,并被保留以防止生成的代码中发生名称冲突。