forked from kframework/javascript-semantics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js-orig-syntax.k
192 lines (169 loc) · 8.69 KB
/
js-orig-syntax.k
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
module JS-ORIG-SYNTAX
// Based on ECMA-262, 5.1 Edition, June 2011
// A.5 Programs
syntax Program ::= SourceElements
syntax SourceElements ::= List{SourceElement, ""}
syntax SourceElement ::= Statement
| FunctionExpression
// A.5 Functions
syntax FunctionExpression ::= "function" Name "(" Parameters ")" "{" FunctionBody "}"
| "function" "(" Parameters ")" "{" FunctionBody "}"
syntax Parameters ::= List{Name, ","}
syntax FunctionBody ::= SourceElements
// A.4 Statements
syntax Statements ::= List{Statement, ""}
syntax Statement ::= Block
| "var" VariableDeclarationS ";"
//| ";"
| ExpressionS ";"
| "if" "(" ExpressionS ")" Statement
| "if" "(" ExpressionS ")" Statement "else" Statement [prefer]
| "do" Statement "while" "(" ExpressionS ")" ";"
| "while" "(" ExpressionS ")" Statement
| "for" "(" Expressions ";" Expressions ";" Expressions ")" Statement
| "for" "(" "var" VariableDeclarationS ";" Expressions ";" Expressions ")" Statement
| "for" "(" LeftExpression "in" ExpressionS ")" Statement
| "for" "(" "var" VariableDeclaration "in" ExpressionS ")" Statement
| "continue" ";"
| "continue" Name ";"
| "break" ";"
| "break" Name ";"
| "return" ";"
| "return" ExpressionS ";"
| "with" "(" ExpressionS ")" Statement
| Name ":" Statement // label
| "switch" "(" ExpressionS ")" CaseBlock
| "throw" ExpressionS ";"
| "try" Block Catch
| "try" Block Finally
| "try" Block Catch Finally
| "debugger" ";"
syntax Block ::= "{" Statements "}"
syntax VariableDeclarationS ::= NeList{VariableDeclaration, ","}
syntax VariableDeclaration ::= Name
| Name "=" Expression
syntax CaseBlock ::= "{" CaseClauses "}"
| "{" CaseClauses DefaultClause CaseClauses "}"
syntax CaseClauses ::= List{CaseClause, ""}
syntax CaseClause ::= "case" ExpressionS ":" Statements
syntax DefaultClause ::= "default" ":" Statements
syntax Catch ::= "catch" "(" Name ")" Block
syntax Finally ::= "finally" Block
// A.3 Expressionessions
syntax ExpressionS ::= NeList{Expression, ","}
syntax Expressions ::= List{Expression, ","}
syntax Literal ::= "this"
| Null
| Bool
| Int
| Float
| String
syntax Null ::= "null"
syntax Name ::= Token{[\_\$A-Za-z][\_\$A-Za-z0-9]*} [notInRules]
// 11.1.5 Object Initialiser
//
// PropertyName ::= IdentifierName
// | StringLiteral
// | NumericLiteral
//
// Note that PropertyName is IdentifierName not Identifier, where Identifier is IdentifierName but not ReservedWord.
// Thus, both Null and Bool also can be PropertyName.
syntax PropertyName ::= Name
| Null
| Bool
| String
| Int
| Float
syntax ArrayElements ::= List{ExpressionOpt, ","}
syntax ExpressionOpt ::= Expression /* | "" [avoid, onlyLabel, klabel('epsilonExpression)] */
/* FIXME: Currently K parser crashes the empty array literal: [ ] */
syntax PropertyAssignments ::= List{PropertyAssignment, ","}
syntax PropertyAssignment ::= PropertyName ":" Expression
| Name /* get */ PropertyName "(" ")" "{" FunctionBody "}"
| Name /* set */ PropertyName "(" Name ")" "{" FunctionBody "}"
syntax PrimaryExpression ::= Name
| Literal
| "[" ArrayElements "]"
| "{" PropertyAssignments "}"
| "(" ExpressionS ")"
syntax MemberExpression ::= PrimaryExpression
> FunctionExpression
| MemberExpression "[" Expressions "]"
| MemberExpression "." Name
| "new" MemberExpression "(" Expressions ")"
//
syntax NewExpression ::= MemberExpression
| "new" NewExpression
//
syntax CallExpression ::= MemberExpression "(" Expressions ")"
| CallExpression "(" Expressions ")"
| CallExpression "[" Expressions "]"
| CallExpression "." Name
//
syntax LeftExpression ::= NewExpression | CallExpression
syntax Expression ::= LeftExpression
> Expression "++"
| Expression "--"
> "delete" Expression
| "void" Expression
| "typeof" Expression
| "++" Expression [prefer]
| "--" Expression [prefer]
| "+" Expression
| "-" Expression
| "~" Expression
| "!" Expression
> left:
Expression "*" Expression [left]
| Expression "/" Expression [left]
| Expression "%" Expression [left]
> left:
Expression "+" Expression [left]
| Expression "-" Expression [left]
> left:
Expression "<<" Expression [left]
| Expression ">>" Expression [left]
| Expression ">>>" Expression [left]
> left:
Expression "<" Expression [left]
| Expression ">" Expression [left]
| Expression "<=" Expression [left]
| Expression ">=" Expression [left]
| Expression "instanceof" Expression [left,prefer]
| Expression "in" Expression [left]
> left:
Expression "==" Expression [left]
| Expression "!=" Expression [left]
| Expression "===" Expression [left]
| Expression "!==" Expression [left]
> left:
Expression "&" Expression [left]
> left:
Expression "^" Expression [left]
> left:
Expression "|" Expression [left]
> left:
Expression "&&" Expression [left]
> left:
Expression "||" Expression [left]
> Expression "?" Expression ":" Expression
> right:
LeftExpression "=" Expression [right]
| LeftExpression "*=" Expression [right]
| LeftExpression "/=" Expression [right]
| LeftExpression "%=" Expression [right]
| LeftExpression "+=" Expression [right]
| LeftExpression "-=" Expression [right]
| LeftExpression "<<=" Expression [right]
| LeftExpression ">>=" Expression [right]
| LeftExpression ">>>=" Expression [right]
| LeftExpression "&=" Expression [right]
| LeftExpression "^=" Expression [right]
| LeftExpression "|=" Expression [right]
// Utils
syntax String ::= StringOfName(Name) [function, notInPrograms]
rule StringOfName(N:Name) => #tokenToString(N)
//
syntax Name ::= NameOfString(String) [function, notInPrograms]
rule NameOfString(S:String) => #parseToken("Name", S)
endmodule