forked from kframework/javascript-semantics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js-syntax.k
184 lines (162 loc) · 8.36 KB
/
js-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
module JS-SYNTAX
// Based on ECMA-262, 5.1 Edition, June 2011
// A.5 Programs
syntax Program ::= SourceElements
syntax SourceElements ::= List{SourceElement, ""}
syntax SourceElement ::= Stmt
| FunExp
// A.5 Functions
syntax FunExp ::= "function" Name "(" Params ")" "{" FunBody "}"
| "function" "(" Params ")" "{" FunBody "}"
syntax Params ::= List{Name, ","}
syntax FunBody ::= SourceElements
// A.4 Statements
syntax Stmts ::= List{Stmt, ""}
syntax DeclStmt ::= "var" VarDeclS ";"
syntax CaseStmt ::= "switch" "(" ExpS ")" CaseBlock [strict(1)]
syntax OtherStmt ::= Block
| ";"
| ExpS ";" [strict(1)]
| "if" "(" ExpS ")" Stmt [strict(1)]
| "if" "(" ExpS ")" Stmt "else" Stmt [strict(1)]
| "do" Stmt "while" "(" ExpS ")" ";"
| "while" "(" ExpS ")" Stmt
| "for" "(" Exps ";" Exps ";" Exps ")" Stmt
| "for" "(" "var" VarDeclS ";" Exps ";" Exps ")" Stmt
| "for" "(" LExp "in" ExpS ")" Stmt
| "for" "(" "var" VarDecl "in" ExpS ")" Stmt
| "continue" ";"
| "continue" Name ";"
| "break" ";"
| "break" Name ";"
| "return" ";"
| "return" ExpS ";" [strict(1)]
| "with" "(" ExpS ")" Stmt [strict(1)]
| Name ":" Stmt // label
| "throw" ExpS ";" [strict(1)]
| "try" Block Catch
| "try" Block Finally
| "try" Block Catch Finally
| "debugger" ";"
| "print" "(" Exp ")" ";" [strict(1)]
syntax Stmt ::= DeclStmt
| CaseStmt
| OtherStmt
syntax Block ::= "{" Stmts "}"
syntax VarDeclS ::= NeList{VarDecl, ","}
syntax VarDecl ::= Name
| Name "=" Exp [strict(2)]
syntax CaseBlock ::= "{" CaseClauses "}"
| "{" CaseClauses DefaultClause CaseClauses "}"
syntax CaseClauses ::= List{CaseClause, ""}
syntax CaseClause ::= "case" ExpS ":" Stmts [strict(1)]
syntax DefaultClause ::= "default" ":" Stmts
syntax Catch ::= "catch" "(" Name ")" Block
syntax Finally ::= "finally" Block
// A.3 Expressions
syntax ExpS ::= NeList{Exp, ","} [seqstrict]
syntax Exps ::= List{Exp, ","} [seqstrict]
syntax AtomicExp ::= Name
| Literal
| "[" ArrayElements "]"
| "{" PropertyAssignments "}"
| "(" Exps ")" [bracket]
syntax Name ::= Token{[\_\$A-Za-z][\_\$A-Za-z0-9]*} [notInRules]
syntax String ::= StringOfName(Name) [function]
rule StringOfName(N:Name) => #tokenToString(N)
syntax Name ::= NameOfString(String) [function]
rule NameOfString(S:String) => #parseToken("Name", S)
syntax Literal ::= "this"
| "null"
| Bool
| Int
| Float
| String
syntax ArrayElements ::= List{ExpOpt, ","}
syntax ExpOpt ::= Exp | "" [onlyLabel, klabel("'epsilonExp")]
syntax PropertyAssignments ::= List{PropertyAssignment, ","}
syntax PropertyAssignment ::= PropertyName ":" Exp [strict(2)]
| "get" PropertyName "(" ")" "{" FunBody "}"
| "set" PropertyName "(" Name ")" "{" FunBody "}"
syntax PropertyName ::= Name
| String
| Int
| Float
syntax PropertyLExp ::= LExp "[" Exps "]" [seqstrict,klabel("'lookupLExp")]
syntax LExp ::= AtomicExp
> FunExp
//| LExp "[" Exps "]" [seqstrict,klabel("'lookupLExp")]
| PropertyLExp
| LExp "." Name
| "new" LExp "(" Exps ")" [seqstrict,prefer]
//| LExp "(" Exps ")" [seqstrict] // TODO: WRONG!! It should be: [strict(2)]
| LExp "(" Exps ")" [strict(2)] // TODO: WRONG!! It should be: [strict(2)]
> "new" LExp [strict(1)]
syntax Exp ::= LExp
> Exp "++" [strict(1)]
| Exp "--" [strict(1)]
> "delete" Exp [strict(1)]
| "void" Exp [strict(1)]
| "typeof" Exp [strict(1)]
| "++" Exp [strict(1),prefer]
| "--" Exp [strict(1),prefer]
| "+" Exp [strict(1)]
| "-" Exp [strict(1)]
| "~" Exp [strict(1)]
| "!" Exp [strict(1)]
> left:
Exp "*" Exp [seqstrict,left]
| Exp "/" Exp [seqstrict,left]
| Exp "%" Exp [seqstrict,left]
> left:
Exp "+" Exp [seqstrict,left]
| Exp "-" Exp [seqstrict,left]
> left:
Exp "<<" Exp [seqstrict,left]
| Exp ">>" Exp [seqstrict,left]
| Exp ">>>" Exp [seqstrict,left]
> left:
Exp "<" Exp [seqstrict,left]
| Exp ">" Exp [seqstrict,left]
| Exp "<=" Exp [seqstrict,left]
| Exp ">=" Exp [seqstrict,left]
| Exp "instanceof" Exp [seqstrict,left]
| Exp "in" Exp [seqstrict,left]
> left:
Exp "==" Exp [seqstrict,left]
| Exp "!=" Exp [seqstrict,left]
| Exp "===" Exp [seqstrict,left]
| Exp "!==" Exp [seqstrict,left]
> left:
Exp "&" Exp [seqstrict,left]
> left:
Exp "^" Exp [seqstrict,left]
> left:
Exp "|" Exp [seqstrict,left]
> left:
Exp "&&" Exp [strict(1),left]
> left:
Exp "||" Exp [strict(1),left]
> Exp "?" Exp ":" Exp [strict(1)]
> right:
LExp "=" Exp [strict(2),right,klabel("'assignExp")]
| LExp "*=" Exp [strict(2),right]
| LExp "/=" Exp [strict(2),right]
| LExp "%=" Exp [strict(2),right]
| LExp "+=" Exp [strict(2),right]
| LExp "-=" Exp [strict(2),right]
| LExp "<<=" Exp [strict(2),right]
| LExp ">>=" Exp [strict(2),right]
| LExp ">>>=" Exp [strict(2),right]
| LExp "&=" Exp [strict(2),right]
| LExp "^=" Exp [strict(2),right]
| LExp "|=" Exp [strict(2),right]
//////////////////////////////////////////////////////////////////////////////
// Desugaring syntactic sugars
//////////////////////////////////////////////////////////////////////////////
rule new E:LExp => new E (.Exps) [macro]
// 11.2.1 Property Accessors
rule E:LExp . N:Name => E[StringOfName(N)] [macro]
syntax Exp ::= assignExp(LExp,Exp)
syntax LExp ::= lookupLExp(LExp,Exps)
endmodule