Merge commit 'feccd2d3100fd2964d4c2df58ab3da7ce4949a66' into vm-check
[bpt/guile.git] / module / language / ecmascript / parse.scm
1 ;;; ECMAScript for Guile
2
3 ;; Copyright (C) 2009 Free Software Foundation, Inc.
4
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
8 ;; any later version.
9 ;;
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
19
20 ;;; Code:
21
22 (define-module (language ecmascript parse)
23 #:use-module (language ecmascript parse-lalr)
24 #:use-module (language ecmascript tokenize)
25 #:export (read-ecmascript read-ecmascript/1 parse-ecmascript))
26
27 (define (syntax-error message . args)
28 (apply throw 'SyntaxError message args))
29
30 (define (read-ecmascript port)
31 (parse-ecmascript (make-tokenizer port) syntax-error))
32
33 (define (read-ecmascript/1 port)
34 (parse-ecmascript (make-tokenizer/1 port) syntax-error))
35
36 (define *eof-object*
37 (call-with-input-string "" read-char))
38
39 (define parse-ecmascript
40 (lalr-parser
41 ;; terminal (i.e. input) token types
42 (lbrace rbrace lparen rparen lbracket rbracket dot semicolon comma <
43 > <= >= == != === !== + - * % ++ -- << >> >>> & bor ^ ! ~ && or ?
44 colon = += -= *= %= <<= >>= >>>= &= bor= ^= / /=
45
46 break else new var case finally return void catch for switch while
47 continue function this with default if throw delete in try do
48 instanceof typeof null true false
49
50 Identifier StringLiteral NumericLiteral RegexpLiteral)
51
52
53 (Program (SourceElements) -> $1
54 (*eoi*) -> *eof-object*)
55
56 ;;
57 ;; Verily, here we define statements. Expressions are defined
58 ;; afterwards.
59 ;;
60
61 (SourceElement (Statement) -> $1
62 (FunctionDeclaration) -> $1)
63
64 (FunctionDeclaration (function Identifier lparen rparen lbrace FunctionBody rbrace) -> `(var (,$2 (lambda () ,$6)))
65 (function Identifier lparen FormalParameterList rparen lbrace FunctionBody rbrace) -> `(var (,$2 (lambda ,$4 ,$7))))
66 (FunctionExpression (function lparen rparen lbrace FunctionBody rbrace) -> `(lambda () ,$5)
67 (function Identifier lparen rparen lbrace FunctionBody rbrace) -> `(lambda () ,$6)
68 (function lparen FormalParameterList rparen lbrace FunctionBody rbrace) -> `(lambda ,$3 ,$6)
69 (function Identifier lparen FormalParameterList rparen lbrace FunctionBody rbrace) -> `(lambda ,$4 ,$7))
70 (FormalParameterList (Identifier) -> `(,$1)
71 (FormalParameterList comma Identifier) -> `(,@$1 ,$3))
72 (SourceElements (SourceElement) -> $1
73 (SourceElements SourceElement) -> (if (and (pair? $1) (eq? (car $1) 'begin))
74 `(begin ,@(cdr $1) ,$2)
75 `(begin ,$1 ,$2)))
76 (FunctionBody (SourceElements) -> $1)
77
78 (Statement (Block) -> $1
79 (VariableStatement) -> $1
80 (EmptyStatement) -> $1
81 (ExpressionStatement) -> $1
82 (IfStatement) -> $1
83 (IterationStatement) -> $1
84 (ContinueStatement) -> $1
85 (BreakStatement) -> $1
86 (ReturnStatement) -> $1
87 (WithStatement) -> $1
88 (LabelledStatement) -> $1
89 (SwitchStatement) -> $1
90 (ThrowStatement) -> $1
91 (TryStatement) -> $1)
92
93 (Block (lbrace StatementList rbrace) -> `(block ,$2))
94 (StatementList (Statement) -> $1
95 (StatementList Statement) -> (if (and (pair? $1) (eq? (car $1) 'begin))
96 `(begin ,@(cdr $1) ,$2)
97 `(begin ,$1 ,$2)))
98
99 (VariableStatement (var VariableDeclarationList) -> `(var ,@$2))
100 (VariableDeclarationList (VariableDeclaration) -> `(,$1)
101 (VariableDeclarationList comma VariableDeclaration) -> `(,@$1 ,$2))
102 (VariableDeclarationListNoIn (VariableDeclarationNoIn) -> `(,$1)
103 (VariableDeclarationListNoIn comma VariableDeclarationNoIn) -> `(,@$1 ,$2))
104 (VariableDeclaration (Identifier) -> `(,$1)
105 (Identifier Initialiser) -> `(,$1 ,$2))
106 (VariableDeclarationNoIn (Identifier) -> `(,$1)
107 (Identifier Initialiser) -> `(,$1 ,$2))
108 (Initialiser (= AssignmentExpression) -> $2)
109 (InitialiserNoIn (= AssignmentExpressionNoIn) -> $2)
110
111 (EmptyStatement (semicolon) -> '(begin))
112
113 (ExpressionStatement (Expression semicolon) -> $1)
114
115 (IfStatement (if lparen Expression rparen Statement else Statement) -> `(if ,$3 ,$5 ,$7)
116 (if lparen Expression rparen Statement) -> `(if ,$3 ,$5))
117
118 (IterationStatement (do Statement while lparen Expression rparen semicolon) -> `(do ,$2 ,$5)
119
120 (while lparen Expression rparen Statement) -> `(while ,$3 ,$5)
121
122 (for lparen semicolon semicolon rparen Statement) -> `(for #f #f #f ,$6)
123 (for lparen semicolon semicolon Expression rparen Statement) -> `(for #f #f ,$5 ,$7)
124 (for lparen semicolon Expression semicolon rparen Statement) -> `(for #f ,$4 #f ,$7)
125 (for lparen semicolon Expression semicolon Expression rparen Statement) -> `(for #f ,$4 ,$6 ,$8)
126
127 (for lparen ExpressionNoIn semicolon semicolon rparen Statement) -> `(for ,$3 #f #f ,$7)
128 (for lparen ExpressionNoIn semicolon semicolon Expression rparen Statement) -> `(for ,$3 #f ,$6 ,$8)
129 (for lparen ExpressionNoIn semicolon Expression semicolon rparen Statement) -> `(for ,$3 ,$5 #f ,$8)
130 (for lparen ExpressionNoIn semicolon Expression semicolon Expression rparen Statement) -> `(for ,$3 ,$5 ,$7 ,$9)
131
132 (for lparen var VariableDeclarationListNoIn semicolon semicolon rparen Statement) -> `(for (var ,@$4) #f #f ,$8)
133 (for lparen var VariableDeclarationListNoIn semicolon semicolon Expression rparen Statement) -> `(for (var ,@$4) #f ,$7 ,$9)
134 (for lparen var VariableDeclarationListNoIn semicolon Expression semicolon rparen Statement) -> `(for (var ,@$4) ,$6 #f ,$9)
135 (for lparen var VariableDeclarationListNoIn semicolon Expression semicolon Expression rparen Statement) -> `(for (var ,@$4) ,$6 ,$8 ,$10)
136
137 (for lparen LeftHandSideExpression in Expression rparen Statement) -> `(for-in ,$3 ,$5 ,$7)
138 (for lparen var VariableDeclarationNoIn in Expression rparen Statement) -> `(begin (var ,$4) (for-in (ref ,@$4) ,$6 ,$8)))
139
140 (ContinueStatement (continue Identifier semicolon) -> `(continue ,$2)
141 (continue semicolon) -> `(continue))
142
143 (BreakStatement (break Identifier semicolon) -> `(break ,$2)
144 (break semicolon) -> `(break))
145
146 (ReturnStatement (return Expression semicolon) -> `(return ,$2)
147 (return semicolon) -> `(return))
148
149 (WithStatement (with lparen Expression rparen Statement) -> `(with ,$3 ,$5))
150
151 (SwitchStatement (switch lparen Expression rparen CaseBlock) -> `(switch ,$3 ,@$5))
152 (CaseBlock (lbrace rbrace) -> '()
153 (lbrace CaseClauses rbrace) -> $2
154 (lbrace CaseClauses DefaultClause rbrace) -> `(,@$2 ,@$3)
155 (lbrace DefaultClause rbrace) -> `(,$2)
156 (lbrace DefaultClause CaseClauses rbrace) -> `(,@$2 ,@$3))
157 (CaseClauses (CaseClause) -> `(,$1)
158 (CaseClauses CaseClause) -> `(,@$1 ,$2))
159 (CaseClause (case Expression colon) -> `(case ,$2)
160 (case Expression colon StatementList) -> `(case ,$2 ,$4))
161 (DefaultClause (default colon) -> `(default)
162 (default colon StatementList) -> `(default ,$3))
163
164 (LabelledStatement (Identifier colon Statement) -> `(label ,$1 ,$3))
165
166 (ThrowStatement (throw Expression semicolon) -> `(throw ,$2))
167
168 (TryStatement (try Block Catch) -> `(try ,$2 ,$3 #f)
169 (try Block Finally) -> `(try ,$2 #f ,$3)
170 (try Block Catch Finally) -> `(try ,$2 ,$3 ,$4))
171 (Catch (catch lparen Identifier rparen Block) -> `(catch ,$3 ,$5))
172 (Finally (finally Block) -> `(finally ,$2))
173
174 ;;
175 ;; As promised, expressions. We build up to Expression bottom-up, so
176 ;; as to get operator precedence right.
177 ;;
178
179 (PrimaryExpression (this) -> 'this
180 (null) -> 'null
181 (true) -> 'true
182 (false) -> 'false
183 (Identifier) -> `(ref ,$1)
184 (StringLiteral) -> `(string ,$1)
185 (RegexpLiteral) -> `(regexp ,$1)
186 (NumericLiteral) -> `(number ,$1)
187 (ArrayLiteral) -> $1
188 (ObjectLiteral) -> $1
189 (lparen Expression rparen) -> $2)
190
191 (ArrayLiteral (lbracket rbracket) -> '(array)
192 (lbracket Elision rbracket) -> '(array ,@$2)
193 (lbracket ElementList rbracket) -> `(array ,@$2)
194 (lbracket ElementList comma rbracket) -> `(array ,@$2)
195 (lbracket ElementList comma Elision rbracket) -> `(array ,@$2))
196 (ElementList (AssignmentExpression) -> `(,$1)
197 (Elision AssignmentExpression) -> `(,@$1 ,$2)
198 (ElementList comma AssignmentExpression) -> `(,@$1 ,$3)
199 (ElementList comma Elision AssignmentExpression) -> `(,@$1 ,@$3 ,$4))
200 (Elision (comma) -> '((number 0))
201 (Elision comma) -> `(,@$1 (number 0)))
202
203 (ObjectLiteral (lbrace rbrace) -> `(object)
204 (lbrace PropertyNameAndValueList rbrace) -> `(object ,@$2))
205 (PropertyNameAndValueList (PropertyName colon AssignmentExpression) -> `((,$1 ,$3))
206 (PropertyNameAndValueList comma PropertyName colon AssignmentExpression) -> `(,@$1 (,$3 ,$5)))
207 (PropertyName (Identifier) -> $1
208 (StringLiteral) -> (string->symbol $1)
209 (NumericLiteral) -> $1)
210
211 (MemberExpression (PrimaryExpression) -> $1
212 (FunctionExpression) -> $1
213 (MemberExpression lbracket Expression rbracket) -> `(aref ,$1 ,$3)
214 (MemberExpression dot Identifier) -> `(pref ,$1 ,$3)
215 (new MemberExpression Arguments) -> `(new ,$2 ,$3))
216
217 (NewExpression (MemberExpression) -> $1
218 (new NewExpression) -> `(new ,$2 ()))
219
220 (CallExpression (MemberExpression Arguments) -> `(call ,$1 ,$2)
221 (CallExpression Arguments) -> `(call ,$1 ,$2)
222 (CallExpression lbracket Expression rbracket) -> `(aref ,$1 ,$3)
223 (CallExpression dot Identifier) -> `(pref ,$1 ,$3))
224 (Arguments (lparen rparen) -> '()
225 (lparen ArgumentList rparen) -> $2)
226 (ArgumentList (AssignmentExpression) -> `(,$1)
227 (ArgumentList comma AssignmentExpression) -> `(,@$1 ,$3))
228
229 (LeftHandSideExpression (NewExpression) -> $1
230 (CallExpression) -> $1)
231
232 (PostfixExpression (LeftHandSideExpression) -> $1
233 (LeftHandSideExpression ++) -> `(postinc ,$1)
234 (LeftHandSideExpression --) -> `(postdec ,$1))
235
236 (UnaryExpression (PostfixExpression) -> $1
237 (delete UnaryExpression) -> `(delete ,$2)
238 (void UnaryExpression) -> `(void ,$2)
239 (typeof UnaryExpression) -> `(typeof ,$2)
240 (++ UnaryExpression) -> `(preinc ,$2)
241 (-- UnaryExpression) -> `(predec ,$2)
242 (+ UnaryExpression) -> `(+ ,$2)
243 (- UnaryExpression) -> `(- ,$2)
244 (~ UnaryExpression) -> `(~ ,$2)
245 (! UnaryExpression) -> `(! ,$2))
246
247 (MultiplicativeExpression (UnaryExpression) -> $1
248 (MultiplicativeExpression * UnaryExpression) -> `(* ,$1 ,$3)
249 (MultiplicativeExpression / UnaryExpression) -> `(/ ,$1 ,$3)
250 (MultiplicativeExpression % UnaryExpression) -> `(% ,$1 ,$3))
251
252 (AdditiveExpression (MultiplicativeExpression) -> $1
253 (AdditiveExpression + MultiplicativeExpression) -> `(+ ,$1 ,$3)
254 (AdditiveExpression - MultiplicativeExpression) -> `(- ,$1 ,$3))
255
256 (ShiftExpression (AdditiveExpression) -> $1
257 (ShiftExpression << MultiplicativeExpression) -> `(<< ,$1 ,$3)
258 (ShiftExpression >> MultiplicativeExpression) -> `(>> ,$1 ,$3)
259 (ShiftExpression >>> MultiplicativeExpression) -> `(>>> ,$1 ,$3))
260
261 (RelationalExpression (ShiftExpression) -> $1
262 (RelationalExpression < ShiftExpression) -> `(< ,$1 ,$3)
263 (RelationalExpression > ShiftExpression) -> `(> ,$1 ,$3)
264 (RelationalExpression <= ShiftExpression) -> `(<= ,$1 ,$3)
265 (RelationalExpression >= ShiftExpression) -> `(>= ,$1 ,$3)
266 (RelationalExpression instanceof ShiftExpression) -> `(instanceof ,$1 ,$3)
267 (RelationalExpression in ShiftExpression) -> `(in ,$1 ,$3))
268
269 (RelationalExpressionNoIn (ShiftExpression) -> $1
270 (RelationalExpressionNoIn < ShiftExpression) -> `(< ,$1 ,$3)
271 (RelationalExpressionNoIn > ShiftExpression) -> `(> ,$1 ,$3)
272 (RelationalExpressionNoIn <= ShiftExpression) -> `(<= ,$1 ,$3)
273 (RelationalExpressionNoIn >= ShiftExpression) -> `(>= ,$1 ,$3)
274 (RelationalExpressionNoIn instanceof ShiftExpression) -> `(instanceof ,$1 ,$3))
275
276 (EqualityExpression (RelationalExpression) -> $1
277 (EqualityExpression == RelationalExpression) -> `(== ,$1 ,$3)
278 (EqualityExpression != RelationalExpression) -> `(!= ,$1 ,$3)
279 (EqualityExpression === RelationalExpression) -> `(=== ,$1 ,$3)
280 (EqualityExpression !== RelationalExpression) -> `(!== ,$1 ,$3))
281
282 (EqualityExpressionNoIn (RelationalExpressionNoIn) -> $1
283 (EqualityExpressionNoIn == RelationalExpressionNoIn) -> `(== ,$1 ,$3)
284 (EqualityExpressionNoIn != RelationalExpressionNoIn) -> `(!= ,$1 ,$3)
285 (EqualityExpressionNoIn === RelationalExpressionNoIn) -> `(=== ,$1 ,$3)
286 (EqualityExpressionNoIn !== RelationalExpressionNoIn) -> `(!== ,$1 ,$3))
287
288 (BitwiseANDExpression (EqualityExpression) -> $1
289 (BitwiseANDExpression & EqualityExpression) -> `(& ,$1 ,$3))
290 (BitwiseANDExpressionNoIn (EqualityExpressionNoIn) -> $1
291 (BitwiseANDExpressionNoIn & EqualityExpressionNoIn) -> `(& ,$1 ,$3))
292
293 (BitwiseXORExpression (BitwiseANDExpression) -> $1
294 (BitwiseXORExpression ^ BitwiseANDExpression) -> `(^ ,$1 ,$3))
295 (BitwiseXORExpressionNoIn (BitwiseANDExpressionNoIn) -> $1
296 (BitwiseXORExpressionNoIn ^ BitwiseANDExpressionNoIn) -> `(^ ,$1 ,$3))
297
298 (BitwiseORExpression (BitwiseXORExpression) -> $1
299 (BitwiseORExpression bor BitwiseXORExpression) -> `(bor ,$1 ,$3))
300 (BitwiseORExpressionNoIn (BitwiseXORExpressionNoIn) -> $1
301 (BitwiseORExpressionNoIn bor BitwiseXORExpressionNoIn) -> `(bor ,$1 ,$3))
302
303 (LogicalANDExpression (BitwiseORExpression) -> $1
304 (LogicalANDExpression && BitwiseORExpression) -> `(and ,$1 ,$3))
305 (LogicalANDExpressionNoIn (BitwiseORExpressionNoIn) -> $1
306 (LogicalANDExpressionNoIn && BitwiseORExpressionNoIn) -> `(and ,$1 ,$3))
307
308 (LogicalORExpression (LogicalANDExpression) -> $1
309 (LogicalORExpression or LogicalANDExpression) -> `(or ,$1 ,$3))
310 (LogicalORExpressionNoIn (LogicalANDExpressionNoIn) -> $1
311 (LogicalORExpressionNoIn or LogicalANDExpressionNoIn) -> `(or ,$1 ,$3))
312
313 (ConditionalExpression (LogicalORExpression) -> $1
314 (LogicalORExpression ? AssignmentExpression colon AssignmentExpression) -> `(if ,$1 ,$3 ,$5))
315 (ConditionalExpressionNoIn (LogicalORExpressionNoIn) -> $1
316 (LogicalORExpressionNoIn ? AssignmentExpressionNoIn colon AssignmentExpressionNoIn) -> `(if ,$1 ,$3 ,$5))
317
318 (AssignmentExpression (ConditionalExpression) -> $1
319 (LeftHandSideExpression AssignmentOperator AssignmentExpression) -> `(,$2 ,$1 ,$3))
320 (AssignmentExpressionNoIn (ConditionalExpressionNoIn) -> $1
321 (LeftHandSideExpression AssignmentOperator AssignmentExpressionNoIn) -> `(,$2 ,$1 ,$3))
322 (AssignmentOperator (=) -> '=
323 (*=) -> '*=
324 (/=) -> '/=
325 (%=) -> '%=
326 (+=) -> '+=
327 (-=) -> '-=
328 (<<=) -> '<<=
329 (>>=) -> '>>=
330 (>>>=) -> '>>>=
331 (&=) -> '&=
332 (^=) -> '^=
333 (bor=) -> 'bor=)
334
335 (Expression (AssignmentExpression) -> $1
336 (Expression comma AssignmentExpression) -> `(begin ,$1 ,$3))
337 (ExpressionNoIn (AssignmentExpressionNoIn) -> $1
338 (ExpressionNoIn comma AssignmentExpressionNoIn) -> `(begin ,$1 ,$3))))