X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/5f9668526491332f62c05ad831dbf6d5fdc2b6d0..8aeae09e74b46ca52866f22b48f55fecdf27b849:/Toc.g4 diff --git a/Toc.g4 b/Toc.g4 index 631e2c3..afe4209 100644 --- a/Toc.g4 +++ b/Toc.g4 @@ -2,7 +2,7 @@ grammar Toc; prog: (decl)+ EOF; -decl: varDecl +decl: varDecl ';' | funcDecl | structDecl ; @@ -12,7 +12,7 @@ var: varName (':' type) ('=' expr)?; varInit: varName (':' type) ('=' expr); type: typeName (typeModifier)*; -typeModifier: '*' | '[' NUMBER? ']'; +typeModifier: '*' | ('[' (INT_LIT)? ']'); funcDecl: 'func' func; @@ -24,23 +24,26 @@ body: '{' stmt* '}'; structDecl: 'struct' structName '{' structMember* '}'; structMember: structVar | structMethod; -structVar: var; +structVar: var ';'; structMethod: func; -stmt: varDecl +stmt: varDecl ';' | ifStmt | switchStmt | forStmt | whileStmt - | assignStmt - | returnStmt - | expr; + | assignStmt ';' + | returnStmt ';' + | expr ';'; -ifStmt: 'if' expr body ('else' 'if' expr body)* ('else' body)?; +ifStmt: 'if' expr body elseIfStmt* elseStmt?; +elseIfStmt: 'else' 'if' expr body; +elseStmt: 'else' body; switchStmt: 'switch' identifierExpr switchBody; -switchBody: '{' ('case' expr body)* '}'; +switchBody: '{' switchCase* '}'; +switchCase: 'case' expr body; forStmt: 'for' (varInit | assignStmt) ',' expr ',' expr body; @@ -72,16 +75,19 @@ nonAccessExpr: funcExpr funcExpr: funcName '(' (expr (',' expr)*)? ')'; opExpr: binaryOp | prefixOp | postfixOp | ternaryOp; -binaryOp: nonOpExpr BINARY_OP nonOpExpr (BINARY_OP nonOpExpr)*; -prefixOp: PREFIX_OP nonOpExpr; -postfixOp: nonOpExpr POSTFIX_OP; +binaryOp: nonOpExpr binary_op nonOpExpr (binary_op nonOpExpr)*; +prefixOp: prefix_op nonOpExpr; +postfixOp: nonOpExpr postfix_op; ternaryOp: nonOpExpr '?' expr ':' expr; identifierExpr: varName; litExpr: INT_LIT | DECIMAL_LIT | STRING_LIT | BOOL_LIT; -accessExpr: nonAccessExpr ((('.' | '->') identifierExpr) | ('[' expr ']'))+; +accessExpr: nonAccessExpr (accessSubExpr)+; +accessSubExpr: accessMember | accessBrackets; +accessMember: ('.' | '->') identifierExpr; +accessBrackets: '[' expr ']'; parenExpr: '(' expr ')'; @@ -91,12 +97,12 @@ typeName: NAME; structName: NAME; -POSTFIX_OP: +postfix_op: '++' | '--'; -PREFIX_OP: - [+!~&*-] | POSTFIX_OP; -BINARY_OP: - [+*/%&<|^>-] | +prefix_op: + '+' | '-' | '!' | '~' | '&' | '*' | postfix_op; +binary_op: + '+' | '-' | '*' | '/' | '%' | '&' | '<' | '|' | '^' | '>' | '==' | '!=' | '<=' | '>=' | '<' | '>' | '<<' | '>>' | '||' | '&&' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '+=' | '-=' | '*=' | '/=' | '%='; @@ -109,4 +115,3 @@ BOOL_LIT: 'true' | 'false'; NAME: ([a-z] | [A-Z] | [0-9])+; WS: [ \t\r\n]+ -> skip; NEWLINE: [\r\n]+; -NUMBER: [0-9]+;