Fix typos.
[emacs.git] / admin / grammars / js.wy
bloba13abf56e6ee469b72a0f5710167595a4a569a98
1 ;;; javascript-jv.wy -- LALR grammar for Javascript
3 ;; Copyright (C) 2005-2011 Free Software Foundation, Inc.
4 ;; Copyright (C) 1998-2011 Ecma International.
6 ;; Author: Joakim Verona
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
25 ;; The grammar itself is transcribed from the ECMAScript Language
26 ;; Specification published at
28 ;; http://www.ecma-international.org/publications/standards/Ecma-262.htm
30 ;; and redistributed under the following license:
32 ;; Redistribution and use in source and binary forms, with or without
33 ;; modification, are permitted provided that the following conditions
34 ;; are met:
36 ;; 1. Redistributions of source code must retain the above copyright
37 ;; notice, this list of conditions and the following disclaimer.
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.
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.
60 %package wisent-javascript-jv-wy
61 ;; JAVE I prefere ecmascript-mode
62 %languagemode ecmascript-mode javascript-mode
64 ;; The default goal
65 %start Program
66 ;; Other Goals
67 %start FormalParameterList
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 ;; ------------------
76 ;;define-lex-string-type-analyzer gets called with the "syntax" comment
77 %type <punctuation> ;;syntax "\\(\\s.\\|\\s$\\|\\s'\\)+" matchdatatype string
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 "~"
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)"
129 %token <open-paren>  OPEN_PARENTHESIS  "("
130 %token <close-paren>  CLOSE_PARENTHESIS ")"
132 %token <open-paren>  START_BLOCK       "{"
133 %token <close-paren>  END_BLOCK         "}"
135 %token <open-paren>  OPEN_SQ_BRACKETS  "["
136 %token <close-paren>  CLOSE_SQ_BRACKETS "]"
139 ;; -----------------
140 ;; Keyword terminals
141 ;; -----------------
143 ;; Generate a keyword analyzer
144 %type  <keyword> ;;syntax "\\(\\sw\\|\\s_\\)+" matchdatatype keyword
146 %keyword IF           "if"
147 %put     IF summary
148 "if (<expr>) <stmt> [else <stmt>] (jv)"
150 %keyword BREAK        "break"
151 %put     BREAK summary
152 "break [<label>] ;"
154 %keyword CONTINUE     "continue"
155 %put     CONTINUE summary
156 "continue [<label>] ;"
158 %keyword ELSE         "else"
159 %put     ELSE summary
160 "if (<expr>) <stmt> else <stmt>"
163 %keyword FOR          "for"
164 %put     FOR summary
165 "for ([<init-expr>]; [<expr>]; [<update-expr>]) <stmt>"
168 %keyword FUNCTION  "function"
169 %put     FUNCTION summary
170 "function declaration blah blah"
172 %keyword THIS         "this"
173 %put THIS summary
174 "this"
177 %keyword RETURN       "return"
178 %put     RETURN summary
179 "return [<expr>] ;"
181 %keyword WHILE        "while"
182 %put     WHILE summary
183 "while (<expr>) <stmt> | do <stmt> while (<expr>);"
185 %keyword VOID_SYMBOL         "void"
186 %put     VOID_SYMBOL summary
187 "Method return type: void <name> ..."
191 %keyword NEW          "new"
192 %put NEW summary
193 "new <objecttype> - Creates a new object."
195 %keyword DELETE "delete"
196 %put DELETE summary
197 "delete(<objectreference>) - Deletes the object."
199 %keyword VAR "var"
200 %put VAR  summary
201 "var <variablename> [= value];"
203 %keyword WITH "with"
204 %put WITH summary
205 "with "
207 %keyword TYPEOF "typeof"
208 %put TYPEOF summary
209 "typeof "
211 %keyword IN "in"
212 %put IN  summary
213 "in something"
216 ;; -----------------
217 ;; Literal terminals
218 ;; -----------------
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
225 %type  <string>      ;;syntax "\\s\"" matchdatatype sexp
226 %token <string>      STRING
228 %type  <number>      ;;syntax semantic-lex-number-expression
229 %token <number>      NUMBER
232 %token FALSE
233 %token TRUE
234 %token QUERY
237 %token NULL_TOKEN
239 ;;%token UNDEFINED_TOKEN
240 ;;%token INFINITY
242 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243 ;; associativity and stuff
244 %left PLUS MINUS
245 %left MULTIPLY DIV MOD
247 %nonassoc FALSE
248 %nonassoc HIGHER_THAN_FALSE
249 %nonassoc ELSE
250 %nonassoc LOWER_THAN_CLOSE_PARENTHESIS
251 %nonassoc CLOSE_PARENTHESIS
255 Program : SourceElement
256         ;
258 SourceElement : Statement
259               | FunctionDeclaration
260               ;
262 Statement : Block
263           | VariableStatement
264           | EmptyStatement
265           | ExpressionStatement
266           | IfStatement
267           | IterationExpression
268           | ContinueStatement
269           | BreakStatement
270           | ReturnStatement
271           | WithStatement
272           ;   
273       
274 FunctionDeclaration : FUNCTION VARIABLE FormalParameterListBlock Block
275                       (FUNCTION-TAG $2 nil $3)
276                     ;
278 FormalParameterListBlock : PAREN_BLOCK
279                            (EXPANDFULL $1 FormalParameterList)
280                         ;
282 FormalParameterList: OPEN_PARENTHESIS
283                      ()
284                    | VARIABLE
285                      (VARIABLE-TAG $1 nil nil)
286                    | CLOSE_PARENTHESIS
287                      ()
288                    | COMMA
289                      ()
290                    ;
292 StatementList : Statement
293               | StatementList Statement
294               ;
296 Block : BRACE_BLOCK
297      ;; If you want to parse the body of the function
298      ;; ( EXPANDFULL $1 BlockExpand )
299       ;
301 BlockExpand: START_BLOCK StatementList END_BLOCK
302            | START_BLOCK END_BLOCK
303            ;
305 VariableStatement : VAR VariableDeclarationList SEMICOLON
306                     (VARIABLE-TAG $2 nil nil)
307                   ;
309 VariableDeclarationList : VariableDeclaration
310                           (list $1)
311                         | VariableDeclarationList COMMA VariableDeclaration
312                           (append $1 (list $3))
313                         ;
315 VariableDeclaration : VARIABLE
316                       (append (list $1 nil) $region)
317                     | VARIABLE Initializer
318                       (append (cons $1 $2) $region)
319                     ;
321 Initializer : ASSIGN_SYMBOL AssignmentExpression
322               (list $2)
323             ;
325 EmptyStatement : SEMICOLON
326                ;
328 ExpressionStatement : Expression SEMICOLON
329                     ;
331 IfStatement : 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             ;
337 IterationExpression : 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                     ;
346 ContinueStatement : CONTINUE SEMICOLON
347                   ;
349 ;;JAVE break needs labels 
350 BreakStatement : BREAK SEMICOLON
351               ;;               | BREAK identifier SEMICOLON
352                ;
354 ReturnStatement : RETURN Expression SEMICOLON
355                 | RETURN SEMICOLON
356                 ;
358 WithStatement : WITH OPEN_PARENTHESIS Expression CLOSE_PARENTHESIS   Statement
359               ;
361 OptionalInitializer : Initializer
362                     |
363                     ;
365 PrimaryExpression : THIS
366                   | VARIABLE
367                   | NUMBER
368                   | STRING
369                   | NULL_TOKEN
370                   | TRUE
371                   | FALSE
372                   | OPEN_PARENTHESIS Expression CLOSE_PARENTHESIS
373                   ;
375 MemberExpression : PrimaryExpression
376                  | MemberExpression OPEN_SQ_BRACKETS Expression  CLOSE_SQ_BRACKETS
377                  | MemberExpression DOT VARIABLE
378                  | NEW MemberExpression Arguments
379                  ;
381 NewExpression : MemberExpression
382               | NEW NewExpression
383               ;
385 CallExpression : MemberExpression Arguments
386                | CallExpression Arguments
387                | CallExpression OPEN_SQ_BRACKETS Expression  CLOSE_SQ_BRACKETS
388                | CallExpression DOT VARIABLE
389                ;
391 Arguments : OPEN_PARENTHESIS CLOSE_PARENTHESIS
392           | OPEN_PARENTHESIS ArgumentList CLOSE_PARENTHESIS
393           ;
395 ArgumentList : AssignmentExpression
396              | ArgumentList COMMA AssignmentExpression
397              ;
399 LeftHandSideExpression : NewExpression
400                        | CallExpression
401                        ;
403 PostfixExpression : LeftHandSideExpression
404                   | LeftHandSideExpression INCREMENT
405                   | LeftHandSideExpression DECREMENT
406                   ;
408 UnaryExpression : 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                 ;
420 MultiplicativeExpression : UnaryExpression
421                          | MultiplicativeExpression MULTIPLY UnaryExpression
422                          | MultiplicativeExpression DIV UnaryExpression
423                          | MultiplicativeExpression MOD UnaryExpression
424                          ;
426 AdditiveExpression : MultiplicativeExpression
427                    | AdditiveExpression PLUS MultiplicativeExpression
428                    | AdditiveExpression MINUS MultiplicativeExpression
429                    ;
431 ShiftExpression : AdditiveExpression
432                 | ShiftExpression BITWISE_SHIFT_LEFT AdditiveExpression
433                 | ShiftExpression BITWISE_SHIFT_RIGHT AdditiveExpression
434                 | ShiftExpression BITWISE_SHIFT_RIGHT_ZERO_FILL  AdditiveExpression
435                 ;
437 RelationalExpression : ShiftExpression
438                      | RelationalExpression LESS_THAN ShiftExpression
439                      | RelationalExpression GREATER_THAN ShiftExpression
440                      | RelationalExpression LS_EQUAL ShiftExpression
441                      | RelationalExpression GT_EQUAL ShiftExpression
442                      ;
444 EqualityExpression : RelationalExpression
445                    | EqualityExpression EQUALS RelationalExpression
446                    | EqualityExpression NOT_EQUAL RelationalExpression
447                    ;
449 BitwiseANDExpression : EqualityExpression
450                      | BitwiseANDExpression BITWISE_AND EqualityExpression
451                      ;
453 BitwiseXORExpression : BitwiseANDExpression
454                      | BitwiseXORExpression BITWISE_EXCLUSIVE_OR     BitwiseANDExpression
455                      ;
457 BitwiseORExpression : BitwiseXORExpression
458                     | BitwiseORExpression BITWISE_OR BitwiseXORExpression
459                     ;
461 LogicalANDExpression : BitwiseORExpression
462                      | LogicalANDExpression LOGICAL_AND BitwiseORExpression
463                      ;
465 LogicalORExpression : LogicalANDExpression
466                     | LogicalORExpression LOGICAL_OR LogicalANDExpression
467                     ;
469 ConditionalExpression : LogicalORExpression
470                       | LogicalORExpression QUERY AssignmentExpression COLON    AssignmentExpression
471                       ;
473 AssignmentExpression : ConditionalExpression
474                      | LeftHandSideExpression AssignmentOperator  AssignmentExpression %prec LOWER_THAN_CLOSE_PARENTHESIS
475                      ;
477 AssignmentOperator : 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                    ;
491 Expression : AssignmentExpression
492            | Expression COMMA AssignmentExpression
493            ;
495 OptionalExpression : Expression
496                    |
497                    ;
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
511   ;;stuff generated from the wy file(one for each "type" declaration)
512   wisent-javascript-jv-wy--<number>-regexp-analyzer
513   wisent-javascript-jv-wy--<string>-sexp-analyzer
515   wisent-javascript-jv-wy--<keyword>-keyword-analyzer
517   wisent-javascript-jv-wy--<symbol>-regexp-analyzer
518   wisent-javascript-jv-wy--<punctuation>-string-analyzer
519   wisent-javascript-jv-wy--<block>-block-analyzer
522   ;;;;more std stuff
523   semantic-lex-default-action
524   )
526 ;;; javascript-jv.wy ends here