]> gitweb.ps.run Git - toc/blob - Toc.g4
add namespace, private struct member grammar, change bracket style
[toc] / Toc.g4
1 grammar Toc;\r
2 \r
3 prog: (decl)+ EOF;\r
4 \r
5 decl: varDecl ';'\r
6     | funcDecl\r
7     | structDecl\r
8     | namespaceDecl\r
9     ;\r
10 \r
11 namespaceDecl: 'namespace' typeName '{' decl* '}';\r
12 \r
13 varDecl:  'var' var;\r
14 var:  varName (':' type) ('=' expr)?;\r
15 varInit:  varName (':' type) ('=' expr);\r
16 \r
17 type: typeName (typeModifier)*;\r
18 typeModifier: '*' | ('[' (INT_LIT)? ']');\r
19 \r
20 \r
21 funcDecl: 'func' func;\r
22 func: funcName genericDecl? '(' parameter ')' (':' type) body;\r
23 parameter: (var (',' var)*)?;\r
24 \r
25 body: '{' stmt* '}';\r
26 \r
27 \r
28 structDecl: 'struct' structName genericDecl? '{' structMember* '}';\r
29 structMember: privateDecl? structVar | structMethod;\r
30 structVar: var ';';\r
31 structMethod: func;\r
32 privateDecl: 'private';\r
33 \r
34 genericDecl: '<' typeName (',' typeName)* '>';\r
35 \r
36 stmt: varDecl ';'\r
37     | ifStmt\r
38     | switchStmt\r
39     | forStmt\r
40     | whileStmt\r
41     | assignStmt ';'\r
42     | returnStmt ';'\r
43     | expr ';';\r
44 \r
45 ifStmt: 'if' expr body elseIfStmt* elseStmt?;\r
46 elseIfStmt: 'else' 'if' expr body;\r
47 elseStmt: 'else' body;\r
48 \r
49 switchStmt: 'switch' identifierExpr switchBody;\r
50 switchBody: '{' switchCase* '}';\r
51 switchCase: 'case' expr body;\r
52 \r
53 forStmt: 'for' (varInit | assignStmt) ',' expr ',' expr body;\r
54 \r
55 whileStmt: 'while' expr body;\r
56 \r
57 assignStmt: identifierExpr '=' expr;\r
58 \r
59 returnStmt: 'return' expr;\r
60 \r
61 expr: funcExpr\r
62     | litExpr\r
63     | identifierExpr\r
64     | parenExpr\r
65     | accessExpr\r
66     | opExpr;\r
67 \r
68 /* op */\r
69 nonOpExpr: funcExpr\r
70          | litExpr\r
71          | identifierExpr\r
72          | parenExpr\r
73          | accessExpr;\r
74 \r
75 /* lit access op */\r
76 nonAccessExpr: funcExpr\r
77              | identifierExpr\r
78              | parenExpr;\r
79 \r
80 funcExpr: funcName '(' (expr (',' expr)*)? ')';\r
81 \r
82 opExpr: binaryOp | prefixOp | postfixOp | ternaryOp;\r
83 binaryOp: nonOpExpr binary_op nonOpExpr (binary_op nonOpExpr)*;\r
84 prefixOp: prefix_op nonOpExpr;\r
85 postfixOp: nonOpExpr postfix_op;\r
86 ternaryOp: nonOpExpr '?' expr ':' expr;\r
87 \r
88 identifierExpr: varName;\r
89 \r
90 litExpr: INT_LIT | DECIMAL_LIT | STRING_LIT | BOOL_LIT;\r
91 \r
92 accessExpr: nonAccessExpr (accessSubExpr)+;\r
93 accessSubExpr: accessMember | accessBrackets;\r
94 accessMember: ('.' | '->') identifierExpr;\r
95 accessBrackets: '[' expr ']';\r
96 \r
97 parenExpr: '(' expr ')';\r
98 \r
99 funcName: NAME;\r
100 varName: NAME;\r
101 typeName: NAME;\r
102 structName: NAME;\r
103 \r
104 \r
105 postfix_op:\r
106     '++' | '--';\r
107 prefix_op:\r
108     '+' | '-' | '!' | '~' | '&' | '*' | postfix_op;\r
109 binary_op:\r
110     '+' | '-' | '*' | '/' | '%' | '&' | '<' | '|' | '^' | '>' |\r
111     '==' | '!=' | '<=' | '>=' | '<'  | '>' |\r
112     '<<' | '>>' | '||' | '&&' | '&=' | '|=' | '^=' |\r
113     '<<=' | '>>=' | '+=' | '-=' | '*=' | '/=' | '%=';\r
114 \r
115 INT_LIT: ('+' | '-')? [0-9]+;\r
116 DECIMAL_LIT: ('+' | '-')* [0-9]+ '.' [0-9]+;\r
117 STRING_LIT: '"' [^"]* '"';\r
118 BOOL_LIT: 'true' | 'false';\r
119 \r
120 NAME: ([a-z] | [A-Z] | [0-9])+;\r
121 WS: [ \t\r\n]+ -> skip;\r
122 NEWLINE: [\r\n]+;\r