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