python.wy 26 KB

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