summaryrefslogtreecommitdiff
path: root/module/language/ecmascript/parse.scm
blob: ce731a7364017c0898c2db52ab2a6592ba9a0f84 (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
;;; ECMAScript for Guile

;; Copyright (C) 2009 Free Software Foundation, Inc.

;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;; 
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

;;; Code:

(define-module (language ecmascript parse)
  #:use-module (language ecmascript parse-lalr)
  #:use-module (language ecmascript tokenize)
  #:export (read-ecmascript read-ecmascript/1 parse-ecmascript))

(define (syntax-error message . args)
  (apply throw 'SyntaxError message args))

(define (read-ecmascript port)
  (parse-ecmascript (make-tokenizer port) syntax-error))

(define (read-ecmascript/1 port)
  (parse-ecmascript (make-tokenizer/1 port) syntax-error))

(define *eof-object*
  (call-with-input-string "" read-char))

(define parse-ecmascript
  (lalr-parser
   ;; terminal (i.e. input) token types
   (lbrace rbrace lparen rparen lbracket rbracket dot semicolon comma <
    > <= >= == != === !== + - * % ++ -- << >> >>> & bor ^ ! ~ && or ? 
    colon = += -= *= %= <<= >>= >>>= &= bor= ^= / /=

    break else new var case finally return void catch for switch while
    continue function this with default if throw delete in try do
    instanceof typeof null true false

    Identifier StringLiteral NumericLiteral RegexpLiteral)


   (Program (SourceElements) -> $1
            (*eoi*) -> *eof-object*)

   ;;
   ;; Verily, here we define statements. Expressions are defined
   ;; afterwards.
   ;;

   (SourceElement (Statement) -> $1
                  (FunctionDeclaration) -> $1)

   (FunctionDeclaration (function Identifier lparen rparen lbrace FunctionBody rbrace) -> `(var (,$2 (lambda () ,$6)))
                        (function Identifier lparen FormalParameterList rparen lbrace FunctionBody rbrace) -> `(var (,$2 (lambda ,$4 ,$7))))
   (FunctionExpression (function lparen rparen lbrace FunctionBody rbrace) -> `(lambda () ,$5)
                       (function Identifier lparen rparen lbrace FunctionBody rbrace) -> `(lambda () ,$6)
                       (function lparen FormalParameterList rparen lbrace FunctionBody rbrace) -> `(lambda ,$3 ,$6)
                       (function Identifier lparen FormalParameterList rparen lbrace FunctionBody rbrace) -> `(lambda ,$4 ,$7))
   (FormalParameterList (Identifier) -> `(,$1)
                        (FormalParameterList comma Identifier) -> `(,@$1 ,$3))
   (SourceElements (SourceElement) -> $1
                   (SourceElements SourceElement) -> (if (and (pair? $1) (eq? (car $1) 'begin))
                                                         `(begin ,@(cdr $1) ,$2)
                                                         `(begin ,$1 ,$2)))
   (FunctionBody (SourceElements) -> $1)

   (Statement (Block) -> $1
              (VariableStatement) -> $1
              (EmptyStatement) -> $1
              (ExpressionStatement) -> $1
              (IfStatement) -> $1
              (IterationStatement) -> $1
              (ContinueStatement) -> $1
              (BreakStatement) -> $1
              (ReturnStatement) -> $1
              (WithStatement) -> $1
              (LabelledStatement) -> $1
              (SwitchStatement) -> $1
              (ThrowStatement) -> $1
              (TryStatement) -> $1)

   (Block (lbrace StatementList rbrace) -> `(block ,$2))
   (StatementList (Statement) -> $1
                  (StatementList Statement) -> (if (and (pair? $1) (eq? (car $1) 'begin))
                                                   `(begin ,@(cdr $1) ,$2)
                                                   `(begin ,$1 ,$2)))

   (VariableStatement (var VariableDeclarationList) -> `(var ,@$2))
   (VariableDeclarationList (VariableDeclaration) -> `(,$1)
                            (VariableDeclarationList comma VariableDeclaration) -> `(,@$1 ,$2))
   (VariableDeclarationListNoIn (VariableDeclarationNoIn) -> `(,$1)
                                (VariableDeclarationListNoIn comma VariableDeclarationNoIn) -> `(,@$1 ,$2))
   (VariableDeclaration (Identifier) -> `(,$1)
                        (Identifier Initialiser) -> `(,$1 ,$2))
   (VariableDeclarationNoIn (Identifier) -> `(,$1)
                            (Identifier Initialiser) -> `(,$1 ,$2))
   (Initialiser (= AssignmentExpression) -> $2)
   (InitialiserNoIn (= AssignmentExpressionNoIn) -> $2)

   (EmptyStatement (semicolon) -> '(begin))

   (ExpressionStatement (Expression semicolon) -> $1)

   (IfStatement (if lparen Expression rparen Statement else Statement) -> `(if ,$3 ,$5 ,$7)
                (if lparen Expression rparen Statement) -> `(if ,$3 ,$5))
   
   (IterationStatement (do Statement while lparen Expression rparen semicolon) -> `(do ,$2 ,$5)

                       (while lparen Expression rparen Statement) -> `(while ,$3 ,$5)

                       (for lparen semicolon semicolon rparen Statement) -> `(for #f #f #f ,$6)
                       (for lparen semicolon semicolon Expression rparen Statement) -> `(for #f #f ,$5 ,$7)
                       (for lparen semicolon Expression semicolon rparen Statement) -> `(for #f ,$4 #f ,$7)
                       (for lparen semicolon Expression semicolon Expression rparen Statement) -> `(for #f ,$4 ,$6 ,$8)

                       (for lparen ExpressionNoIn semicolon semicolon rparen Statement) -> `(for ,$3 #f #f ,$7)
                       (for lparen ExpressionNoIn semicolon semicolon Expression rparen Statement) -> `(for ,$3 #f ,$6 ,$8)
                       (for lparen ExpressionNoIn semicolon Expression semicolon rparen Statement) -> `(for ,$3 ,$5 #f ,$8)
                       (for lparen ExpressionNoIn semicolon Expression semicolon Expression rparen Statement) -> `(for ,$3 ,$5 ,$7 ,$9)

                       (for lparen var VariableDeclarationListNoIn semicolon semicolon rparen Statement) -> `(for (var ,@$4) #f #f ,$8)
                       (for lparen var VariableDeclarationListNoIn semicolon semicolon Expression rparen Statement) -> `(for (var ,@$4) #f ,$7 ,$9)
                       (for lparen var VariableDeclarationListNoIn semicolon Expression semicolon rparen Statement) -> `(for (var ,@$4) ,$6 #f ,$9)
                       (for lparen var VariableDeclarationListNoIn semicolon Expression semicolon Expression rparen Statement) -> `(for (var ,@$4) ,$6 ,$8 ,$10)

                       (for lparen LeftHandSideExpression in Expression rparen Statement) -> `(for-in ,$3 ,$5 ,$7)
                       (for lparen var VariableDeclarationNoIn in Expression rparen Statement) -> `(begin (var ,$4) (for-in (ref ,@$4) ,$6 ,$8)))

   (ContinueStatement (continue Identifier semicolon) -> `(continue ,$2)
                      (continue semicolon) -> `(continue))

   (BreakStatement (break Identifier semicolon) -> `(break ,$2)
                   (break semicolon) -> `(break))

   (ReturnStatement (return Expression semicolon) -> `(return ,$2)
                    (return semicolon) -> `(return))

   (WithStatement (with lparen Expression rparen Statement) -> `(with ,$3 ,$5))

   (SwitchStatement (switch lparen Expression rparen CaseBlock) -> `(switch ,$3 ,@$5))
   (CaseBlock (lbrace rbrace) -> '()
              (lbrace CaseClauses rbrace) -> $2
              (lbrace CaseClauses DefaultClause rbrace) -> `(,@$2 ,@$3)
              (lbrace DefaultClause rbrace) -> `(,$2)
              (lbrace DefaultClause CaseClauses rbrace) -> `(,@$2 ,@$3))
   (CaseClauses (CaseClause) -> `(,$1)
                (CaseClauses CaseClause) -> `(,@$1 ,$2))
   (CaseClause (case Expression colon) -> `(case ,$2)
               (case Expression colon StatementList) -> `(case ,$2 ,$4))
   (DefaultClause (default colon) -> `(default)
                  (default colon StatementList) -> `(default ,$3))

   (LabelledStatement (Identifier colon Statement) -> `(label ,$1 ,$3))

   (ThrowStatement (throw Expression semicolon) -> `(throw ,$2))

   (TryStatement (try Block Catch) -> `(try ,$2 ,$3 #f)
                 (try Block Finally) -> `(try ,$2 #f ,$3)
                 (try Block Catch Finally) -> `(try ,$2 ,$3 ,$4))
   (Catch (catch lparen Identifier rparen Block) -> `(catch ,$3 ,$5))
   (Finally (finally Block) -> `(finally ,$2))

   ;;
   ;; As promised, expressions. We build up to Expression bottom-up, so
   ;; as to get operator precedence right.
   ;;

   (PrimaryExpression (this) -> 'this
                      (null) -> 'null
                      (true) -> 'true
                      (false) -> 'false
                      (Identifier) -> `(ref ,$1)
                      (StringLiteral) -> `(string ,$1)
                      (RegexpLiteral) -> `(regexp ,$1)
                      (NumericLiteral) -> `(number ,$1)
                      (ArrayLiteral) -> $1
                      (ObjectLiteral) -> $1
                      (lparen Expression rparen) -> $2)

   (ArrayLiteral (lbracket rbracket) -> '(array)
                 (lbracket Elision rbracket) -> '(array ,@$2)
                 (lbracket ElementList rbracket) -> `(array ,@$2)
                 (lbracket ElementList comma rbracket) -> `(array ,@$2)
                 (lbracket ElementList comma Elision rbracket) -> `(array ,@$2))
   (ElementList (AssignmentExpression) -> `(,$1)
                (Elision AssignmentExpression) -> `(,@$1 ,$2)
                (ElementList comma AssignmentExpression) -> `(,@$1 ,$3)
                (ElementList comma Elision AssignmentExpression) -> `(,@$1 ,@$3 ,$4))
   (Elision (comma) -> '((number 0))
            (Elision comma) -> `(,@$1 (number 0)))

   (ObjectLiteral (lbrace rbrace) -> `(object)
                  (lbrace PropertyNameAndValueList rbrace) -> `(object ,@$2))
   (PropertyNameAndValueList (PropertyName colon AssignmentExpression) -> `((,$1 ,$3))
                             (PropertyNameAndValueList comma PropertyName colon AssignmentExpression) -> `(,@$1 (,$3 ,$5)))
   (PropertyName (Identifier) -> $1
                 (StringLiteral) -> (string->symbol $1)
                 (NumericLiteral) -> $1)

   (MemberExpression (PrimaryExpression) -> $1
                     (FunctionExpression) -> $1
                     (MemberExpression lbracket Expression rbracket) -> `(aref ,$1 ,$3)
                     (MemberExpression dot Identifier) -> `(pref ,$1 ,$3)
                     (new MemberExpression Arguments) -> `(new ,$2 ,$3))

   (NewExpression (MemberExpression) -> $1
                  (new NewExpression) -> `(new ,$2 ()))

   (CallExpression (MemberExpression Arguments) -> `(call ,$1 ,$2)
                   (CallExpression Arguments) -> `(call ,$1 ,$2)
                   (CallExpression lbracket Expression rbracket) -> `(aref ,$1 ,$3)
                   (CallExpression dot Identifier) -> `(pref ,$1 ,$3))
   (Arguments (lparen rparen) -> '()
              (lparen ArgumentList rparen) -> $2)
   (ArgumentList (AssignmentExpression) -> `(,$1)
                 (ArgumentList comma AssignmentExpression) -> `(,@$1 ,$3))

   (LeftHandSideExpression (NewExpression) -> $1
                           (CallExpression) -> $1)

   (PostfixExpression (LeftHandSideExpression) -> $1
                      (LeftHandSideExpression ++) -> `(postinc ,$1)
                      (LeftHandSideExpression --) -> `(postdec ,$1))

   (UnaryExpression (PostfixExpression) -> $1
                    (delete UnaryExpression) -> `(delete ,$2)
                    (void UnaryExpression) -> `(void ,$2)
                    (typeof UnaryExpression) -> `(typeof ,$2)
                    (++ UnaryExpression) -> `(preinc ,$2)
                    (-- UnaryExpression) -> `(predec ,$2)
                    (+ UnaryExpression) -> `(+ ,$2)
                    (- UnaryExpression) -> `(- ,$2)
                    (~ UnaryExpression) -> `(~ ,$2)
                    (! UnaryExpression) -> `(! ,$2))

   (MultiplicativeExpression (UnaryExpression) -> $1
                             (MultiplicativeExpression * UnaryExpression) -> `(* ,$1 ,$3)
                             (MultiplicativeExpression / UnaryExpression) -> `(/ ,$1 ,$3)
                             (MultiplicativeExpression % UnaryExpression) -> `(% ,$1 ,$3))

   (AdditiveExpression (MultiplicativeExpression) -> $1
                       (AdditiveExpression + MultiplicativeExpression) -> `(+ ,$1 ,$3)
                       (AdditiveExpression - MultiplicativeExpression) -> `(- ,$1 ,$3))

   (ShiftExpression (AdditiveExpression) -> $1
                    (ShiftExpression << MultiplicativeExpression) -> `(<< ,$1 ,$3)
                    (ShiftExpression >> MultiplicativeExpression) -> `(>> ,$1 ,$3)
                    (ShiftExpression >>> MultiplicativeExpression) -> `(>>> ,$1 ,$3))

   (RelationalExpression (ShiftExpression) -> $1
                         (RelationalExpression < ShiftExpression) -> `(< ,$1 ,$3)
                         (RelationalExpression > ShiftExpression) -> `(> ,$1 ,$3)
                         (RelationalExpression <= ShiftExpression) -> `(<= ,$1 ,$3)
                         (RelationalExpression >= ShiftExpression) -> `(>= ,$1 ,$3)
                         (RelationalExpression instanceof ShiftExpression) -> `(instanceof ,$1 ,$3)
                         (RelationalExpression in ShiftExpression) -> `(in ,$1 ,$3))

   (RelationalExpressionNoIn (ShiftExpression) -> $1
                             (RelationalExpressionNoIn < ShiftExpression) -> `(< ,$1 ,$3)
                             (RelationalExpressionNoIn > ShiftExpression) -> `(> ,$1 ,$3)
                             (RelationalExpressionNoIn <= ShiftExpression) -> `(<= ,$1 ,$3)
                             (RelationalExpressionNoIn >= ShiftExpression) -> `(>= ,$1 ,$3)
                             (RelationalExpressionNoIn instanceof ShiftExpression) -> `(instanceof ,$1 ,$3))

   (EqualityExpression (RelationalExpression) -> $1
                       (EqualityExpression == RelationalExpression) -> `(== ,$1 ,$3)
                       (EqualityExpression != RelationalExpression) -> `(!= ,$1 ,$3)
                       (EqualityExpression === RelationalExpression) -> `(=== ,$1 ,$3)
                       (EqualityExpression !== RelationalExpression) -> `(!== ,$1 ,$3))

   (EqualityExpressionNoIn (RelationalExpressionNoIn) -> $1
                           (EqualityExpressionNoIn == RelationalExpressionNoIn) -> `(== ,$1 ,$3)
                           (EqualityExpressionNoIn != RelationalExpressionNoIn) -> `(!= ,$1 ,$3)
                           (EqualityExpressionNoIn === RelationalExpressionNoIn) -> `(=== ,$1 ,$3)
                           (EqualityExpressionNoIn !== RelationalExpressionNoIn) -> `(!== ,$1 ,$3))

   (BitwiseANDExpression (EqualityExpression) -> $1
                         (BitwiseANDExpression & EqualityExpression) -> `(& ,$1 ,$3))
   (BitwiseANDExpressionNoIn (EqualityExpressionNoIn) -> $1
                             (BitwiseANDExpressionNoIn & EqualityExpressionNoIn) -> `(& ,$1 ,$3))

   (BitwiseXORExpression (BitwiseANDExpression) -> $1
                         (BitwiseXORExpression ^ BitwiseANDExpression) -> `(^ ,$1 ,$3))
   (BitwiseXORExpressionNoIn (BitwiseANDExpressionNoIn) -> $1
                             (BitwiseXORExpressionNoIn ^ BitwiseANDExpressionNoIn) -> `(^ ,$1 ,$3))

   (BitwiseORExpression (BitwiseXORExpression) -> $1
                        (BitwiseORExpression bor BitwiseXORExpression) -> `(bor ,$1 ,$3))
   (BitwiseORExpressionNoIn (BitwiseXORExpressionNoIn) -> $1
                            (BitwiseORExpressionNoIn bor BitwiseXORExpressionNoIn) -> `(bor ,$1 ,$3))

   (LogicalANDExpression (BitwiseORExpression) -> $1
                         (LogicalANDExpression && BitwiseORExpression) -> `(and ,$1 ,$3))
   (LogicalANDExpressionNoIn (BitwiseORExpressionNoIn) -> $1
                             (LogicalANDExpressionNoIn && BitwiseORExpressionNoIn) -> `(and ,$1 ,$3))

   (LogicalORExpression (LogicalANDExpression) -> $1
                        (LogicalORExpression or LogicalANDExpression) -> `(or ,$1 ,$3))
   (LogicalORExpressionNoIn (LogicalANDExpressionNoIn) -> $1
                            (LogicalORExpressionNoIn or LogicalANDExpressionNoIn) -> `(or ,$1 ,$3))

   (ConditionalExpression (LogicalORExpression) -> $1
                          (LogicalORExpression ? AssignmentExpression colon AssignmentExpression) -> `(if ,$1 ,$3 ,$5))
   (ConditionalExpressionNoIn (LogicalORExpressionNoIn) -> $1
                              (LogicalORExpressionNoIn ? AssignmentExpressionNoIn colon AssignmentExpressionNoIn) -> `(if ,$1 ,$3 ,$5))

   (AssignmentExpression (ConditionalExpression) -> $1
                         (LeftHandSideExpression AssignmentOperator AssignmentExpression) -> `(,$2 ,$1 ,$3))
   (AssignmentExpressionNoIn (ConditionalExpressionNoIn) -> $1
                             (LeftHandSideExpression AssignmentOperator AssignmentExpressionNoIn) -> `(,$2 ,$1 ,$3))
   (AssignmentOperator (=) -> '=
                       (*=) -> '*=
                       (/=) -> '/=
                       (%=) -> '%=
                       (+=) -> '+=
                       (-=) -> '-=
                       (<<=) -> '<<=
                       (>>=) -> '>>=
                       (>>>=) -> '>>>=
                       (&=) -> '&=
                       (^=) -> '^=
                       (bor=) -> 'bor=)

   (Expression (AssignmentExpression) -> $1
               (Expression comma AssignmentExpression) -> `(begin ,$1 ,$3))
   (ExpressionNoIn (AssignmentExpressionNoIn) -> $1
                   (ExpressionNoIn comma AssignmentExpressionNoIn) -> `(begin ,$1 ,$3))))