declare smobs in alloc.c
[bpt/emacs.git] / admin / grammars / js.wy
1 ;;; javascript-jv.wy -- LALR grammar for Javascript
2
3 ;; Copyright (C) 2005-2014 Free Software Foundation, Inc.
4 ;; Copyright (C) 1998-2011 Ecma International.
5
6 ;; Author: Joakim Verona
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; The grammar itself is transcribed from the ECMAScript Language
26 ;; Specification published at
27 ;;
28 ;; http://www.ecma-international.org/publications/standards/Ecma-262.htm
29 ;;
30 ;; and redistributed under the following license:
31
32 ;; Redistribution and use in source and binary forms, with or without
33 ;; modification, are permitted provided that the following conditions
34 ;; are met:
35
36 ;; 1. Redistributions of source code must retain the above copyright
37 ;; notice, this list of conditions and the following disclaimer.
38
39 ;; 2. Redistributions in binary form must reproduce the above
40 ;; copyright notice, this list of conditions and the following
41 ;; disclaimer in the documentation and/or other materials provided
42 ;; with the distribution.
43
44 ;; 3. Neither the name of the authors nor Ecma International may be
45 ;; used to endorse or promote products derived from this software
46 ;; without specific prior written permission. THIS SOFTWARE IS
47 ;; PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR
48 ;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
49 ;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 ;; ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR
51 ;; ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
53 ;; OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
54 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
55 ;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
57 ;; USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
58 ;; DAMAGE.
59
60 %package wisent-javascript-jv-wy
61 %provide semantic/wisent/js-wy
62
63 %{
64 (declare-function semantic-parse-region "semantic"
65 (start end &optional nonterminal depth returnonerror))
66 }
67
68 ;; JAVE I preferred ecmascript-mode.
69 %languagemode ecmascript-mode javascript-mode
70
71 ;; The default goal
72 %start Program
73 ;; Other Goals
74 %start FormalParameterList
75
76 ;; with the terminals stuff, I used the javascript.y names,
77 ;; but the semantic/wisent/java-tags.wy types
78 ;; when possible
79 ;; ------------------
80 ;; Operator terminals
81 ;; ------------------
82
83 ;;define-lex-string-type-analyzer gets called with the "syntax" comment
84 %type <punctuation> ;;syntax "\\(\\s.\\|\\s$\\|\\s'\\)+" matchdatatype string
85
86 %token <punctuation> ASSIGN_SYMBOL "="
87 %token <punctuation> BITWISE_AND "&"
88 %token <punctuation> BITWISE_AND_EQUALS "&="
89 %token <punctuation> BITWISE_EXCLUSIVE_OR "^"
90 %token <punctuation> BITWISE_EXCLUSIVE_OR_EQUALS "^="
91 %token <punctuation> BITWISE_OR "|"
92 %token <punctuation> BITWISE_OR_EQUALS "|="
93 %token <punctuation> BITWISE_SHIFT_LEFT "<<"
94 %token <punctuation> BITWISE_SHIFT_LEFT_EQUALS "<<="
95 %token <punctuation> BITWISE_SHIFT_RIGHT ">>"
96 %token <punctuation> BITWISE_SHIFT_RIGHT_EQUALS ">>="
97 %token <punctuation> BITWISE_SHIFT_RIGHT_ZERO_FILL ">>>"
98 %token <punctuation> BITWISE_SHIFT_RIGHT_ZERO_FILL_EQUALS ">>>="
99 %token <punctuation> NOT_EQUAL "!="
100 %token <punctuation> DIV_EQUALS "/="
101 %token <punctuation> EQUALS "=="
102 %token <punctuation> GREATER_THAN ">"
103 %token <punctuation> GT_EQUAL ">="
104 %token <punctuation> LOGICAL_AND "&&"
105 %token <punctuation> LOGICAL_OR "||"
106 %token <punctuation> LOGICAL_NOT "!!"
107 %token <punctuation> LS_EQUAL "<="
108 %token <punctuation> MINUS "-"
109 %token <punctuation> MINUS_EQUALS "-="
110 %token <punctuation> MOD "%"
111 %token <punctuation> MOD_EQUALS "%="
112 %token <punctuation> MULTIPLY "*"
113 %token <punctuation> MULTIPLY_EQUALS "*="
114 %token <punctuation> PLUS "+"
115 %token <punctuation> PLUS_EQUALS "+="
116 %token <punctuation> INCREMENT "++"
117 %token <punctuation> DECREMENT "--"
118 %token <punctuation> DIV "/"
119 %token <punctuation> COLON ":"
120 %token <punctuation> COMMA ","
121 %token <punctuation> DOT "."
122 %token <punctuation> LESS_THAN "<"
123 %token <punctuation> LINE_TERMINATOR "\n"
124 %token <punctuation> SEMICOLON ";"
125 %token <punctuation> ONES_COMPLIMENT "~"
126
127
128 ;; -----------------------------
129 ;; Block & Parenthesis terminals
130 ;; -----------------------------
131 %type <block> ;;syntax "\\s(\\|\\s)" matchdatatype block
132 %token <block> PAREN_BLOCK "(OPEN_PARENTHESIS CLOSE_PARENTHESIS)"
133 %token <block> BRACE_BLOCK "(START_BLOCK END_BLOCK)"
134 %token <block> BRACK_BLOCK "(OPEN_SQ_BRACKETS CLOSE_SQ_BRACKETS)"
135
136 %token <open-paren> OPEN_PARENTHESIS "("
137 %token <close-paren> CLOSE_PARENTHESIS ")"
138
139 %token <open-paren> START_BLOCK "{"
140 %token <close-paren> END_BLOCK "}"
141
142 %token <open-paren> OPEN_SQ_BRACKETS "["
143 %token <close-paren> CLOSE_SQ_BRACKETS "]"
144
145
146 ;; -----------------
147 ;; Keyword terminals
148 ;; -----------------
149
150 ;; Generate a keyword analyzer
151 %type <keyword> ;;syntax "\\(\\sw\\|\\s_\\)+" matchdatatype keyword
152
153 %keyword IF "if"
154 %put IF summary
155 "if (<expr>) <stmt> [else <stmt>] (jv)"
156
157 %keyword BREAK "break"
158 %put BREAK summary
159 "break [<label>] ;"
160
161 %keyword CONTINUE "continue"
162 %put CONTINUE summary
163 "continue [<label>] ;"
164
165 %keyword ELSE "else"
166 %put ELSE summary
167 "if (<expr>) <stmt> else <stmt>"
168
169
170 %keyword FOR "for"
171 %put FOR summary
172 "for ([<init-expr>]; [<expr>]; [<update-expr>]) <stmt>"
173
174
175 %keyword FUNCTION "function"
176 %put FUNCTION summary
177 "function declaration blah blah"
178
179 %keyword THIS "this"
180 %put THIS summary
181 "this"
182
183
184 %keyword RETURN "return"
185 %put RETURN summary
186 "return [<expr>] ;"
187
188 %keyword WHILE "while"
189 %put WHILE summary
190 "while (<expr>) <stmt> | do <stmt> while (<expr>);"
191
192 %keyword VOID_SYMBOL "void"
193 %put VOID_SYMBOL summary
194 "Method return type: void <name> ..."
195
196
197
198 %keyword NEW "new"
199 %put NEW summary
200 "new <objecttype> - Creates a new object."
201
202 %keyword DELETE "delete"
203 %put DELETE summary
204 "delete(<objectreference>) - Deletes the object."
205
206 %keyword VAR "var"
207 %put VAR summary
208 "var <variablename> [= value];"
209
210 %keyword WITH "with"
211 %put WITH summary
212 "with "
213
214 %keyword TYPEOF "typeof"
215 %put TYPEOF summary
216 "typeof "
217
218 %keyword IN "in"
219 %put IN summary
220 "in something"
221
222
223 ;; -----------------
224 ;; Literal terminals
225 ;; -----------------
226
227 ;;the .y file uses VARIABLE as IDENTIFIER, which seems a bit evil
228 ;; it think the normal .wy convention is better than this
229 %type <symbol> ;;syntax "\\(\\sw\\|\\s_\\)+"
230 %token <symbol> VARIABLE
231
232 %type <string> ;;syntax "\\s\"" matchdatatype sexp
233 %token <string> STRING
234
235 %type <number> ;;syntax semantic-lex-number-expression
236 %token <number> NUMBER
237
238
239 %token FALSE
240 %token TRUE
241 %token QUERY
242
243
244 %token NULL_TOKEN
245
246 ;;%token UNDEFINED_TOKEN
247 ;;%token INFINITY
248
249 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
250 ;; associativity and stuff
251 %left PLUS MINUS
252 %left MULTIPLY DIV MOD
253
254 %nonassoc FALSE
255 %nonassoc HIGHER_THAN_FALSE
256 %nonassoc ELSE
257 %nonassoc LOWER_THAN_CLOSE_PARENTHESIS
258 %nonassoc CLOSE_PARENTHESIS
259
260 %%
261
262 Program : SourceElement
263 ;
264
265 SourceElement : Statement
266 | FunctionDeclaration
267 ;
268
269 Statement : Block
270 | VariableStatement
271 | EmptyStatement
272 | ExpressionStatement
273 | IfStatement
274 | IterationExpression
275 | ContinueStatement
276 | BreakStatement
277 | ReturnStatement
278 | WithStatement
279 ;
280
281 FunctionDeclaration : FUNCTION VARIABLE FormalParameterListBlock Block
282 (FUNCTION-TAG $2 nil $3)
283 ;
284
285 FormalParameterListBlock : PAREN_BLOCK
286 (EXPANDFULL $1 FormalParameterList)
287 ;
288
289 FormalParameterList: OPEN_PARENTHESIS
290 ()
291 | VARIABLE
292 (VARIABLE-TAG $1 nil nil)
293 | CLOSE_PARENTHESIS
294 ()
295 | COMMA
296 ()
297 ;
298
299 StatementList : Statement
300 | StatementList Statement
301 ;
302
303 Block : BRACE_BLOCK
304 ;; If you want to parse the body of the function
305 ;; ( EXPANDFULL $1 BlockExpand )
306 ;
307
308 BlockExpand: START_BLOCK StatementList END_BLOCK
309 | START_BLOCK END_BLOCK
310 ;
311
312 VariableStatement : VAR VariableDeclarationList SEMICOLON
313 (VARIABLE-TAG $2 nil nil)
314 ;
315
316 VariableDeclarationList : VariableDeclaration
317 (list $1)
318 | VariableDeclarationList COMMA VariableDeclaration
319 (append $1 (list $3))
320 ;
321
322 VariableDeclaration : VARIABLE
323 (append (list $1 nil) $region)
324 | VARIABLE Initializer
325 (append (cons $1 $2) $region)
326 ;
327
328 Initializer : ASSIGN_SYMBOL AssignmentExpression
329 (list $2)
330 ;
331
332 EmptyStatement : SEMICOLON
333 ;
334
335 ExpressionStatement : Expression SEMICOLON
336 ;
337
338 IfStatement : IF OPEN_PARENTHESIS Expression CLOSE_PARENTHESIS Statement %prec HIGHER_THAN_FALSE
339 | IF OPEN_PARENTHESIS Expression CLOSE_PARENTHESIS Statement ELSE Statement
340 | IF OPEN_PARENTHESIS FALSE CLOSE_PARENTHESIS Statement
341 | IF OPEN_PARENTHESIS LeftHandSideExpression AssignmentOperator AssignmentExpression CLOSE_PARENTHESIS Statement
342 ;
343
344 IterationExpression : WHILE OPEN_PARENTHESIS Expression CLOSE_PARENTHESIS Statement %prec HIGHER_THAN_FALSE
345 | WHILE OPEN_PARENTHESIS FALSE CLOSE_PARENTHESIS Statement
346 | WHILE OPEN_PARENTHESIS LeftHandSideExpression AssignmentOperator AssignmentExpression CLOSE_PARENTHESIS Statement
347 | FOR OPEN_PARENTHESIS OptionalExpression SEMICOLON OptionalExpression SEMICOLON OptionalExpression CLOSE_PARENTHESIS Statement
348 | FOR OPEN_PARENTHESIS VAR VariableDeclarationList SEMICOLON OptionalExpression SEMICOLON OptionalExpression CLOSE_PARENTHESIS Statement
349 | FOR OPEN_PARENTHESIS LeftHandSideExpression IN Expression CLOSE_PARENTHESIS Statement
350 | FOR OPEN_PARENTHESIS VAR VARIABLE OptionalInitializer IN Expression CLOSE_PARENTHESIS Statement
351 ;
352
353 ContinueStatement : CONTINUE SEMICOLON
354 ;
355
356 ;;JAVE break needs labels
357 BreakStatement : BREAK SEMICOLON
358 ;; | BREAK identifier SEMICOLON
359 ;
360
361 ReturnStatement : RETURN Expression SEMICOLON
362 | RETURN SEMICOLON
363 ;
364
365 WithStatement : WITH OPEN_PARENTHESIS Expression CLOSE_PARENTHESIS Statement
366 ;
367
368 OptionalInitializer : Initializer
369 |
370 ;
371
372 PrimaryExpression : THIS
373 | VARIABLE
374 | NUMBER
375 | STRING
376 | NULL_TOKEN
377 | TRUE
378 | FALSE
379 | OPEN_PARENTHESIS Expression CLOSE_PARENTHESIS
380 ;
381
382 MemberExpression : PrimaryExpression
383 | MemberExpression OPEN_SQ_BRACKETS Expression CLOSE_SQ_BRACKETS
384 | MemberExpression DOT VARIABLE
385 | NEW MemberExpression Arguments
386 ;
387
388 NewExpression : MemberExpression
389 | NEW NewExpression
390 ;
391
392 CallExpression : MemberExpression Arguments
393 | CallExpression Arguments
394 | CallExpression OPEN_SQ_BRACKETS Expression CLOSE_SQ_BRACKETS
395 | CallExpression DOT VARIABLE
396 ;
397
398 Arguments : OPEN_PARENTHESIS CLOSE_PARENTHESIS
399 | OPEN_PARENTHESIS ArgumentList CLOSE_PARENTHESIS
400 ;
401
402 ArgumentList : AssignmentExpression
403 | ArgumentList COMMA AssignmentExpression
404 ;
405
406 LeftHandSideExpression : NewExpression
407 | CallExpression
408 ;
409
410 PostfixExpression : LeftHandSideExpression
411 | LeftHandSideExpression INCREMENT
412 | LeftHandSideExpression DECREMENT
413 ;
414
415 UnaryExpression : PostfixExpression
416 | DELETE UnaryExpression
417 | VOID_SYMBOL UnaryExpression
418 | TYPEOF UnaryExpression
419 | INCREMENT UnaryExpression
420 | DECREMENT UnaryExpression
421 | PLUS UnaryExpression
422 | MINUS UnaryExpression
423 | ONES_COMPLIMENT UnaryExpression
424 | LOGICAL_NOT UnaryExpression
425 ;
426
427 MultiplicativeExpression : UnaryExpression
428 | MultiplicativeExpression MULTIPLY UnaryExpression
429 | MultiplicativeExpression DIV UnaryExpression
430 | MultiplicativeExpression MOD UnaryExpression
431 ;
432
433 AdditiveExpression : MultiplicativeExpression
434 | AdditiveExpression PLUS MultiplicativeExpression
435 | AdditiveExpression MINUS MultiplicativeExpression
436 ;
437
438 ShiftExpression : AdditiveExpression
439 | ShiftExpression BITWISE_SHIFT_LEFT AdditiveExpression
440 | ShiftExpression BITWISE_SHIFT_RIGHT AdditiveExpression
441 | ShiftExpression BITWISE_SHIFT_RIGHT_ZERO_FILL AdditiveExpression
442 ;
443
444 RelationalExpression : ShiftExpression
445 | RelationalExpression LESS_THAN ShiftExpression
446 | RelationalExpression GREATER_THAN ShiftExpression
447 | RelationalExpression LS_EQUAL ShiftExpression
448 | RelationalExpression GT_EQUAL ShiftExpression
449 ;
450
451 EqualityExpression : RelationalExpression
452 | EqualityExpression EQUALS RelationalExpression
453 | EqualityExpression NOT_EQUAL RelationalExpression
454 ;
455
456 BitwiseANDExpression : EqualityExpression
457 | BitwiseANDExpression BITWISE_AND EqualityExpression
458 ;
459
460 BitwiseXORExpression : BitwiseANDExpression
461 | BitwiseXORExpression BITWISE_EXCLUSIVE_OR BitwiseANDExpression
462 ;
463
464 BitwiseORExpression : BitwiseXORExpression
465 | BitwiseORExpression BITWISE_OR BitwiseXORExpression
466 ;
467
468 LogicalANDExpression : BitwiseORExpression
469 | LogicalANDExpression LOGICAL_AND BitwiseORExpression
470 ;
471
472 LogicalORExpression : LogicalANDExpression
473 | LogicalORExpression LOGICAL_OR LogicalANDExpression
474 ;
475
476 ConditionalExpression : LogicalORExpression
477 | LogicalORExpression QUERY AssignmentExpression COLON AssignmentExpression
478 ;
479
480 AssignmentExpression : ConditionalExpression
481 | LeftHandSideExpression AssignmentOperator AssignmentExpression %prec LOWER_THAN_CLOSE_PARENTHESIS
482 ;
483
484 AssignmentOperator : ASSIGN_SYMBOL
485 | MULTIPLY_EQUALS
486 | DIV_EQUALS
487 | MOD_EQUALS
488 | PLUS_EQUALS
489 | MINUS_EQUALS
490 | BITWISE_SHIFT_LEFT_EQUALS
491 | BITWISE_SHIFT_RIGHT_EQUALS
492 | BITWISE_SHIFT_RIGHT_ZERO_FILL_EQUALS
493 | BITWISE_AND_EQUALS
494 | BITWISE_EXCLUSIVE_OR_EQUALS
495 | BITWISE_OR_EQUALS
496 ;
497
498 Expression : AssignmentExpression
499 | Expression COMMA AssignmentExpression
500 ;
501
502 OptionalExpression : Expression
503 |
504 ;
505
506 %%
507
508 ;;here something like:
509 ;;(define-lex wisent-java-tags-lexer
510 ;; should go
511 (define-lex javascript-lexer-jv
512 "javascript thingy"
513 ;;std stuff
514 semantic-lex-ignore-whitespace
515 semantic-lex-ignore-newline
516 semantic-lex-ignore-comments
517
518 ;;stuff generated from the wy file(one for each "type" declaration)
519 wisent-javascript-jv-wy--<number>-regexp-analyzer
520 wisent-javascript-jv-wy--<string>-sexp-analyzer
521
522 wisent-javascript-jv-wy--<keyword>-keyword-analyzer
523
524 wisent-javascript-jv-wy--<symbol>-regexp-analyzer
525 wisent-javascript-jv-wy--<punctuation>-string-analyzer
526 wisent-javascript-jv-wy--<block>-block-analyzer
527
528
529 ;;;;more std stuff
530 semantic-lex-default-action
531 )
532
533 ;;; javascript-jv.wy ends here