source: doc/proposals/modules-alvin/1_stitched_modules/parser/CMOD.g4

stuck-waitfor-destruct
Last change on this file was 2cb10170, checked in by Alvin Zhang <alvin.zhang@…>, 3 weeks ago

stitched modules proposal

  • Property mode set to 100644
File size: 1.9 KB
Line 
1// C-like language with expressions removed, used to showcase module capabilities
2
3grammar CMOD;
4
5compilationUnit
6 : translationUnit EOF
7 ;
8
9translationUnit
10 : moduleDeclaration importDeclaration* externalDeclaration*
11 ;
12
13moduleDeclaration
14 : 'module' ';'
15 ;
16
17importDeclaration
18 : 'import' (StringLiteral | (Identifier ('/' Identifier)*)) ';'
19 ;
20
21externalDeclaration
22 : 'export'? limitedFunctionDefinition
23 | 'export'? limitedGlobal
24 | 'export'? limitedStruct
25 | ';' // stray ;
26 ;
27
28limitedFunctionDefinition
29 : limitedTypeSpecifier limitedDeclarator '(' limitedParameterList? ')' limitedCompoundStatement
30 ;
31
32limitedTypeSpecifier
33 : 'void'
34 | 'char'
35 | 'short'
36 | 'int'
37 | 'long'
38 | 'float'
39 | 'double'
40 | 'struct' Identifier
41 ;
42
43limitedDeclarator
44 : '*'? Identifier
45 ;
46
47limitedParameterList
48 : limitedTypeSpecifier limitedDeclarator (',' limitedTypeSpecifier limitedDeclarator)*
49 ;
50
51limitedCompoundStatement
52 : '{' limitedStatement* '}'
53 ;
54
55limitedStatement
56 : Identifier ';' // these are values or functions
57 | 'type' Identifier ';' // these are structs, need full definition
58 | 'type' '*' Identifier ';' // these are structs, only need forward declaration
59 ;
60
61limitedGlobal
62 : limitedTypeSpecifier limitedDeclarator ('=' limitedInitializer)? ';'
63 ;
64
65limitedInitializer
66 : Identifier ('+' Identifier)* // these are values or functions
67 ;
68
69limitedStruct
70 : 'struct' Identifier limitedCompoundStatement ';'
71 ;
72
73Identifier
74 : Nondigit (Nondigit | Digit)*
75 ;
76
77fragment Nondigit
78 : [a-zA-Z_$]
79 ;
80
81fragment Digit
82 : [0-9]
83 ;
84
85StringLiteral
86 : '"' SChar* '"'
87 ;
88
89fragment SChar
90 : ~["\r\n]
91 ;
92
93Whitespace
94 : [ \t]+ -> channel(HIDDEN)
95 ;
96
97Newline
98 : ('\r' '\n'? | '\n') -> channel(HIDDEN)
99 ;
100
101BlockComment
102 : '/*' .*? '*/' -> channel(HIDDEN)
103 ;
104
105LineComment
106 : '//' ~[\r\n]* -> channel(HIDDEN)
107 ;
Note: See TracBrowser for help on using the repository browser.