-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.pegjs
136 lines (105 loc) · 2.46 KB
/
grammar.pegjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Structal Script Grammar
*
* This grammar is in form of peg, specifically parseable by pegjs (https://pegjs.org)
*
* Jian Li
*/
// start
Start
= _0 Declarations? {}
// all allowed declarations
Declarations
= (ImportDeclaration _1)* (ExtendsDeclaration _1)? (ConstructorDeclaration _1)? (Function _1)*
// import declaration
ImportDeclaration
= "import" _ (NameSpaceIdentifier ".")* TypeIdentifier
// extends declaration
ExtendsDeclaration
= "extends" _ TypeIdentifier
// constructor
ConstructorDeclaration
= "constructor" FunctionPart
// function
Function
= FunctionDeclaration
/ StaticFunctionDeclaration
// identifier used for namespace
NameSpaceIdentifier
= LowerLetter+
// type identifier used for type
TypeIdentifier
= UpperLetter IdentifierPart*
// regular identifier, which is used as name of constructor, function, parameter, etc.
Identifier
= LowerLetter IdentifierPart*
// non-start of identifier
IdentifierPart
= Letter
/ Digit
// function declaration
FunctionDeclaration
= "function" FunctionPart
// static function declaration
StaticFunctionDeclaration
= "static" _ FunctionDeclaration
// function part starting with name followed by parameter list and body
FunctionPart
= _ Identifier _ "(" _ ParameterList? _ ")" _ EOL? FunctionBody
// function body staring with "{" and ending with "}"
FunctionBody
= "{" _1 (SourceLine / EOL)* "}"
// parameter list for constructor or function
ParameterList
= Identifier (_ "," __ Identifier)*
// any white space
_
= WhiteSpace*
// any white space, comment, with end of line
_0
= ((WhiteSpace / Comment)* EOL)*
// at least one end of line with optional white space or comment preceeding it
_1
= ((WhiteSpace / Comment)* EOL)+
// any white space, end of line, or comment
__
= (WhiteSpace / EOL / Comment)*
// white spaces
WhiteSpace
= "\t"
/ "\v"
/ "\f"
/ " "
/ "\u00A0"
/ "\uFEFF"
/ Zs
// end of line
EOL
= "\n"
/ "\r\n"
/ "\r"
// Separator, Space
Zs = [\u0020\u00A0]
SourceLineCharacter
= !EOL .
SourceLine
= WhiteSpace+ SourceLineCharacter* EOL
Comment
= MultiLineComment
/ SingleLineComment
// single line comment
SingleLineComment
= "//" SourceLineCharacter*
// multi-line comment
MultiLineComment
= "/*" (!"*/" .)* "*/"
// Ascii upper or lower letter
Letter
= UpperLetter
/ LowerLetter
// Ascii Lowercase Letter
LowerLetter = [\u0061-\u007A]
// Ascii Uppercase Letter
UpperLetter = [\u0041-\u005A]
// Ascii Decimal Digit
Digit = [\u0030-\u0039]