Chester 語法文法 (類 BNF)
重要提示: 本文檔僅提供Chester語言語法的大致描述,可能高度不準確或與當前實現不完全一致。應將其用作一般指南,而非精確的規範。
本文檔嘗試使用類似BNF的符號來描述Chester語言語法。
注意: 空白字符和註釋(// ...
)通常允許在標記之間出現,除非有特殊意義(如區塊中的換行),否則在語法規則中不會明確顯示。
頂層結構
Toplevel ::= Statement* (Expr | ';')?
Statement ::= Expr (';' | Newline)
注意:在許多上下文(如區塊或頂層)中,換行可以像分號一樣作為語句分隔符。區塊中的最後一個表達式作為其返回值,除非後跟分號。
表達式
Expr ::= Atom | Expr Operator Expr | Operator Expr | Expr Operator
Atom ::= ID
| Literal
| Tuple
| List
| Block
| Object
| Application
| MethodCall
| SymbolLookup
| Keyword
Tuple ::= '(' ExprList? ')'
List ::= '[' ExprList? ']'
Block ::= '{' Statement* Expr? '}'
ExprList ::= Expr (',' Expr)* ','?
Application ::= GenericApplication | FunctionCall | BlockApplication | CombinedApplication
GenericApplication ::= Atom '[' ExprList ']'
FunctionCall ::= Atom '(' ExprList? ')'
BlockApplication ::= Atom Block
CombinedApplication ::= GenericApplication FunctionCall
| GenericApplication BlockApplication
| FunctionCall BlockApplication
| GenericApplication FunctionCall BlockApplication
MethodCall ::= Atom '.' ID ('(' ExprList? ')')? // Parentheses are optional for field access
Attribute ::= '@' ID // NOT IMPLEMENTED YET
Keyword ::= '#' ID (GenericApplication | FunctionCall)*
Note: All identifiers are treated uniformly at the syntax level, with no distinction between different kinds of identifiers. Operator precedence and associativity are handled during semantic analysis, not in the grammar.
物件字面值
Object ::= '{' ObjectClauseList? '}'
ObjectClauseList ::= ObjectClause (',' ObjectClause)* ','?
ObjectClause ::= ObjectKey ('=' | '=>') Expr
ObjectKey ::= ID
| QualifiedName
| STRING_LITERAL
| SYMBOL_LITERAL
QualifiedName ::= ID ('.' ID)*
字面值
Literal ::= INT_LITERAL
| RAT_LITERAL
| STRING_LITERAL
| SYMBOL_LITERAL
SYMBOL_LITERAL ::= "'" ID
終結符
ID ::= /* Starts with letter, _, or emoji; contains letters, digits, _, emoji */
Operator ::= /* Sequence of operator symbols like +, -, *, /, =, ->, =>, :, ., etc. */
INT_LITERAL ::= /* Integer in decimal, hex (0x...), or binary (0b...) format */
RAT_LITERAL ::= /* Rational/float literal (e.g., 1.2, 3.14e-5) */
STRING_LITERAL ::= /* Double-quoted string "..." with escapes */
Newline ::= /* \n or \r\n */
標點符號和符號(終結符)
'(' | ')' | '{' | '}' | '[' | ']' | ',' | ';' | ':' | '.' | '=' | '=>' | '->' | '@' | '#'