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