Files
Endorphin-Document/appendices/formal-grammar.md
T

125 lines
4.5 KiB
Markdown

# 形式文法
本附录集中列出正文采用的文法草案。文法说明可表达的源码结构;名称、类型、可见性与执行效果由相应章节定义。各主题的确定程度见[规范状态](specification-status.md)。
## EBNF 记法
| 记法 | 含义 |
| --- | --- |
| `=` | 定义产生式 |
| `;` | 结束产生式 |
| `|` | 选择 |
| `,` | 顺序连接 |
| `[ ... ]` | 可选项 |
| `{ ... }` | 零次或多次重复 |
| `( ... )` | 产生式分组 |
| 引号中的文本 | 源码终结符 |
产生式中的逗号和分号属于 EBNF 记法,不要求源码在相同位置包含分隔符。任意词法单元之间可出现空白或行注释。EOF 表示输入结束。
## 程序与导入
```ebnf
program = { use-decl }, declaration, { declaration }, EOF ;
declaration = class-decl | function-decl | scene-decl ;
use-decl = "use", namespace-key ;
namespace-key = user-handle, ":", namespace-path ;
namespace-path = identifier, { ".", identifier } ;
```
user-handle 是系统认可的唯一用户名,由身份规则提供,不等同于源码 identifier。代码自身的归属通过外部上下文提供,程序文法中没有自身 namespace 声明。
相关说明:[namespace](../guide/03-namespaces.md)、[程序结构与导入](../guide/04-program-structure.md)。
## 类与类型
```ebnf
class-decl = "class", identifier, "{", { field-decl }, "}" ;
field-decl = identifier, ":", type-name ;
type-name = identifier ;
```
相关说明:[类型与类](../guide/07-types-and-classes.md)。
## 函数与绑定
```ebnf
function-decl = "function", identifier, "(", [ parameters ], ")",
":", type-name, block ;
parameters = parameter, { ",", parameter } ;
parameter = identifier, ":", type-name ;
block = "{", { binding }, return-stmt, "}" ;
binding = "let", identifier, "=", expression ;
return-stmt = "return", expression ;
```
本基本函数体文法只包含绑定和末尾返回,不覆盖完整控制流或独立表达式语句。
相关说明:[表达式与绑定](../guide/08-expressions-and-bindings.md)、[函数与调用](../guide/09-functions-and-calls.md)。
## 表达式
```ebnf
expression = sum ;
sum = product, { ("+" | "-"), product } ;
product = unary, { ("*" | "/"), unary } ;
unary = "-", unary | postfix ;
postfix = primary, { member-access | call-arguments } ;
member-access = ".", identifier ;
call-arguments = "(", [ expression-list ], ")" ;
expression-list = expression, { ",", expression } ;
primary = identifier | string | number | symbol
| "(", expression, ")" ;
```
成员访问与调用先于一元负号,一元负号先于乘除,乘除先于加减。正文采用同级二元算术运算从左到右组合的规则。
此处能表达成员调用,不代表某个类型自动拥有相应成员。数据读取提案使用同一套表达式文法,没有单独的 get 关键字。
相关说明:[表达式与绑定](../guide/08-expressions-and-bindings.md)、[数据读取](../guide/10-data-access.md)。
## 场景与渲染节点
```ebnf
scene-decl = "export", "scene", identifier, "{", render-decl, "}" ;
render-decl = "render", "{", node, "}" ;
node = identifier, [ arguments ], [ children ] ;
arguments = "(", [ argument, { ",", argument } ], ")" ;
argument = [ identifier, ":" ], expression ;
children = "{", { node }, "}" ;
```
render 内单根结构由产生式表达。组件是否接受子节点、参数是否合法以及 render 内可使用哪些表达式效果,属于语义规则。
相关说明:[场景与渲染树](../guide/11-scenes-and-render-trees.md)、[UI 组件](../guide/12-ui-components.md)。
## 词法
```ebnf
identifier = identifier-start, { identifier-continue } ;
identifier-start = "_" | unicode-letter ;
identifier-continue = "_" | unicode-letter | unicode-number ;
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
number = digit, { digit }, [ ".", digit, { digit } ] ;
symbol = ".", identifier ;
string = '"', { string-character | escape }, '"' ;
escape = "\\n" | "\\t" | '\\"' | "\\\\" ;
line-comment = "//", { character-except-line-feed }, [ line-feed ] ;
```
unicode-letter、unicode-number、string-character、character-except-line-feed 和 line-feed 为词法字符类别。string-character 不包括未转义的双引号、反斜杠和换行;负号不属于 number。
Unicode 分类边界及源码中的换行字符规则列于[规范状态](specification-status.md#词法与表达式)。
相关说明:[词法结构](../guide/06-lexical-structure.md)。
[目录](../README.md)