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