X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/45409c781a9e35df68c43b1e2f028d30bf90c0a0..71a20a4f3d4e5f5278f7d004af710af89dfd7ebc:/Toc.g4 diff --git a/Toc.g4 b/Toc.g4 index 95c9a0d..0f2f613 100644 --- a/Toc.g4 +++ b/Toc.g4 @@ -2,81 +2,97 @@ grammar Toc; prog: (decl)+ EOF; -decl: varDecl +decl: varDecl ';' | funcDecl | structDecl + | namespaceDecl ; +namespaceDecl: 'namespace' typeName '{' decl* '}'; + varDecl: 'var' var; var: varName (':' type) ('=' expr)?; +varInit: varName (':' type) ('=' expr); -type: typeName; +type: typeName (typeModifier)*; +typeModifier: '*' | ('[' (INT_LIT)? ']'); funcDecl: 'func' func; -func: funcName '(' parameter ')' (':' type) body; -parameter: (firstParameter (additionalParameter)*)?; -firstParameter: var; -additionalParameter: ',' var; +func: funcName genericDecl? '(' parameter ')' (':' type) body; +parameter: (var (',' var)*)?; body: '{' stmt* '}'; -structDecl: 'struct' structName '{' structMember* '}'; -structMember: structVar | structMethod; -structVar: var; +structDecl: 'struct' structName genericDecl? '{' structMember* '}'; +structMember: privateDecl? structVar | structMethod; +structVar: var ';'; structMethod: func; +privateDecl: 'private'; + +genericDecl: '<' typeName (',' typeName)* '>'; + +stmt: varDecl ';' + | ifStmt + | switchStmt + | forStmt + | whileStmt + | assignStmt ';' + | returnStmt ';' + | expr ';'; +ifStmt: 'if' expr body elseIfStmt* elseStmt?; +elseIfStmt: 'else' 'if' expr body; +elseStmt: 'else' body; -stmt: (varDecl - | conditional - | loop - | assignment - | returnStmt - | expr) ; +switchStmt: 'switch' identifierExpr switchBody; +switchBody: '{' switchCase* '}'; +switchCase: 'case' expr body; -conditional: ifCond; -ifCond: 'if' expr body; +forStmt: 'for' (varInit | assignStmt) ',' expr ',' expr body; -loop: whileLoop; -whileLoop: 'while' expr body; +whileStmt: 'while' expr body; -assignment: identifier '=' expr; +assignStmt: identifierExpr '=' expr; returnStmt: 'return' expr; -expr: funcCall - | literal - | identifier - | subscript - | memberAccess +expr: funcExpr + | litExpr + | identifierExpr | parenExpr - | operatorExpr; + | accessExpr + | opExpr; -nonOpExpr: funcCall - | literal - | identifier - | subscript - | memberAccess - | parenExpr; +/* op */ +nonOpExpr: funcExpr + | litExpr + | identifierExpr + | parenExpr + | accessExpr; -nonSubscriptExpr: funcCall - | identifier - | memberAccess - | parenExpr; +/* lit access op */ +nonAccessExpr: funcExpr + | identifierExpr + | parenExpr; -funcCall: funcName '(' (expr (',' expr)*)? ')'; +funcExpr: funcName '(' (expr (',' expr)*)? ')'; -operatorExpr: binaryOperator; -binaryOperator: nonOpExpr BINARY_OPERATOR nonOpExpr (BINARY_OPERATOR nonOpExpr)*; +opExpr: binaryOp | prefixOp | postfixOp | ternaryOp; +binaryOp: nonOpExpr binary_op nonOpExpr (binary_op nonOpExpr)*; +prefixOp: prefix_op nonOpExpr; +postfixOp: nonOpExpr postfix_op; +ternaryOp: nonOpExpr '?' expr ':' expr; -identifier: varName; +identifierExpr: varName; -literal: INTLIT; +litExpr: INT_LIT | DECIMAL_LIT | STRING_LIT | BOOL_LIT; -subscript: nonSubscriptExpr '[' expr ']'; - -memberAccess: identifier '.' identifier; +accessExpr: nonAccessExpr (accessSubExpr)+; +accessSubExpr: accessMember | accessBrackets; +accessMember: ('.' | '->') identifierExpr; +accessBrackets: '[' expr ']'; parenExpr: '(' expr ')'; @@ -86,11 +102,21 @@ typeName: NAME; structName: NAME; -BINARY_OPERATOR: - '+' | '-' | '*' | '/' - | '==' | '!=' - | '<' | '>'; -INTLIT: ('+' | '-')? [0-9]+; +postfix_op: + '++' | '--'; +prefix_op: + '+' | '-' | '!' | '~' | '&' | '*' | postfix_op; +binary_op: + '+' | '-' | '*' | '/' | '%' | '&' | '<' | '|' | '^' | '>' | + '==' | '!=' | '<=' | '>=' | '<' | '>' | + '<<' | '>>' | '||' | '&&' | '&=' | '|=' | '^=' | + '<<=' | '>>=' | '+=' | '-=' | '*=' | '/=' | '%='; + +INT_LIT: ('+' | '-')? [0-9]+; +DECIMAL_LIT: ('+' | '-')* [0-9]+ '.' [0-9]+; +STRING_LIT: '"' [^"]* '"'; +BOOL_LIT: 'true' | 'false'; + NAME: ([a-z] | [A-Z] | [0-9])+; WS: [ \t\r\n]+ -> skip; NEWLINE: [\r\n]+;