1 ;;; python.wy -- LALR grammar for Python
3 ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
4 ;; Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5 ;; 2009, 2010 Python Software Foundation; All Rights Reserved
7 ;; Author: Richard Kim <ryk@dspwiz.com>
8 ;; Maintainer: Richard Kim <ryk@dspwiz.com>
12 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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:
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.
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
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.
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.
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.
69 ;; 6. This License Agreement will automatically terminate upon a
70 ;; material breach of its terms and conditions.
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.
78 ;; 8. By copying, installing or otherwise using Python, Licensee
79 ;; agrees to be bound by the terms and conditions of this License
84 ;; * Verify that semantic-lex-python-number regexp is correct.
90 %package wisent-python-wy
92 %languagemode python-mode
94 ;; The default start symbol
96 ;; Alternate entry points
97 ;; - Needed by partial re-parse
98 %start function_parameter
100 %start indented_block
101 ;; - Needed by EXPANDFULL clauses
102 %start function_parameters
104 %start indented_block_body
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)"
117 ;; -----------------------------
118 ;; Block & Parenthesis terminals
119 ;; -----------------------------
120 %type <block> ;;syntax "\\s(\\|\\s)" matchdatatype block
122 %token <block> PAREN_BLOCK "(LPAREN RPAREN)"
123 %token <block> BRACE_BLOCK "(LBRACE RBRACE)"
124 %token <block> BRACK_BLOCK "(LBRACK RBRACK)"
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 "]"
133 ;; ------------------
134 ;; Operator terminals
135 ;; ------------------
136 %type <punctuation> ;;syntax "\\(\\s.\\|\\s$\\|\\s'\\)+" matchdatatype string
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 "`"
181 %token <string> STRING_LITERAL
183 %type <number> ;;syntax semantic-lex-number-expression
184 %token <number> NUMBER_LITERAL
186 %type <symbol> ;;syntax "\\(\\sw\\|\\s_\\)+"
192 %type <keyword> ;;syntax "\\(\\sw\\|\\s_\\)+" matchdatatype keyword
196 "Logical AND binary operator ... "
200 "EXPR as NAME makes value of EXPR available as variable NAME"
202 %keyword ASSERT "assert"
204 "Raise AssertionError exception if <expr> is false"
206 %keyword BREAK "break"
208 "Terminate 'for' or 'while' loop"
210 %keyword CLASS "class"
214 %keyword CONTINUE "continue"
215 %put CONTINUE summary
216 "Skip to the next iteration of enclosing 'for' or 'while' loop"
220 "Define a new function"
224 "Delete specified objects, i.e., undo what assignment did"
228 "Shorthand for 'else if' following an 'if' statement"
232 "Start the 'else' clause following an 'if' statement"
234 %keyword EXCEPT "except"
236 "Specify exception handlers along with 'try' keyword"
240 "Dynamically execute Python code"
242 %keyword FINALLY "finally"
244 "Specify code to be executed after 'try' statements whether or not an exception occurred"
252 "Modify behavior of 'import' statement"
254 %keyword GLOBAL "global"
256 "Declare one or more symbols as global symbols"
260 "Start 'if' conditional statement"
262 %keyword IMPORT "import"
264 "Load specified modules"
268 "Part of 'for' statement "
272 "Binary operator that tests for object equality"
274 %keyword LAMBDA "lambda"
276 "Create anonymous function"
280 "Unary boolean negation operator"
284 "Binary logical 'or' operator"
288 "Statement that does nothing"
290 %keyword PRINT "print"
292 "Print each argument to standard output"
294 %keyword RAISE "raise"
298 %keyword RETURN "return"
300 "Return from a function"
304 "Start of statements protected by exception handlers"
306 %keyword WHILE "while"
308 "Start a 'while' loop"
310 %keyword YIELD "yield"
312 "Create a generator function"
316 ;;;****************************************************************************
318 ;;;****************************************************************************
320 ;; simple_stmt are statements that do not involve INDENT tokens
321 ;; compound_stmt are statements that involve INDENT tokens
328 ;;;****************************************************************************
330 ;;;****************************************************************************
332 ;; simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
334 : small_stmt_list semicolon_opt NEWLINE
337 ;; small_stmt (';' small_stmt)*
340 | small_stmt_list SEMICOLON small_stmt
355 ;;;============================================================================
357 ;;;============================================================================
359 ;; print_stmt: 'print' [ test (',' test)* [','] ]
360 ;; | '>>' test [ (',' test)+ [','] ]
362 : PRINT print_stmt_trailer
366 ;; [ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ]
370 | GTGT test trailing_test_list_with_opt_comma_opt
374 ;; [ (',' test)+ [','] ]
375 trailing_test_list_with_opt_comma_opt
377 | trailing_test_list comma_opt
385 | trailing_test_list COMMA test
389 ;;;============================================================================
391 ;;;============================================================================
393 ;; expr_stmt: testlist (augassign testlist | ('=' testlist)*)
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)
403 ;; Could be EMPTY because of eq_testlist_zom.
404 ;; (augassign testlist | ('=' testlist)*)
414 | eq_testlist_zom ASSIGN testlist
418 ;; augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
419 ;; | '<<=' | '>>=' | '**=' | '//='
421 : PLUSEQ | MINUSEQ | MULTEQ | DIVEQ | MODEQ
422 | AMPEQ | OREQ | HATEQ | LTLTEQ
423 | GTGTEQ | EXPEQ | DIVDIVEQ
426 ;;;============================================================================
428 ;;;============================================================================
430 ;; del_stmt: 'del' exprlist
436 ;; exprlist: expr (',' expr)* [',']
438 : expr_list comma_opt
446 | expr_list COMMA expr
450 ;;;============================================================================
452 ;;;============================================================================
460 ;;;============================================================================
462 ;;;============================================================================
472 ;; break_stmt: 'break'
478 ;; continue_stmt: 'continue'
484 ;; return_stmt: 'return' [testlist]
486 : RETURN testlist_opt
497 ;; yield_stmt: 'yield' testlist
505 ;; raise_stmt: 'raise' [test [',' test [',' test]]]
507 : RAISE zero_one_two_or_three_tests
511 ;; [test [',' test [',' test]]]
512 zero_one_two_or_three_tests
514 | test zero_one_or_two_tests
518 ;; [',' test [',' test]]
519 zero_one_or_two_tests
521 | COMMA test zero_or_one_comma_test
526 zero_or_one_comma_test
532 ;;;============================================================================
534 ;;;============================================================================
536 ;; import_stmt : 'import' dotted_as_name (',' dotted_as_name)*
537 ;; | 'from' dotted_name 'import'
538 ;; ('*' | import_as_name (',' import_as_name)*)
540 : IMPORT dotted_as_name_list
542 | FROM dotted_name IMPORT star_or_import_as_name_list
546 ;; dotted_as_name (',' dotted_as_name)*
549 | dotted_as_name_list COMMA dotted_as_name
552 ;; ('*' | import_as_name (',' import_as_name)*)
553 star_or_import_as_name_list
556 | import_as_name_list
560 ;; import_as_name (',' import_as_name)*
564 | import_as_name_list COMMA import_as_name
568 ;; import_as_name: NAME [NAME NAME]
574 ;; dotted_as_name: dotted_name [AS NAME]
576 : dotted_name as_name_opt
586 ;; dotted_name: NAME ('.' NAME)*
589 | dotted_name PERIOD NAME
590 (format "%s.%s" $1 $3)
593 ;;;============================================================================
595 ;;;============================================================================
597 ;; global_stmt: 'global' NAME (',' NAME)*
599 : GLOBAL comma_sep_name_list
606 | comma_sep_name_list COMMA NAME
609 ;;;============================================================================
611 ;;;============================================================================
613 ;; exec_stmt: 'exec' expr ['in' test [',' test]]
615 : EXEC expr exec_trailer
619 ;; ['in' test [',' test]]
622 | IN test comma_test_opt
633 ;;;============================================================================
635 ;;;============================================================================
637 ;; assert_stmt: 'assert' test [',' test]
639 : ASSERT test comma_test_opt
643 ;;;****************************************************************************
645 ;;;****************************************************************************
656 ;;;============================================================================
658 ;;;============================================================================
660 ;; if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
662 : IF test COLON suite elif_suite_pair_list else_suite_pair_opt
666 ;; ('elif' test ':' suite)*
669 | elif_suite_pair_list ELIF test COLON suite
673 ;; ['else' ':' suite]
680 ;; This NT follows the COLON token for most compound statements.
681 ;; suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
685 | NEWLINE indented_block
691 (EXPANDFULL $1 indented_block_body)
703 ;;;============================================================================
705 ;;;============================================================================
707 ;; while_stmt: 'while' test ':' suite ['else' ':' suite]
709 : WHILE test COLON suite else_suite_pair_opt
713 ;;;============================================================================
715 ;;;============================================================================
717 ;; for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
719 : FOR exprlist IN testlist COLON suite else_suite_pair_opt
723 ;;;============================================================================
725 ;;;============================================================================
727 ;; try_stmt: ('try' ':' suite (except_clause ':' suite)+ #diagram:break
728 ;; ['else' ':' suite] | 'try' ':' suite 'finally' ':' suite)
730 : TRY COLON suite except_clause_suite_pair_list else_suite_pair_opt
732 | TRY COLON suite FINALLY COLON suite
736 ;; (except_clause ':' suite)+
737 except_clause_suite_pair_list
738 : except_clause COLON suite
740 | except_clause_suite_pair_list except_clause COLON suite
744 ;; # NB compile.c makes sure that the default except clause is last
745 ;; except_clause: 'except' [test [',' test]]
747 : EXCEPT zero_one_or_two_test
754 | test zero_or_one_comma_test
758 ;;;============================================================================
760 ;;;============================================================================
762 ;; funcdef: 'def' NAME parameters ':' suite
764 : DEF NAME function_parameter_list COLON suite
765 (FUNCTION-TAG $2 nil $3)
768 function_parameter_list
770 (let ((wisent-python-EXPANDING-block t))
771 (EXPANDFULL $1 function_parameters))
774 ;; parameters: '(' [varargslist] ')'
780 | function_parameter COMMA
781 | function_parameter RPAREN
787 ;; (VARIABLE-TAG $1 nil nil)
789 (VARIABLE-TAG $2 nil nil)
791 (VARIABLE-TAG $2 nil nil)
794 ;;;============================================================================
795 ;;;@@ class_declaration
796 ;;;============================================================================
798 ;; classdef: 'class' NAME ['(' testlist ')'] ':' suite
800 : CLASS NAME paren_class_list_opt COLON suite
801 (TYPE-TAG $2 $1 ;; Name "class"
803 (cons $3 nil) ;; (SUPERCLASSES . INTERFACES)
807 ;; ['(' testlist ')']
815 (let ((wisent-python-EXPANDING-block t))
816 (mapcar 'semantic-tag-name (EXPANDFULL $1 paren_classes)))
819 ;; parameters: '(' [varargslist] ')'
826 (VARIABLE-TAG $1 nil nil)
828 (VARIABLE-TAG $1 nil nil)
831 ;; In general, the base class can be specified by a general expression
832 ;; which evaluates to a class object, i.e., base classes are not just names!
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".
841 ;;;****************************************************************************
843 ;;;****************************************************************************
845 ;; test: and_test ('or' and_test)* | lambdef
851 ;; and_test ('or' and_test)*
854 | test_test OR and_test
858 ;; and_test: not_test ('and' not_test)*
861 | and_test AND not_test
865 ;; not_test: 'not' not_test | comparison
872 ;; comparison: expr (comp_op expr)*
875 | comparison comp_op expr
879 ;; comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
881 : LT | GT | EQ | GE | LE | LTGT | NE | IN | NOT IN | IS | IS NOT
884 ;; expr: xor_expr ('|' xor_expr)*
891 ;; xor_expr: and_expr ('^' and_expr)*
894 | xor_expr HAT and_expr
898 ;; and_expr: shift_expr ('&' shift_expr)*
901 | and_expr AMP shift_expr
905 ;; shift_expr: arith_expr (('<<'|'>>') arith_expr)*
908 | shift_expr shift_expr_operators arith_expr
918 ;; arith_expr: term (('+'|'-') term)*
921 | arith_expr plus_or_minus term
931 ;; term: factor (('*'|'/'|'%'|'//') factor)*
934 | term term_operator factor
945 ;; factor: ('+'|'-'|'~') factor | power
947 : prefix_operators factor
959 ;; power: atom trailer* ('**' factor)*
961 : atom trailer_zom exponent_zom
963 (if $2 (concat " " $2 " ") "")
964 (if $3 (concat " " $3) "")
970 | trailer_zom trailer
976 | exponent_zom EXPONENT factor
980 ;; trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
990 ;; atom: '(' [testlist] ')' | '[' [listmaker] ']' | '{' [dictmaker] '}'
991 ;; | '`' testlist '`' | NAME | NUMBER | STRING+
999 | BACKQUOTE testlist BACKQUOTE
1003 | one_or_more_string
1012 ;; testlist: test (',' test)* [',']
1014 : comma_sep_test_list comma_opt
1020 | comma_sep_test_list COMMA test
1021 (format "%s, %s" $1 $3)
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.
1028 | one_or_more_string STRING_LITERAL
1032 ;;;****************************************************************************
1034 ;;;****************************************************************************
1036 ;; lambdef: 'lambda' [varargslist] ':' test
1038 : LAMBDA varargslist_opt COLON test
1039 (format "%s %s" $1 (or $2 ""))
1048 ;; varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
1049 ;; | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1051 : fpdef_opt_test_list_comma_zom rest_args
1053 | fpdef_opt_test_list comma_opt
1056 ;; ('*' NAME [',' '**' NAME] | '**' NAME)
1058 : MULT NAME multmult_name_opt
1059 () ;;(VARIABLE-TAG $2 nil nil)
1061 () ;;(VARIABLE-TAG $2 nil nil)
1067 | COMMA EXPONENT NAME
1068 (VARIABLE-TAG $3 nil nil)
1071 fpdef_opt_test_list_comma_zom
1073 | fpdef_opt_test_list_comma_zom fpdef_opt_test COMMA
1077 ;; fpdef ['=' test] (',' fpdef ['=' test])*
1080 | fpdef_opt_test_list COMMA fpdef_opt_test
1089 ;; fpdef: NAME | '(' fplist ')'
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
1099 ;; fplist: fpdef (',' fpdef)* [',']
1101 : fpdef_list comma_opt
1104 ;; fpdef (',' fpdef)*
1107 | fpdef_list COMMA fpdef
1117 ;;;****************************************************************************
1119 ;;;****************************************************************************
1133 ;;; python.wy ends here