1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
use core.lang;
optional delimiter = SDelimiter;
void SDelimiter();
SDelimiter : "[ \n\r\t]*" - (SComment - SDelimiter)?;
void SComment();
SComment : "//[^\n]*\n" #comment;
Str SIdentifier();
SIdentifier => x : "[A-Za-z]+" x;
Array<DemoFunction> SRoot();
SRoot => functions : , (SFunction functions, )*;
DemoFunction SFunction();
SFunction => DemoFunction(pos, name, params, body)
: SIdentifier name #fnName, "(", SParamList params, ")", "=", SExpr @body;
Array<Str> SParamList();
SParamList => Array<Str>() : ;
SParamList => Array<Str>()
: SIdentifier -> push #varName - (, ",", SIdentifier -> push #varName)*;
Expr SExpr(Scope scope);
SExpr => OpAdd(pos, l, r) : SExpr(scope) l, "\+", SProduct(scope) r;
SExpr => OpSub(pos, l, r) : SExpr(scope) l, "-", SProduct(scope) r;
SExpr => p : SProduct(scope) p;
Expr SProduct(Scope scope);
SProduct => OpMul(pos, l, r) : SProduct(scope) l, "\*", SAtom(scope) r;
SProduct => OpDiv(pos, l, r) : SProduct(scope) l, "/", SAtom(scope) r;
SProduct => a : SAtom(scope) a;
Expr SAtom(Scope scope);
SAtom => Literal(pos, num) : "-?[0-9]+" num #constant;
SAtom => VarAccess(pos, name, scope) : SIdentifier name #varName;
SAtom => FnCall(pos, name, params, scope)
: SIdentifier name #fnName, "(", SActuals(scope) params, ")";
SAtom => x : "(", SExpr(scope) x, ")";
Array<Expr> SActuals(Scope scope);
SActuals => Array<Expr>() : ;
SActuals => Array<Expr>() : SExpr(scope) -> push - (, ",", SExpr(scope) -> push)*;
|