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