abouttreesummaryrefslogcommitdiff
path: root/Toc.g4
diff options
context:
space:
mode:
authorPatrick Schönberger2021-07-28 09:07:53 +0200
committerPatrick Schönberger2021-07-28 09:07:53 +0200
commit45409c781a9e35df68c43b1e2f028d30bf90c0a0 (patch)
tree0085614e19fdc136f664568e89f1686332ba8850 /Toc.g4
downloadtoc-45409c781a9e35df68c43b1e2f028d30bf90c0a0.tar.gz
toc-45409c781a9e35df68c43b1e2f028d30bf90c0a0.zip
Initial commit
Diffstat (limited to 'Toc.g4')
-rw-r--r--Toc.g496
1 files changed, 96 insertions, 0 deletions
diff --git a/Toc.g4 b/Toc.g4
new file mode 100644
index 0000000..95c9a0d
--- /dev/null
+++ b/Toc.g4
@@ -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]+;