79 lines
1.9 KiB
Text
79 lines
1.9 KiB
Text
YourLang {
|
|
// Entry point
|
|
Program = Statement*
|
|
|
|
// Statements
|
|
Statement = LiveDecl
|
|
| ComputedDecl
|
|
| TrackStmt
|
|
| VarDecl
|
|
| ExprStatement
|
|
| Block
|
|
|
|
// Reactive declarations
|
|
LiveDecl = "live" identifier "=" Expr
|
|
|
|
ComputedDecl = "computed" identifier Block
|
|
|
|
TrackStmt = "track" ListOf<identifier, ","> LambdaParams? Block
|
|
|
|
// Regular variable declarations
|
|
VarDecl = VarType identifier "=" Expr
|
|
VarType = "const" | "var" | "let"
|
|
|
|
// Expression statement (for function calls, etc)
|
|
ExprStatement = Expr
|
|
|
|
// Blocks and lambdas
|
|
Block = "{" Statement* "}"
|
|
|
|
LambdaParams = "|" ListOf<identifier, ","> "|"
|
|
|
|
// Expressions (precedence from loose to tight)
|
|
Expr = AssignExpr
|
|
|
|
AssignExpr = CompareExpr ("=" CompareExpr)?
|
|
|
|
CompareExpr = AddExpr (CompareOp AddExpr)?
|
|
CompareOp = "==" | "!=" | "<=" | ">=" | "<" | ">"
|
|
|
|
AddExpr = MulExpr (("+"|"-") MulExpr)*
|
|
|
|
MulExpr = UnaryExpr (("*"|"/"|"%") UnaryExpr)*
|
|
|
|
UnaryExpr = "!" UnaryExpr
|
|
| "-" UnaryExpr
|
|
| CallExpr
|
|
|
|
CallExpr = MemberExpr ("(" ListOf<Expr, ","> ")")?
|
|
|
|
MemberExpr = PrimaryExpr ("." identifier)*
|
|
|
|
PrimaryExpr = number
|
|
| string
|
|
| boolean
|
|
| identifier
|
|
| "(" Expr ")"
|
|
| Block // Block expressions
|
|
|
|
// Literals
|
|
number = digit+ ("." digit+)?
|
|
|
|
string = "\"" (~"\"" any)* "\""
|
|
| "'" (~"'" any)* "'"
|
|
| "`" templateChar* "`"
|
|
|
|
templateChar = ~"`" any
|
|
|
|
boolean = "true" | "false"
|
|
|
|
// Identifiers and keywords
|
|
identifier = ~keyword letter (letter | digit | "_")*
|
|
|
|
keyword = "live" | "computed" | "track" | "const" | "var" | "let"
|
|
| "true" | "false" | "if" | "else" | "for" | "while"
|
|
|
|
// Lexical rules
|
|
space += "//" (~"\n" any)* // Single-line comments
|
|
| "/*" (~"*/" any)* "*/" // Multi-line comments
|
|
}
|