Commit | Line | Data |
---|---|---|
62f43d66 CY |
1 | ;;; python.wy -- LALR grammar for Python |
2 | ||
ab422c4d | 3 | ;; Copyright (C) 2002-2013 Free Software Foundation, Inc. |
685305eb GM |
4 | ;; Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, |
5 | ;; 2009, 2010 Python Software Foundation; All Rights Reserved | |
811954e3 | 6 | |
469d2149 CY |
7 | ;; Author: Richard Kim <ryk@dspwiz.com> |
8 | ;; Maintainer: Richard Kim <ryk@dspwiz.com> | |
9 | ;; Created: June 2002 | |
10 | ;; Keywords: syntax | |
11 | ;; | |
62f43d66 CY |
12 | ;; This file is part of GNU Emacs. |
13 | ||
14 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
15 | ;; it under the terms of the GNU General Public License as published by | |
16 | ;; the Free Software Foundation, either version 3 of the License, or | |
17 | ;; (at your option) any later version. | |
18 | ||
19 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
469d2149 | 20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
62f43d66 CY |
21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | ;; GNU General Public License for more details. | |
23 | ||
469d2149 | 24 | ;; You should have received a copy of the GNU General Public License |
62f43d66 | 25 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
469d2149 CY |
26 | |
27 | ;;; Commentary: | |
28 | ;; | |
29 | ;; This is an LALR python parser that follows the official python | |
811954e3 CY |
30 | ;; grammar closely with very few exceptions. The Python grammar is |
31 | ;; used and reproduced under the following license: | |
32 | ;; | |
33 | ;; PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2 | |
34 | ;; -------------------------------------------- | |
35 | ;; 1. This LICENSE AGREEMENT is between the Python Software Foundation | |
36 | ;; ("PSF"), and the Individual or Organization ("Licensee") accessing | |
37 | ;; and otherwise using this software ("Python") in source or binary | |
38 | ;; form and its associated documentation. | |
39 | ;; | |
40 | ;; 2. Subject to the terms and conditions of this License Agreement, | |
41 | ;; PSF hereby grants Licensee a nonexclusive, royalty-free, world-wide | |
42 | ;; license to reproduce, analyze, test, perform and/or display | |
43 | ;; publicly, prepare derivative works, distribute, and otherwise use | |
44 | ;; Python alone or in any derivative version, provided, however, that | |
45 | ;; PSF's License Agreement and PSF's notice of copyright, i.e., | |
46 | ;; "Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, | |
47 | ;; 2009, 2010 Python Software Foundation; All Rights Reserved" are | |
48 | ;; retained in Python alone or in any derivative version prepared by | |
49 | ;; Licensee. | |
50 | ;; | |
51 | ;; 3. In the event Licensee prepares a derivative work that is based | |
52 | ;; on or incorporates Python or any part thereof, and wants to make | |
53 | ;; the derivative work available to others as provided herein, then | |
54 | ;; Licensee hereby agrees to include in any such work a brief summary | |
55 | ;; of the changes made to Python. | |
469d2149 | 56 | ;; |
811954e3 CY |
57 | ;; 4. PSF is making Python available to Licensee on an "AS IS" |
58 | ;; basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR | |
59 | ;; IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND | |
60 | ;; DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS | |
61 | ;; FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT | |
62 | ;; INFRINGE ANY THIRD PARTY RIGHTS. | |
63 | ;; | |
64 | ;; 5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON | |
65 | ;; FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A | |
66 | ;; RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON, OR | |
67 | ;; ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. | |
68 | ;; | |
69 | ;; 6. This License Agreement will automatically terminate upon a | |
70 | ;; material breach of its terms and conditions. | |
71 | ;; | |
72 | ;; 7. Nothing in this License Agreement shall be deemed to create any | |
73 | ;; relationship of agency, partnership, or joint venture between PSF | |
74 | ;; and Licensee. This License Agreement does not grant permission to | |
75 | ;; use PSF trademarks or trade name in a trademark sense to endorse or | |
76 | ;; promote products or services of Licensee, or any third party. | |
77 | ;; | |
78 | ;; 8. By copying, installing or otherwise using Python, Licensee | |
79 | ;; agrees to be bound by the terms and conditions of this License | |
80 | ;; Agreement. | |
81 | ||
469d2149 CY |
82 | ;;; To do: |
83 | ;; | |
84 | ;; * Verify that semantic-lex-python-number regexp is correct. | |
85 | ||
86 | ;; -------- | |
87 | ;; Settings | |
88 | ;; -------- | |
89 | ||
f79fbbc7 | 90 | %package wisent-python-wy |
62a81506 CY |
91 | %provide semantic/wisent/python-wy |
92 | ||
93 | %{ | |
94 | (declare-function wisent-python-reconstitute-function-tag "semantic/wisent/python") | |
95 | (declare-function wisent-python-reconstitute-class-tag "semantic/wisent/python") | |
96 | } | |
469d2149 CY |
97 | |
98 | %languagemode python-mode | |
99 | ||
100 | ;; The default start symbol | |
101 | %start goal | |
102 | ;; Alternate entry points | |
103 | ;; - Needed by partial re-parse | |
104 | %start function_parameter | |
105 | %start paren_class | |
106 | %start indented_block | |
107 | ;; - Needed by EXPANDFULL clauses | |
108 | %start function_parameters | |
109 | %start paren_classes | |
110 | %start indented_block_body | |
111 | ||
112 | ;; ------------------------------- | |
113 | ;; Misc. Python specific terminals | |
114 | ;; ------------------------------- | |
115 | ;; The value of these tokens are for documentation only, they are not | |
116 | ;; used by the lexer. | |
117 | %token <charquote> BACKSLASH "\\" | |
118 | %token <newline> NEWLINE "\n" | |
119 | %token <indentation> INDENT "^\\s-+" | |
120 | %token <indentation> DEDENT "[^:INDENT:]" | |
121 | %token <indentation> INDENT_BLOCK "(INDENT DEDENT)" | |
122 | ||
123 | ;; ----------------------------- | |
124 | ;; Block & Parenthesis terminals | |
125 | ;; ----------------------------- | |
126 | %type <block> ;;syntax "\\s(\\|\\s)" matchdatatype block | |
127 | ||
128 | %token <block> PAREN_BLOCK "(LPAREN RPAREN)" | |
129 | %token <block> BRACE_BLOCK "(LBRACE RBRACE)" | |
130 | %token <block> BRACK_BLOCK "(LBRACK RBRACK)" | |
131 | ||
132 | %token <open-paren> LPAREN "(" | |
133 | %token <close-paren> RPAREN ")" | |
134 | %token <open-paren> LBRACE "{" | |
135 | %token <close-paren> RBRACE "}" | |
136 | %token <open-paren> LBRACK "[" | |
137 | %token <close-paren> RBRACK "]" | |
138 | ||
139 | ;; ------------------ | |
140 | ;; Operator terminals | |
141 | ;; ------------------ | |
142 | %type <punctuation> ;;syntax "\\(\\s.\\|\\s$\\|\\s'\\)+" matchdatatype string | |
143 | ||
144 | %token <punctuation> LTLTEQ "<<=" | |
145 | %token <punctuation> GTGTEQ ">>=" | |
146 | %token <punctuation> EXPEQ "**=" | |
147 | %token <punctuation> DIVDIVEQ "//=" | |
148 | %token <punctuation> DIVDIV "//" | |
149 | %token <punctuation> LTLT "<<" | |
150 | %token <punctuation> GTGT ">>" | |
151 | %token <punctuation> EXPONENT "**" | |
152 | %token <punctuation> EQ "==" | |
153 | %token <punctuation> GE ">=" | |
154 | %token <punctuation> LE "<=" | |
155 | %token <punctuation> PLUSEQ "+=" | |
156 | %token <punctuation> MINUSEQ "-=" | |
157 | %token <punctuation> MULTEQ "*=" | |
158 | %token <punctuation> DIVEQ "/=" | |
159 | %token <punctuation> MODEQ "%=" | |
160 | %token <punctuation> AMPEQ "&=" | |
161 | %token <punctuation> OREQ "|=" | |
162 | %token <punctuation> HATEQ "^=" | |
163 | %token <punctuation> LTGT "<>" | |
164 | %token <punctuation> NE "!=" | |
165 | %token <punctuation> HAT "^" | |
166 | %token <punctuation> LT "<" | |
167 | %token <punctuation> GT ">" | |
168 | %token <punctuation> AMP "&" | |
169 | %token <punctuation> MULT "*" | |
170 | %token <punctuation> DIV "/" | |
171 | %token <punctuation> MOD "%" | |
172 | %token <punctuation> PLUS "+" | |
173 | %token <punctuation> MINUS "-" | |
174 | %token <punctuation> PERIOD "." | |
175 | %token <punctuation> TILDE "~" | |
176 | %token <punctuation> BAR "|" | |
177 | %token <punctuation> COLON ":" | |
178 | %token <punctuation> SEMICOLON ";" | |
179 | %token <punctuation> COMMA "," | |
180 | %token <punctuation> ASSIGN "=" | |
181 | %token <punctuation> BACKQUOTE "`" | |
62a81506 | 182 | %token <punctuation> AT "@" |
469d2149 CY |
183 | |
184 | ||
185 | ;; ----------------- | |
186 | ;; Literal terminals | |
187 | ;; ----------------- | |
188 | %token <string> STRING_LITERAL | |
189 | ||
190 | %type <number> ;;syntax semantic-lex-number-expression | |
191 | %token <number> NUMBER_LITERAL | |
192 | ||
193 | %type <symbol> ;;syntax "\\(\\sw\\|\\s_\\)+" | |
194 | %token <symbol> NAME | |
195 | ||
196 | ;; ----------------- | |
197 | ;; Keyword terminals | |
198 | ;; ----------------- | |
199 | %type <keyword> ;;syntax "\\(\\sw\\|\\s_\\)+" matchdatatype keyword | |
200 | ||
201 | %keyword AND "and" | |
202 | %put AND summary | |
203 | "Logical AND binary operator ... " | |
204 | ||
205 | %keyword AS "as" | |
206 | %put AS summary | |
207 | "EXPR as NAME makes value of EXPR available as variable NAME" | |
208 | ||
209 | %keyword ASSERT "assert" | |
210 | %put ASSERT summary | |
211 | "Raise AssertionError exception if <expr> is false" | |
212 | ||
213 | %keyword BREAK "break" | |
214 | %put BREAK summary | |
215 | "Terminate 'for' or 'while' loop" | |
216 | ||
217 | %keyword CLASS "class" | |
218 | %put CLASS summary | |
219 | "Define a new class" | |
220 | ||
221 | %keyword CONTINUE "continue" | |
222 | %put CONTINUE summary | |
223 | "Skip to the next iteration of enclosing 'for' or 'while' loop" | |
224 | ||
225 | %keyword DEF "def" | |
226 | %put DEF summary | |
227 | "Define a new function" | |
228 | ||
229 | %keyword DEL "del" | |
230 | %put DEL summary | |
231 | "Delete specified objects, i.e., undo what assignment did" | |
232 | ||
233 | %keyword ELIF "elif" | |
234 | %put ELIF summary | |
235 | "Shorthand for 'else if' following an 'if' statement" | |
236 | ||
237 | %keyword ELSE "else" | |
238 | %put ELSE summary | |
239 | "Start the 'else' clause following an 'if' statement" | |
240 | ||
241 | %keyword EXCEPT "except" | |
242 | %put EXCEPT summary | |
243 | "Specify exception handlers along with 'try' keyword" | |
244 | ||
245 | %keyword EXEC "exec" | |
246 | %put EXEC summary | |
247 | "Dynamically execute Python code" | |
248 | ||
249 | %keyword FINALLY "finally" | |
250 | %put FINALLY summary | |
251 | "Specify code to be executed after 'try' statements whether or not an exception occurred" | |
252 | ||
253 | %keyword FOR "for" | |
254 | %put FOR summary | |
255 | "Start a 'for' loop" | |
256 | ||
257 | %keyword FROM "from" | |
258 | %put FROM summary | |
259 | "Modify behavior of 'import' statement" | |
260 | ||
261 | %keyword GLOBAL "global" | |
262 | %put GLOBAL summary | |
263 | "Declare one or more symbols as global symbols" | |
264 | ||
265 | %keyword IF "if" | |
266 | %put IF summary | |
267 | "Start 'if' conditional statement" | |
268 | ||
269 | %keyword IMPORT "import" | |
270 | %put IMPORT summary | |
271 | "Load specified modules" | |
272 | ||
273 | %keyword IN "in" | |
274 | %put IN summary | |
275 | "Part of 'for' statement " | |
276 | ||
277 | %keyword IS "is" | |
278 | %put IS summary | |
279 | "Binary operator that tests for object equality" | |
280 | ||
281 | %keyword LAMBDA "lambda" | |
282 | %put LAMBDA summary | |
283 | "Create anonymous function" | |
284 | ||
285 | %keyword NOT "not" | |
286 | %put NOT summary | |
287 | "Unary boolean negation operator" | |
288 | ||
289 | %keyword OR "or" | |
290 | %put OR summary | |
291 | "Binary logical 'or' operator" | |
292 | ||
293 | %keyword PASS "pass" | |
294 | %put PASS summary | |
295 | "Statement that does nothing" | |
296 | ||
297 | %keyword PRINT "print" | |
298 | %put PRINT summary | |
299 | "Print each argument to standard output" | |
300 | ||
301 | %keyword RAISE "raise" | |
302 | %put RAISE summary | |
303 | "Raise an exception" | |
304 | ||
305 | %keyword RETURN "return" | |
306 | %put RETURN summary | |
307 | "Return from a function" | |
308 | ||
309 | %keyword TRY "try" | |
310 | %put TRY summary | |
311 | "Start of statements protected by exception handlers" | |
312 | ||
313 | %keyword WHILE "while" | |
314 | %put WHILE summary | |
315 | "Start a 'while' loop" | |
316 | ||
62a81506 CY |
317 | %keyword WITH "with" |
318 | %put WITH summary | |
319 | "Start statement with an associated context object" | |
320 | ||
469d2149 CY |
321 | %keyword YIELD "yield" |
322 | %put YIELD summary | |
323 | "Create a generator function" | |
324 | ||
325 | %% | |
326 | ||
327 | ;;;**************************************************************************** | |
328 | ;;;@ goal | |
329 | ;;;**************************************************************************** | |
330 | ||
331 | ;; simple_stmt are statements that do not involve INDENT tokens | |
332 | ;; compound_stmt are statements that involve INDENT tokens | |
333 | goal | |
334 | : NEWLINE | |
335 | | simple_stmt | |
336 | | compound_stmt | |
337 | ; | |
338 | ||
339 | ;;;**************************************************************************** | |
340 | ;;;@ simple_stmt | |
341 | ;;;**************************************************************************** | |
342 | ||
343 | ;; simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE | |
344 | simple_stmt | |
345 | : small_stmt_list semicolon_opt NEWLINE | |
346 | ; | |
347 | ||
348 | ;; small_stmt (';' small_stmt)* | |
349 | small_stmt_list | |
350 | : small_stmt | |
351 | | small_stmt_list SEMICOLON small_stmt | |
352 | ; | |
353 | ||
354 | small_stmt | |
355 | : expr_stmt | |
356 | | print_stmt | |
357 | | del_stmt | |
358 | | pass_stmt | |
359 | | flow_stmt | |
360 | | import_stmt | |
361 | | global_stmt | |
362 | | exec_stmt | |
363 | | assert_stmt | |
364 | ; | |
365 | ||
366 | ;;;============================================================================ | |
367 | ;;;@@ print_stmt | |
368 | ;;;============================================================================ | |
369 | ||
370 | ;; print_stmt: 'print' [ test (',' test)* [','] ] | |
371 | ;; | '>>' test [ (',' test)+ [','] ] | |
372 | print_stmt | |
373 | : PRINT print_stmt_trailer | |
374 | (CODE-TAG $1 nil) | |
375 | ; | |
376 | ||
377 | ;; [ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ] | |
378 | print_stmt_trailer | |
379 | : test_list_opt | |
380 | () | |
381 | | GTGT test trailing_test_list_with_opt_comma_opt | |
382 | () | |
383 | ; | |
384 | ||
385 | ;; [ (',' test)+ [','] ] | |
386 | trailing_test_list_with_opt_comma_opt | |
387 | : ;;EMPTY | |
388 | | trailing_test_list comma_opt | |
389 | () | |
390 | ; | |
391 | ||
392 | ;; (',' test)+ | |
393 | trailing_test_list | |
394 | : COMMA test | |
395 | () | |
396 | | trailing_test_list COMMA test | |
397 | () | |
398 | ; | |
399 | ||
400 | ;;;============================================================================ | |
401 | ;;;@@ expr_stmt | |
402 | ;;;============================================================================ | |
403 | ||
404 | ;; expr_stmt: testlist (augassign testlist | ('=' testlist)*) | |
405 | expr_stmt | |
406 | : testlist expr_stmt_trailer | |
407 | (if (and $2 (stringp $1) (string-match "^\\(\\sw\\|\\s_\\)+$" $1)) | |
408 | ;; If this is an assignment statement and left side is a symbol, | |
409 | ;; then generate a 'variable token, else return 'code token. | |
410 | (VARIABLE-TAG $1 nil nil) | |
411 | (CODE-TAG $1 nil)) | |
412 | ; | |
413 | ||
414 | ;; Could be EMPTY because of eq_testlist_zom. | |
415 | ;; (augassign testlist | ('=' testlist)*) | |
416 | expr_stmt_trailer | |
417 | : augassign testlist | |
418 | | eq_testlist_zom | |
419 | ; | |
420 | ||
421 | ;; Could be EMPTY! | |
422 | ;; ('=' testlist)* | |
423 | eq_testlist_zom | |
424 | : ;;EMPTY | |
425 | | eq_testlist_zom ASSIGN testlist | |
426 | (identity $3) | |
427 | ; | |
428 | ||
429 | ;; augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
430 | ;; | '<<=' | '>>=' | '**=' | '//=' | |
431 | augassign | |
432 | : PLUSEQ | MINUSEQ | MULTEQ | DIVEQ | MODEQ | |
433 | | AMPEQ | OREQ | HATEQ | LTLTEQ | |
434 | | GTGTEQ | EXPEQ | DIVDIVEQ | |
435 | ; | |
436 | ||
437 | ;;;============================================================================ | |
438 | ;;;@@ del_stmt | |
439 | ;;;============================================================================ | |
440 | ||
441 | ;; del_stmt: 'del' exprlist | |
442 | del_stmt | |
443 | : DEL exprlist | |
444 | (CODE-TAG $1 nil) | |
445 | ; | |
446 | ||
447 | ;; exprlist: expr (',' expr)* [','] | |
448 | exprlist | |
449 | : expr_list comma_opt | |
450 | () | |
451 | ; | |
452 | ||
453 | ;; expr (',' expr)* | |
454 | expr_list | |
455 | : expr | |
456 | () | |
457 | | expr_list COMMA expr | |
458 | () | |
459 | ; | |
460 | ||
461 | ;;;============================================================================ | |
462 | ;;;@@ pass_stmt | |
463 | ;;;============================================================================ | |
464 | ||
465 | ;; pass_stmt: 'pass' | |
466 | pass_stmt | |
467 | : PASS | |
468 | (CODE-TAG $1 nil) | |
469 | ; | |
470 | ||
471 | ;;;============================================================================ | |
472 | ;;;@@ flow_stmt | |
473 | ;;;============================================================================ | |
474 | ||
475 | flow_stmt | |
476 | : break_stmt | |
477 | | continue_stmt | |
478 | | return_stmt | |
479 | | raise_stmt | |
480 | | yield_stmt | |
481 | ; | |
482 | ||
483 | ;; break_stmt: 'break' | |
484 | break_stmt | |
485 | : BREAK | |
486 | (CODE-TAG $1 nil) | |
487 | ; | |
488 | ||
489 | ;; continue_stmt: 'continue' | |
490 | continue_stmt | |
491 | : CONTINUE | |
492 | (CODE-TAG $1 nil) | |
493 | ; | |
494 | ||
495 | ;; return_stmt: 'return' [testlist] | |
496 | return_stmt | |
497 | : RETURN testlist_opt | |
498 | (CODE-TAG $1 nil) | |
499 | ; | |
500 | ||
501 | ;; [testlist] | |
502 | testlist_opt | |
503 | : ;;EMPTY | |
504 | | testlist | |
505 | () | |
506 | ; | |
507 | ||
508 | ;; yield_stmt: 'yield' testlist | |
509 | yield_stmt | |
510 | : YIELD | |
511 | (CODE-TAG $1 nil) | |
512 | | YIELD testlist | |
513 | (CODE-TAG $1 nil) | |
514 | ; | |
515 | ||
516 | ;; raise_stmt: 'raise' [test [',' test [',' test]]] | |
517 | raise_stmt | |
518 | : RAISE zero_one_two_or_three_tests | |
519 | (CODE-TAG $1 nil) | |
520 | ; | |
521 | ||
522 | ;; [test [',' test [',' test]]] | |
523 | zero_one_two_or_three_tests | |
524 | : ;;EMPTY | |
525 | | test zero_one_or_two_tests | |
526 | () | |
527 | ; | |
528 | ||
529 | ;; [',' test [',' test]] | |
530 | zero_one_or_two_tests | |
531 | : ;;EMPTY | |
532 | | COMMA test zero_or_one_comma_test | |
533 | () | |
534 | ; | |
535 | ||
536 | ;; [',' test] | |
537 | zero_or_one_comma_test | |
538 | : ;;EMPTY | |
539 | | COMMA test | |
540 | () | |
541 | ; | |
542 | ||
543 | ;;;============================================================================ | |
544 | ;;;@@ import_stmt | |
545 | ;;;============================================================================ | |
546 | ||
547 | ;; import_stmt : 'import' dotted_as_name (',' dotted_as_name)* | |
548 | ;; | 'from' dotted_name 'import' | |
549 | ;; ('*' | import_as_name (',' import_as_name)*) | |
550 | import_stmt | |
551 | : IMPORT dotted_as_name_list | |
552 | (INCLUDE-TAG $2 nil) | |
553 | | FROM dotted_name IMPORT star_or_import_as_name_list | |
554 | (INCLUDE-TAG $2 nil) | |
555 | ; | |
556 | ||
557 | ;; dotted_as_name (',' dotted_as_name)* | |
558 | dotted_as_name_list | |
62a81506 CY |
559 | : dotted_as_name_list COMMA dotted_as_name |
560 | (cons $3 $1) | |
561 | | dotted_as_name | |
562 | (list $1) | |
469d2149 CY |
563 | ; |
564 | ||
565 | ;; ('*' | import_as_name (',' import_as_name)*) | |
566 | star_or_import_as_name_list | |
567 | : MULT | |
568 | () | |
569 | | import_as_name_list | |
570 | () | |
571 | ; | |
572 | ||
573 | ;; import_as_name (',' import_as_name)* | |
574 | import_as_name_list | |
575 | : import_as_name | |
576 | () | |
577 | | import_as_name_list COMMA import_as_name | |
578 | () | |
579 | ; | |
580 | ||
581 | ;; import_as_name: NAME [NAME NAME] | |
582 | import_as_name | |
583 | : NAME as_name_opt | |
584 | () | |
585 | ; | |
586 | ||
587 | ;; dotted_as_name: dotted_name [AS NAME] | |
588 | dotted_as_name | |
589 | : dotted_name as_name_opt | |
590 | ; | |
591 | ||
592 | ;; [AS NAME] | |
593 | as_name_opt | |
594 | : ;;EMPTY | |
595 | | AS NAME | |
596 | (identity $2) | |
597 | ; | |
598 | ||
599 | ;; dotted_name: NAME ('.' NAME)* | |
600 | dotted_name | |
601 | : NAME | |
602 | | dotted_name PERIOD NAME | |
603 | (format "%s.%s" $1 $3) | |
604 | ; | |
605 | ||
606 | ;;;============================================================================ | |
607 | ;;;@@ global_stmt | |
608 | ;;;============================================================================ | |
609 | ||
610 | ;; global_stmt: 'global' NAME (',' NAME)* | |
611 | global_stmt | |
612 | : GLOBAL comma_sep_name_list | |
613 | (CODE-TAG $1 nil) | |
614 | ; | |
615 | ||
616 | ;; NAME (',' NAME)* | |
617 | comma_sep_name_list | |
618 | : NAME | |
619 | | comma_sep_name_list COMMA NAME | |
620 | ; | |
621 | ||
622 | ;;;============================================================================ | |
623 | ;;;@@ exec_stmt | |
624 | ;;;============================================================================ | |
625 | ||
626 | ;; exec_stmt: 'exec' expr ['in' test [',' test]] | |
627 | exec_stmt | |
628 | : EXEC expr exec_trailer | |
629 | (CODE-TAG $1 nil) | |
630 | ; | |
631 | ||
632 | ;; ['in' test [',' test]] | |
633 | exec_trailer | |
634 | : ;;EMPTY | |
635 | | IN test comma_test_opt | |
636 | () | |
637 | ; | |
638 | ||
639 | ;; [',' test] | |
640 | comma_test_opt | |
641 | : ;;EMPTY | |
642 | | COMMA test | |
643 | () | |
644 | ; | |
645 | ||
646 | ;;;============================================================================ | |
647 | ;;;@@ assert_stmt | |
648 | ;;;============================================================================ | |
649 | ||
650 | ;; assert_stmt: 'assert' test [',' test] | |
651 | assert_stmt | |
652 | : ASSERT test comma_test_opt | |
653 | (CODE-TAG $1 nil) | |
654 | ; | |
655 | ||
656 | ;;;**************************************************************************** | |
657 | ;;;@ compound_stmt | |
658 | ;;;**************************************************************************** | |
659 | ||
660 | compound_stmt | |
661 | : if_stmt | |
662 | | while_stmt | |
663 | | for_stmt | |
664 | | try_stmt | |
62a81506 | 665 | | with_stmt |
469d2149 CY |
666 | | funcdef |
667 | | class_declaration | |
668 | ; | |
669 | ||
670 | ;;;============================================================================ | |
671 | ;;;@@ if_stmt | |
672 | ;;;============================================================================ | |
673 | ||
674 | ;; if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] | |
675 | if_stmt | |
676 | : IF test COLON suite elif_suite_pair_list else_suite_pair_opt | |
677 | (CODE-TAG $1 nil) | |
678 | ; | |
679 | ||
680 | ;; ('elif' test ':' suite)* | |
681 | elif_suite_pair_list | |
682 | : ;;EMPTY | |
683 | | elif_suite_pair_list ELIF test COLON suite | |
684 | () | |
685 | ; | |
686 | ||
687 | ;; ['else' ':' suite] | |
688 | else_suite_pair_opt | |
689 | : ;;EMPTY | |
690 | | ELSE COLON suite | |
691 | () | |
692 | ; | |
693 | ||
694 | ;; This NT follows the COLON token for most compound statements. | |
695 | ;; suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT | |
696 | suite | |
697 | : simple_stmt | |
698 | (list $1) | |
699 | | NEWLINE indented_block | |
700 | (progn $2) | |
701 | ; | |
702 | ||
703 | indented_block | |
704 | : INDENT_BLOCK | |
705 | (EXPANDFULL $1 indented_block_body) | |
706 | ; | |
707 | ||
708 | indented_block_body | |
709 | : INDENT | |
710 | () | |
711 | | DEDENT | |
712 | () | |
713 | | simple_stmt | |
714 | | compound_stmt | |
715 | ; | |
716 | ||
717 | ;;;============================================================================ | |
718 | ;;;@@ while_stmt | |
719 | ;;;============================================================================ | |
720 | ||
721 | ;; while_stmt: 'while' test ':' suite ['else' ':' suite] | |
722 | while_stmt | |
723 | : WHILE test COLON suite else_suite_pair_opt | |
724 | (CODE-TAG $1 nil) | |
725 | ; | |
726 | ||
727 | ;;;============================================================================ | |
728 | ;;;@@ for_stmt | |
729 | ;;;============================================================================ | |
730 | ||
731 | ;; for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] | |
732 | for_stmt | |
733 | : FOR exprlist IN testlist COLON suite else_suite_pair_opt | |
734 | (CODE-TAG $1 nil) | |
735 | ; | |
736 | ||
737 | ;;;============================================================================ | |
738 | ;;;@@ try_stmt | |
739 | ;;;============================================================================ | |
740 | ||
741 | ;; try_stmt: ('try' ':' suite (except_clause ':' suite)+ #diagram:break | |
742 | ;; ['else' ':' suite] | 'try' ':' suite 'finally' ':' suite) | |
743 | try_stmt | |
744 | : TRY COLON suite except_clause_suite_pair_list else_suite_pair_opt | |
745 | (CODE-TAG $1 nil) | |
746 | | TRY COLON suite FINALLY COLON suite | |
747 | (CODE-TAG $1 nil) | |
748 | ; | |
749 | ||
750 | ;; (except_clause ':' suite)+ | |
751 | except_clause_suite_pair_list | |
752 | : except_clause COLON suite | |
753 | () | |
754 | | except_clause_suite_pair_list except_clause COLON suite | |
755 | () | |
756 | ; | |
757 | ||
758 | ;; # NB compile.c makes sure that the default except clause is last | |
759 | ;; except_clause: 'except' [test [',' test]] | |
760 | except_clause | |
761 | : EXCEPT zero_one_or_two_test | |
762 | () | |
763 | ; | |
764 | ||
765 | ;; [test [',' test]] | |
766 | zero_one_or_two_test | |
767 | : ;;EMPTY | |
768 | | test zero_or_one_comma_test | |
769 | () | |
770 | ; | |
771 | ||
62a81506 CY |
772 | ;;;============================================================================ |
773 | ;;@@ with_stmt | |
774 | ;;;============================================================================ | |
775 | ||
776 | ;; with_stmt: 'with' test [ with_var ] ':' suite | |
777 | with_stmt | |
778 | : WITH test COLON suite | |
779 | (CODE-TAG $1 nil) | |
780 | | WITH test with_var COLON suite | |
781 | (CODE-TAG $1 nil) ;; TODO capture variable | |
782 | ; | |
783 | ||
784 | with_var | |
785 | : AS expr | |
786 | () ;; TODO capture | |
787 | ; | |
788 | ||
469d2149 CY |
789 | ;;;============================================================================ |
790 | ;;;@@ funcdef | |
791 | ;;;============================================================================ | |
792 | ||
62a81506 CY |
793 | decorator |
794 | : AT dotted_name varargslist_opt NEWLINE | |
795 | (FUNCTION-TAG $2 "decorator" $3) | |
796 | ; | |
797 | ||
798 | decorators | |
799 | : decorator | |
800 | (list $1) | |
801 | | decorator decorators | |
802 | (cons $1 $2) | |
803 | ; | |
804 | ||
805 | ;; funcdef: [decorators] 'def' NAME parameters ':' suite | |
469d2149 CY |
806 | funcdef |
807 | : DEF NAME function_parameter_list COLON suite | |
62a81506 CY |
808 | (wisent-python-reconstitute-function-tag |
809 | (FUNCTION-TAG $2 nil $3) $5) | |
810 | | decorators DEF NAME function_parameter_list COLON suite | |
811 | (wisent-python-reconstitute-function-tag | |
812 | (FUNCTION-TAG $3 nil $4 :decorators $1) $6) | |
469d2149 CY |
813 | ; |
814 | ||
815 | function_parameter_list | |
816 | : PAREN_BLOCK | |
817 | (let ((wisent-python-EXPANDING-block t)) | |
818 | (EXPANDFULL $1 function_parameters)) | |
819 | ; | |
820 | ||
821 | ;; parameters: '(' [varargslist] ')' | |
822 | function_parameters | |
823 | : LPAREN | |
824 | () | |
825 | | RPAREN | |
826 | () | |
827 | | function_parameter COMMA | |
828 | | function_parameter RPAREN | |
829 | ; | |
830 | ||
831 | function_parameter | |
832 | : fpdef_opt_test | |
833 | ;; : NAME | |
834 | ;; (VARIABLE-TAG $1 nil nil) | |
835 | | MULT NAME | |
836 | (VARIABLE-TAG $2 nil nil) | |
837 | | EXPONENT NAME | |
838 | (VARIABLE-TAG $2 nil nil) | |
839 | ; | |
840 | ||
841 | ;;;============================================================================ | |
842 | ;;;@@ class_declaration | |
843 | ;;;============================================================================ | |
844 | ||
845 | ;; classdef: 'class' NAME ['(' testlist ')'] ':' suite | |
846 | class_declaration | |
847 | : CLASS NAME paren_class_list_opt COLON suite | |
62a81506 CY |
848 | (wisent-python-reconstitute-class-tag |
849 | (TYPE-TAG $2 $1 ;; Name "class" | |
850 | $5 ;; Members | |
851 | (cons $3 nil) ;; (SUPERCLASSES . INTERFACES) | |
852 | )) | |
469d2149 CY |
853 | ; |
854 | ||
855 | ;; ['(' testlist ')'] | |
856 | paren_class_list_opt | |
857 | : ;;EMPTY | |
858 | | paren_class_list | |
859 | ; | |
860 | ||
861 | paren_class_list | |
862 | : PAREN_BLOCK | |
863 | (let ((wisent-python-EXPANDING-block t)) | |
864 | (mapcar 'semantic-tag-name (EXPANDFULL $1 paren_classes))) | |
865 | ; | |
866 | ||
867 | ;; parameters: '(' [varargslist] ')' | |
868 | paren_classes | |
869 | : LPAREN | |
870 | () | |
871 | | RPAREN | |
872 | () | |
873 | | paren_class COMMA | |
874 | (VARIABLE-TAG $1 nil nil) | |
875 | | paren_class RPAREN | |
876 | (VARIABLE-TAG $1 nil nil) | |
877 | ; | |
878 | ||
879 | ;; In general, the base class can be specified by a general expression | |
40a8bdf6 | 880 | ;; which evaluates to a class object, i.e., base classes are not just names! |
469d2149 CY |
881 | ;; However base classes are names in most cases. Thus the |
882 | ;; non-terminals below work only with simple names. Even if the | |
883 | ;; parser can parse general expressions, I don't see much benefit in | |
884 | ;; generating a string of expression as base class "name". | |
885 | paren_class | |
886 | : dotted_name | |
887 | ; | |
888 | ||
889 | ;;;**************************************************************************** | |
890 | ;;;@ test | |
891 | ;;;**************************************************************************** | |
892 | ||
893 | ;; test: and_test ('or' and_test)* | lambdef | |
894 | test | |
895 | : test_test | |
896 | | lambdef | |
897 | ; | |
898 | ||
899 | ;; and_test ('or' and_test)* | |
900 | test_test | |
901 | : and_test | |
902 | | test_test OR and_test | |
903 | () | |
904 | ; | |
905 | ||
906 | ;; and_test: not_test ('and' not_test)* | |
907 | and_test | |
908 | : not_test | |
909 | | and_test AND not_test | |
910 | () | |
911 | ; | |
912 | ||
913 | ;; not_test: 'not' not_test | comparison | |
914 | not_test | |
915 | : NOT not_test | |
916 | () | |
917 | | comparison | |
918 | ; | |
919 | ||
920 | ;; comparison: expr (comp_op expr)* | |
921 | comparison | |
922 | : expr | |
923 | | comparison comp_op expr | |
924 | () | |
925 | ; | |
926 | ||
927 | ;; comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' | |
928 | comp_op | |
929 | : LT | GT | EQ | GE | LE | LTGT | NE | IN | NOT IN | IS | IS NOT | |
930 | ; | |
931 | ||
932 | ;; expr: xor_expr ('|' xor_expr)* | |
933 | expr | |
934 | : xor_expr | |
935 | | expr BAR xor_expr | |
936 | () | |
937 | ; | |
938 | ||
939 | ;; xor_expr: and_expr ('^' and_expr)* | |
940 | xor_expr | |
941 | : and_expr | |
942 | | xor_expr HAT and_expr | |
943 | () | |
944 | ; | |
945 | ||
946 | ;; and_expr: shift_expr ('&' shift_expr)* | |
947 | and_expr | |
948 | : shift_expr | |
949 | | and_expr AMP shift_expr | |
950 | () | |
951 | ; | |
952 | ||
953 | ;; shift_expr: arith_expr (('<<'|'>>') arith_expr)* | |
954 | shift_expr | |
955 | : arith_expr | |
956 | | shift_expr shift_expr_operators arith_expr | |
957 | () | |
958 | ; | |
959 | ||
960 | ;; ('<<'|'>>') | |
961 | shift_expr_operators | |
962 | : LTLT | |
963 | | GTGT | |
964 | ; | |
965 | ||
966 | ;; arith_expr: term (('+'|'-') term)* | |
967 | arith_expr | |
968 | : term | |
969 | | arith_expr plus_or_minus term | |
970 | () | |
971 | ; | |
972 | ||
973 | ;; ('+'|'-') | |
974 | plus_or_minus | |
975 | : PLUS | |
976 | | MINUS | |
977 | ; | |
978 | ||
979 | ;; term: factor (('*'|'/'|'%'|'//') factor)* | |
980 | term | |
981 | : factor | |
982 | | term term_operator factor | |
983 | () | |
984 | ; | |
985 | ||
986 | term_operator | |
987 | : MULT | |
988 | | DIV | |
989 | | MOD | |
990 | | DIVDIV | |
991 | ; | |
992 | ||
993 | ;; factor: ('+'|'-'|'~') factor | power | |
994 | factor | |
995 | : prefix_operators factor | |
996 | () | |
997 | | power | |
998 | ; | |
999 | ||
1000 | ;; ('+'|'-'|'~') | |
1001 | prefix_operators | |
1002 | : PLUS | |
1003 | | MINUS | |
1004 | | TILDE | |
1005 | ; | |
1006 | ||
1007 | ;; power: atom trailer* ('**' factor)* | |
1008 | power | |
1009 | : atom trailer_zom exponent_zom | |
1010 | (concat $1 | |
1011 | (if $2 (concat " " $2 " ") "") | |
1012 | (if $3 (concat " " $3) "") | |
1013 | ) | |
1014 | ; | |
1015 | ||
1016 | trailer_zom | |
1017 | : ;;EMPTY | |
1018 | | trailer_zom trailer | |
1019 | () | |
1020 | ; | |
1021 | ||
1022 | exponent_zom | |
1023 | : ;;EMPTY | |
1024 | | exponent_zom EXPONENT factor | |
1025 | () | |
1026 | ; | |
1027 | ||
1028 | ;; trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME | |
1029 | trailer | |
1030 | : PAREN_BLOCK | |
1031 | () | |
1032 | | BRACK_BLOCK | |
1033 | () | |
1034 | | PERIOD NAME | |
1035 | () | |
1036 | ; | |
1037 | ||
1038 | ;; atom: '(' [testlist] ')' | '[' [listmaker] ']' | '{' [dictmaker] '}' | |
1039 | ;; | '`' testlist '`' | NAME | NUMBER | STRING+ | |
1040 | atom | |
1041 | : PAREN_BLOCK | |
1042 | () | |
1043 | | BRACK_BLOCK | |
1044 | () | |
1045 | | BRACE_BLOCK | |
1046 | () | |
1047 | | BACKQUOTE testlist BACKQUOTE | |
1048 | () | |
1049 | | NAME | |
1050 | | NUMBER_LITERAL | |
1051 | | one_or_more_string | |
1052 | ; | |
1053 | ||
1054 | test_list_opt | |
1055 | : ;;EMPTY | |
1056 | | testlist | |
1057 | () | |
1058 | ; | |
1059 | ||
1060 | ;; testlist: test (',' test)* [','] | |
1061 | testlist | |
1062 | : comma_sep_test_list comma_opt | |
1063 | ; | |
1064 | ||
1065 | ;; test (',' test)* | |
1066 | comma_sep_test_list | |
1067 | : test | |
1068 | | comma_sep_test_list COMMA test | |
1069 | (format "%s, %s" $1 $3) | |
1070 | ; | |
1071 | ||
1072 | ;; (read $1) and (read $2) were done before to peel away the double quotes. | |
1073 | ;; However that does not work for single quotes, so it was taken out. | |
1074 | one_or_more_string | |
1075 | : STRING_LITERAL | |
1076 | | one_or_more_string STRING_LITERAL | |
1077 | (concat $1 $2) | |
1078 | ; | |
1079 | ||
1080 | ;;;**************************************************************************** | |
1081 | ;;;@ lambdef | |
1082 | ;;;**************************************************************************** | |
1083 | ||
1084 | ;; lambdef: 'lambda' [varargslist] ':' test | |
1085 | lambdef | |
1086 | : LAMBDA varargslist_opt COLON test | |
1087 | (format "%s %s" $1 (or $2 "")) | |
1088 | ; | |
1089 | ||
1090 | ;; [varargslist] | |
1091 | varargslist_opt | |
1092 | : ;;EMPTY | |
1093 | | varargslist | |
1094 | ; | |
1095 | ||
1096 | ;; varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) | |
1097 | ;; | fpdef ['=' test] (',' fpdef ['=' test])* [','] | |
1098 | varargslist | |
1099 | : fpdef_opt_test_list_comma_zom rest_args | |
1100 | (nconc $2 $1) | |
1101 | | fpdef_opt_test_list comma_opt | |
1102 | ; | |
1103 | ||
1104 | ;; ('*' NAME [',' '**' NAME] | '**' NAME) | |
1105 | rest_args | |
1106 | : MULT NAME multmult_name_opt | |
1107 | () ;;(VARIABLE-TAG $2 nil nil) | |
1108 | | EXPONENT NAME | |
1109 | () ;;(VARIABLE-TAG $2 nil nil) | |
1110 | ; | |
1111 | ||
1112 | ;; [',' '**' NAME] | |
1113 | multmult_name_opt | |
1114 | : ;;EMPTY | |
1115 | | COMMA EXPONENT NAME | |
1116 | (VARIABLE-TAG $3 nil nil) | |
1117 | ; | |
1118 | ||
1119 | fpdef_opt_test_list_comma_zom | |
1120 | : ;;EMPTY | |
1121 | | fpdef_opt_test_list_comma_zom fpdef_opt_test COMMA | |
1122 | (nconc $2 $1) | |
1123 | ; | |
1124 | ||
1125 | ;; fpdef ['=' test] (',' fpdef ['=' test])* | |
1126 | fpdef_opt_test_list | |
1127 | : fpdef_opt_test | |
1128 | | fpdef_opt_test_list COMMA fpdef_opt_test | |
1129 | (nconc $3 $1) | |
1130 | ; | |
1131 | ||
1132 | ;; fpdef ['=' test] | |
1133 | fpdef_opt_test | |
1134 | : fpdef eq_test_opt | |
1135 | ; | |
1136 | ||
1137 | ;; fpdef: NAME | '(' fplist ')' | |
1138 | fpdef | |
1139 | : NAME | |
1140 | (VARIABLE-TAG $1 nil nil) | |
1141 | ;; Below breaks the parser. Don't know why, but my guess is that | |
1142 | ;; LPAREN/RPAREN clashes with the ones in function_parameters. | |
1143 | ;; | LPAREN fplist RPAREN | |
1144 | ;; (identity $2) | |
1145 | ; | |
1146 | ||
1147 | ;; fplist: fpdef (',' fpdef)* [','] | |
1148 | fplist | |
1149 | : fpdef_list comma_opt | |
1150 | ; | |
1151 | ||
1152 | ;; fpdef (',' fpdef)* | |
1153 | fpdef_list | |
1154 | : fpdef | |
1155 | | fpdef_list COMMA fpdef | |
1156 | ; | |
1157 | ||
1158 | ;; ['=' test] | |
1159 | eq_test_opt | |
1160 | : ;;EMPTY | |
1161 | | ASSIGN test | |
1162 | () | |
1163 | ; | |
1164 | ||
1165 | ;;;**************************************************************************** | |
1166 | ;;;@ Misc | |
1167 | ;;;**************************************************************************** | |
1168 | ||
1169 | ;; [','] | |
1170 | comma_opt | |
1171 | : ;;EMPTY | |
1172 | | COMMA | |
1173 | ; | |
1174 | ||
1175 | ;; [';'] | |
1176 | semicolon_opt | |
1177 | : ;;EMPTY | |
1178 | | SEMICOLON | |
1179 | ; | |
1180 | ||
62f43d66 | 1181 | ;;; python.wy ends here |