diff options
| author | Patrick Schönberger | 2021-07-28 09:07:53 +0200 |
|---|---|---|
| committer | Patrick Schönberger | 2021-07-28 09:07:53 +0200 |
| commit | 45409c781a9e35df68c43b1e2f028d30bf90c0a0 (patch) | |
| tree | 0085614e19fdc136f664568e89f1686332ba8850 /Toc.g4 | |
| download | toc-45409c781a9e35df68c43b1e2f028d30bf90c0a0.tar.gz toc-45409c781a9e35df68c43b1e2f028d30bf90c0a0.zip | |
Initial commit
Diffstat (limited to 'Toc.g4')
| -rw-r--r-- | Toc.g4 | 96 |
1 files changed, 96 insertions, 0 deletions
@@ -0,0 +1,96 @@ +grammar Toc;
+
+prog: (decl)+ EOF;
+
+decl: varDecl
+ | funcDecl
+ | structDecl
+ ;
+
+varDecl: 'var' var;
+var: varName (':' type) ('=' expr)?;
+
+type: typeName;
+
+
+funcDecl: 'func' func;
+func: funcName '(' parameter ')' (':' type) body;
+parameter: (firstParameter (additionalParameter)*)?;
+firstParameter: var;
+additionalParameter: ',' var;
+
+body: '{' stmt* '}';
+
+
+structDecl: 'struct' structName '{' structMember* '}';
+structMember: structVar | structMethod;
+structVar: var;
+structMethod: func;
+
+
+stmt: (varDecl
+ | conditional
+ | loop
+ | assignment
+ | returnStmt
+ | expr) ;
+
+conditional: ifCond;
+ifCond: 'if' expr body;
+
+loop: whileLoop;
+whileLoop: 'while' expr body;
+
+assignment: identifier '=' expr;
+
+returnStmt: 'return' expr;
+
+expr: funcCall
+ | literal
+ | identifier
+ | subscript
+ | memberAccess
+ | parenExpr
+ | operatorExpr;
+
+nonOpExpr: funcCall
+ | literal
+ | identifier
+ | subscript
+ | memberAccess
+ | parenExpr;
+
+nonSubscriptExpr: funcCall
+ | identifier
+ | memberAccess
+ | parenExpr;
+
+funcCall: funcName '(' (expr (',' expr)*)? ')';
+
+operatorExpr: binaryOperator;
+binaryOperator: nonOpExpr BINARY_OPERATOR nonOpExpr (BINARY_OPERATOR nonOpExpr)*;
+
+identifier: varName;
+
+literal: INTLIT;
+
+subscript: nonSubscriptExpr '[' expr ']';
+
+memberAccess: identifier '.' identifier;
+
+parenExpr: '(' expr ')';
+
+funcName: NAME;
+varName: NAME;
+typeName: NAME;
+structName: NAME;
+
+
+BINARY_OPERATOR:
+ '+' | '-' | '*' | '/'
+ | '==' | '!='
+ | '<' | '>';
+INTLIT: ('+' | '-')? [0-9]+;
+NAME: ([a-z] | [A-Z] | [0-9])+;
+WS: [ \t\r\n]+ -> skip;
+NEWLINE: [\r\n]+;
|
