参考
简化文法
集中查看言序声明、语句、调用与表达式的语法形式。
以下文法用于快速理解结构,不替代解析器实现中的完整错误恢复与优先级规则。
program → declaration* EOF ;
declaration → "公"? ("令" | "定") IDENT type? "为" expression ";"
| functionDecl
| classDecl
| importDecl
| statement ;
type → ":" typePrimary (("|") typePrimary)* ;
typePrimary → TYPE_NAME "?"?
| IDENT "<" type ("," type)* ">"
| "法" "(" (type ("," type)*)? ")" ":" type ;
functionDecl → "异"? "法" IDENT "(" parameters? ")" type? "则" declaration* "终" ;
classDecl → "公"? "类" IDENT ("承" IDENT)? ("纳" IDENT ("," IDENT)*)?
"则" classMember* "终" ;
classMember → ("公" | "私" | "只" | "静")*
(fieldDecl | functionDecl) ;
fieldDecl → "域" IDENT type ("为" expression)? ";" ;
protocolDecl → "公"? "协" IDENT "则" (fieldDecl | functionSignature)* "终" ;
importDecl → "引" STRING ("为" | "作") IDENT ";" ;
statement → "置" (IDENT | member | index) "为" expression ";"
| "言" expression ";"
| "若" expression "则" declaration* ("否则" declaration*)? "终"
| "当" expression "则" declaration* "终"
| "逐" IDENT type? "于" expression "则" declaration* "终"
| "试" "则" declaration* "救" IDENT "则" declaration* "终"
| "抛" expression ";"
| "归" expression? ";"
| expression ";" ;
call → primary (("(" arguments? ")") | ("." IDENT) | index | slice)* ;
index → "【" expression "】" ;
slice → "【" expression? ":" expression? "】" ;
primary → literal | IDENT | list | tuple | map | "候" expression | "(" expression ")" ;
list → "【" arguments? "】" ;
tuple → "(" expression "," (expression ("," expression)*)? ")" ;
map → "{" (expression ":" expression) ("," ...)* "}" ;
expression → 逻辑或、逻辑且、相等、比较、加减、乘除、一元、调用、原子表达式 ;#或//起始至行末为注释;紧邻公开声明的#:或///会成为 API 文档注释。运算符的详细优先级见基础语法与类型。