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