1 ;;; python.wy -- LALR grammar for Python
3 ;; Copyright (C) 2002-2011 Free Software Foundation, Inc.
4 ;; Copyright (C) 2001-2010 Python Software Foundation
6 ;; Author: Richard Kim <ryk@dspwiz.com>
7 ;; Maintainer: Richard Kim <ryk@dspwiz.com>
11 ;; This file is part of GNU Emacs.
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.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This is an LALR python parser that follows the official python
29 ;; grammar closely with very few exceptions. The Python grammar is
30 ;; used and reproduced under the following license:
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.
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
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.
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.
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.
68 ;; 6. This License Agreement will automatically terminate upon a
69 ;; material breach of its terms and conditions.
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.
77 ;; 8. By copying, installing or otherwise using Python, Licensee
78 ;; agrees to be bound by the terms and conditions of this License
83 ;; * Verify that semantic-lex-python-number regexp is correct.
89 %package wisent-python-wy
91 %languagemode python-mode
93 ;; The default start symbol
95 ;; Alternate entry points
96 ;; - Needed by partial re-parse
97 %start function_parameter
100 ;; - Needed by EXPANDFULL clauses
101 %start function_parameters
103 %start indented_block_body
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)"
116 ;; -----------------------------
117 ;; Block & Parenthesis terminals
118 ;; -----------------------------
119 %type <block> ;;syntax "\\s(\\|\\s)" matchdatatype block
121 %token <block> PAREN_BLOCK "(LPAREN RPAREN)"
122 %token <block> BRACE_BLOCK "(LBRACE RBRACE)"
123 %token <block> BRACK_BLOCK "(LBRACK RBRACK)"
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 "]"
132 ;; ------------------
133 ;; Operator terminals
134 ;; ------------------
135 %type <punctuation> ;;syntax "\\(\\s.\\|\\s$\\|\\s'\\)+" matchdatatype string
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 "`"
180 %token <string> STRING_LITERAL
182 %type <number> ;;syntax semantic-lex-number-expression
183 %token <number> NUMBER_LITERAL
185 %type <symbol> ;;syntax "\\(\\sw\\|\\s_\\)+"
191 %type <keyword> ;;syntax "\\(\\sw\\|\\s_\\)+" matchdatatype keyword
195 "Logical AND binary operator ... "
199 "EXPR as NAME makes value of EXPR available as variable NAME"
201 %keyword ASSERT "assert"
203 "Raise AssertionError exception if <expr> is false"
205 %keyword BREAK "break"
207 "Terminate 'for' or 'while' loop"
209 %keyword CLASS "class"
213 %keyword CONTINUE "continue"
214 %put CONTINUE summary
215 "Skip to the next iteration of enclosing 'for' or 'while' loop"
219 "Define a new function"
223 "Delete specified objects, i.e., undo what assignment did"
227 "Shorthand for 'else if' following an 'if' statement"
231 "Start the 'else' clause following an 'if' statement"
233 %keyword EXCEPT "except"
235 "Specify exception handlers along with 'try' keyword"
239 "Dynamically execute Python code"
241 %keyword FINALLY "finally"
243 "Specify code to be executed after 'try' statements whether or not an exception occurred"
251 "Modify behavior of 'import' statement"
253 %keyword GLOBAL "global"
255 "Declare one or more symbols as global symbols"
259 "Start 'if' conditional statement"
261 %keyword IMPORT "import"
263 "Load specified modules"
267 "Part of 'for' statement "
271 "Binary operator that tests for object equality"
273 %keyword LAMBDA "lambda"
275 "Create anonymous function"
279 "Unary boolean negation operator"
283 "Binary logical 'or' operator"
287 "Statement that does nothing"
289 %keyword PRINT "print"
291 "Print each argument to standard output"
293 %keyword RAISE "raise"
297 %keyword RETURN "return"
299 "Return from a function"
303 "Start of statements protected by exception handlers"
305 %keyword WHILE "while"
307 "Start a 'while' loop"
309 %keyword YIELD "yield"
311 "Create a generator function"
315 ;;;****************************************************************************
317 ;;;****************************************************************************
319 ;; simple_stmt are statements that do not involve INDENT tokens
320 ;; compound_stmt are statements that involve INDENT tokens
327 ;;;****************************************************************************
329 ;;;****************************************************************************
331 ;; simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
333 : small_stmt_list semicolon_opt NEWLINE
336 ;; small_stmt (';' small_stmt)*
339 | small_stmt_list SEMICOLON small_stmt
354 ;;;============================================================================
356 ;;;============================================================================
358 ;; print_stmt: 'print' [ test (',' test)* [','] ]
359 ;; | '>>' test [ (',' test)+ [','] ]
361 : PRINT print_stmt_trailer
365 ;; [ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ]
369 | GTGT test trailing_test_list_with_opt_comma_opt
373 ;; [ (',' test)+ [','] ]
374 trailing_test_list_with_opt_comma_opt
376 | trailing_test_list comma_opt
384 | trailing_test_list COMMA test
388 ;;;============================================================================
390 ;;;============================================================================
392 ;; expr_stmt: testlist (augassign testlist | ('=' testlist)*)
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)
402 ;; Could be EMPTY because of eq_testlist_zom.
403 ;; (augassign testlist | ('=' testlist)*)
413 | eq_testlist_zom ASSIGN testlist
417 ;; augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
418 ;; | '<<=' | '>>=' | '**=' | '//='
420 : PLUSEQ | MINUSEQ | MULTEQ | DIVEQ | MODEQ
421 | AMPEQ | OREQ | HATEQ | LTLTEQ
422 | GTGTEQ | EXPEQ | DIVDIVEQ
425 ;;;============================================================================
427 ;;;============================================================================
429 ;; del_stmt: 'del' exprlist
435 ;; exprlist: expr (',' expr)* [',']
437 : expr_list comma_opt
445 | expr_list COMMA expr
449 ;;;============================================================================
451 ;;;============================================================================
459 ;;;============================================================================
461 ;;;============================================================================
471 ;; break_stmt: 'break'
477 ;; continue_stmt: 'continue'
483 ;; return_stmt: 'return' [testlist]
485 : RETURN testlist_opt
496 ;; yield_stmt: 'yield' testlist
504 ;; raise_stmt: 'raise' [test [',' test [',' test]]]
506 : RAISE zero_one_two_or_three_tests
510 ;; [test [',' test [',' test]]]
511 zero_one_two_or_three_tests
513 | test zero_one_or_two_tests
517 ;; [',' test [',' test]]
518 zero_one_or_two_tests
520 | COMMA test zero_or_one_comma_test
525 zero_or_one_comma_test
531 ;;;============================================================================
533 ;;;============================================================================
535 ;; import_stmt : 'import' dotted_as_name (',' dotted_as_name)*
536 ;; | 'from' dotted_name 'import'
537 ;; ('*' | import_as_name (',' import_as_name)*)
539 : IMPORT dotted_as_name_list
541 | FROM dotted_name IMPORT star_or_import_as_name_list
545 ;; dotted_as_name (',' dotted_as_name)*
548 | dotted_as_name_list COMMA dotted_as_name
551 ;; ('*' | import_as_name (',' import_as_name)*)
552 star_or_import_as_name_list
555 | import_as_name_list
559 ;; import_as_name (',' import_as_name)*
563 | import_as_name_list COMMA import_as_name
567 ;; import_as_name: NAME [NAME NAME]
573 ;; dotted_as_name: dotted_name [AS NAME]
575 : dotted_name as_name_opt
585 ;; dotted_name: NAME ('.' NAME)*
588 | dotted_name PERIOD NAME
589 (format "%s.%s" $1 $3)
592 ;;;============================================================================
594 ;;;============================================================================
596 ;; global_stmt: 'global' NAME (',' NAME)*
598 : GLOBAL comma_sep_name_list
605 | comma_sep_name_list COMMA NAME
608 ;;;============================================================================
610 ;;;============================================================================
612 ;; exec_stmt: 'exec' expr ['in' test [',' test]]
614 : EXEC expr exec_trailer
618 ;; ['in' test [',' test]]
621 | IN test comma_test_opt
632 ;;;============================================================================
634 ;;;============================================================================
636 ;; assert_stmt: 'assert' test [',' test]
638 : ASSERT test comma_test_opt
642 ;;;****************************************************************************
644 ;;;****************************************************************************
655 ;;;============================================================================
657 ;;;============================================================================
659 ;; if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
661 : IF test COLON suite elif_suite_pair_list else_suite_pair_opt
665 ;; ('elif' test ':' suite)*
668 | elif_suite_pair_list ELIF test COLON suite
672 ;; ['else' ':' suite]
679 ;; This NT follows the COLON token for most compound statements.
680 ;; suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
684 | NEWLINE indented_block
690 (EXPANDFULL $1 indented_block_body)
702 ;;;============================================================================
704 ;;;============================================================================
706 ;; while_stmt: 'while' test ':' suite ['else' ':' suite]
708 : WHILE test COLON suite else_suite_pair_opt
712 ;;;============================================================================
714 ;;;============================================================================
716 ;; for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
718 : FOR exprlist IN testlist COLON suite else_suite_pair_opt
722 ;;;============================================================================
724 ;;;============================================================================
726 ;; try_stmt: ('try' ':' suite (except_clause ':' suite)+ #diagram:break
727 ;; ['else' ':' suite] | 'try' ':' suite 'finally' ':' suite)
729 : TRY COLON suite except_clause_suite_pair_list else_suite_pair_opt
731 | TRY COLON suite FINALLY COLON suite
735 ;; (except_clause ':' suite)+
736 except_clause_suite_pair_list
737 : except_clause COLON suite
739 | except_clause_suite_pair_list except_clause COLON suite
743 ;; # NB compile.c makes sure that the default except clause is last
744 ;; except_clause: 'except' [test [',' test]]
746 : EXCEPT zero_one_or_two_test
753 | test zero_or_one_comma_test
757 ;;;============================================================================
759 ;;;============================================================================
761 ;; funcdef: 'def' NAME parameters ':' suite
763 : DEF NAME function_parameter_list COLON suite
764 (FUNCTION-TAG $2 nil $3)
767 function_parameter_list
769 (let ((wisent-python-EXPANDING-block t))
770 (EXPANDFULL $1 function_parameters))
773 ;; parameters: '(' [varargslist] ')'
779 | function_parameter COMMA
780 | function_parameter RPAREN
786 ;; (VARIABLE-TAG $1 nil nil)
788 (VARIABLE-TAG $2 nil nil)
790 (VARIABLE-TAG $2 nil nil)
793 ;;;============================================================================
794 ;;;@@ class_declaration
795 ;;;============================================================================
797 ;; classdef: 'class' NAME ['(' testlist ')'] ':' suite
799 : CLASS NAME paren_class_list_opt COLON suite
800 (TYPE-TAG $2 $1 ;; Name "class"
802 (cons $3 nil) ;; (SUPERCLASSES . INTERFACES)
806 ;; ['(' testlist ')']
814 (let ((wisent-python-EXPANDING-block t))
815 (mapcar 'semantic-tag-name (EXPANDFULL $1 paren_classes)))
818 ;; parameters: '(' [varargslist] ')'
825 (VARIABLE-TAG $1 nil nil)
827 (VARIABLE-TAG $1 nil nil)
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".
840 ;;;****************************************************************************
842 ;;;****************************************************************************
844 ;; test: and_test ('or' and_test)* | lambdef
850 ;; and_test ('or' and_test)*
853 | test_test OR and_test
857 ;; and_test: not_test ('and' not_test)*
860 | and_test AND not_test
864 ;; not_test: 'not' not_test | comparison
871 ;; comparison: expr (comp_op expr)*
874 | comparison comp_op expr
878 ;; comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
880 : LT | GT | EQ | GE | LE | LTGT | NE | IN | NOT IN | IS | IS NOT
883 ;; expr: xor_expr ('|' xor_expr)*
890 ;; xor_expr: and_expr ('^' and_expr)*
893 | xor_expr HAT and_expr
897 ;; and_expr: shift_expr ('&' shift_expr)*
900 | and_expr AMP shift_expr
904 ;; shift_expr: arith_expr (('<<'|'>>') arith_expr)*
907 | shift_expr shift_expr_operators arith_expr
917 ;; arith_expr: term (('+'|'-') term)*
920 | arith_expr plus_or_minus term
930 ;; term: factor (('*'|'/'|'%'|'//') factor)*
933 | term term_operator factor
944 ;; factor: ('+'|'-'|'~') factor | power
946 : prefix_operators factor
958 ;; power: atom trailer* ('**' factor)*
960 : atom trailer_zom exponent_zom
962 (if $2 (concat " " $2 " ") "")
963 (if $3 (concat " " $3) "")
969 | trailer_zom trailer
975 | exponent_zom EXPONENT factor
979 ;; trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
989 ;; atom: '(' [testlist] ')' | '[' [listmaker] ']' | '{' [dictmaker] '}'
990 ;; | '`' testlist '`' | NAME | NUMBER | STRING+
998 | BACKQUOTE testlist BACKQUOTE
1002 | one_or_more_string
1011 ;; testlist: test (',' test)* [',']
1013 : comma_sep_test_list comma_opt
1019 | comma_sep_test_list COMMA test
1020 (format "%s, %s" $1 $3)
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.
1027 | one_or_more_string STRING_LITERAL
1031 ;;;****************************************************************************
1033 ;;;****************************************************************************
1035 ;; lambdef: 'lambda' [varargslist] ':' test
1037 : LAMBDA varargslist_opt COLON test
1038 (format "%s %s" $1 (or $2 ""))
1047 ;; varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
1048 ;; | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1050 : fpdef_opt_test_list_comma_zom rest_args
1052 | fpdef_opt_test_list comma_opt
1055 ;; ('*' NAME [',' '**' NAME] | '**' NAME)
1057 : MULT NAME multmult_name_opt
1058 () ;;(VARIABLE-TAG $2 nil nil)
1060 () ;;(VARIABLE-TAG $2 nil nil)
1066 | COMMA EXPONENT NAME
1067 (VARIABLE-TAG $3 nil nil)
1070 fpdef_opt_test_list_comma_zom
1072 | fpdef_opt_test_list_comma_zom fpdef_opt_test COMMA
1076 ;; fpdef ['=' test] (',' fpdef ['=' test])*
1079 | fpdef_opt_test_list COMMA fpdef_opt_test
1088 ;; fpdef: NAME | '(' fplist ')'
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
1098 ;; fplist: fpdef (',' fpdef)* [',']
1100 : fpdef_list comma_opt
1103 ;; fpdef (',' fpdef)*
1106 | fpdef_list COMMA fpdef
1116 ;;;****************************************************************************
1118 ;;;****************************************************************************
1132 ;;; python.wy ends here