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