annotate Tools/parser/unparse.py @ 77115:352147bbefdb

#14862: Add missing names to os.__all__
author Petri Lehtinen <petri@digip.org>
date Wed, 23 May 2012 21:36:16 +0300
parents 89e928048903
children 5b71f5891c54
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
1 "Usage: unparse.py <path to source file>"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
2 import sys
62623
fe44828ea7ed More unparse.py fixes:
Mark Dickinson <dickinsm@gmail.com>
parents: 62622
diff changeset
3 import math
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
4 import ast
62621
a4e1c56be532 unparse.py: respect coding cookie in input files
Mark Dickinson <dickinsm@gmail.com>
parents: 62614
diff changeset
5 import tokenize
42304
7d16b42fcdbd Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Collin Winter <collinw@gmail.com>
parents: 41554
diff changeset
6 import io
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
7 import os
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
8
62651
043ef04f64d2 Unparse infinite imaginary literals correctly. Add some more numeric tests.
Mark Dickinson <dickinsm@gmail.com>
parents: 62650
diff changeset
9 # Large float and imaginary literals get turned into infinities in the AST.
043ef04f64d2 Unparse infinite imaginary literals correctly. Add some more numeric tests.
Mark Dickinson <dickinsm@gmail.com>
parents: 62650
diff changeset
10 # We unparse those infinities to INFSTR.
043ef04f64d2 Unparse infinite imaginary literals correctly. Add some more numeric tests.
Mark Dickinson <dickinsm@gmail.com>
parents: 62650
diff changeset
11 INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1)
043ef04f64d2 Unparse infinite imaginary literals correctly. Add some more numeric tests.
Mark Dickinson <dickinsm@gmail.com>
parents: 62650
diff changeset
12
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
13 def interleave(inter, f, seq):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
14 """Call f on each item in seq, calling inter() in between.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
15 """
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
16 seq = iter(seq)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
17 try:
42304
7d16b42fcdbd Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Collin Winter <collinw@gmail.com>
parents: 41554
diff changeset
18 f(next(seq))
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
19 except StopIteration:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
20 pass
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
21 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
22 for x in seq:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
23 inter()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
24 f(x)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
25
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
26 class Unparser:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
27 """Methods in this class recursively traverse an AST and
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
28 output source code for the abstract syntax; original formatting
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
29 is disregarded. """
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
30
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
31 def __init__(self, tree, file = sys.stdout):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
32 """Unparser(tree, file=sys.stdout) -> None.
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
33 Print the source for tree to file."""
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
34 self.f = file
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
35 self._indent = 0
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
36 self.dispatch(tree)
42304
7d16b42fcdbd Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Collin Winter <collinw@gmail.com>
parents: 41554
diff changeset
37 print("", file=self.f)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
38 self.f.flush()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
39
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
40 def fill(self, text = ""):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
41 "Indent a piece of text, according to the current indentation level"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
42 self.f.write("\n"+" "*self._indent + text)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
43
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
44 def write(self, text):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
45 "Append a piece of text to the current line."
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
46 self.f.write(text)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
47
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
48 def enter(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
49 "Print ':', and increase the indentation."
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
50 self.write(":")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
51 self._indent += 1
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
52
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
53 def leave(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
54 "Decrease the indentation level."
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
55 self._indent -= 1
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
56
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
57 def dispatch(self, tree):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
58 "Dispatcher function, dispatching tree type T to method _T."
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
59 if isinstance(tree, list):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
60 for t in tree:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
61 self.dispatch(t)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
62 return
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
63 meth = getattr(self, "_"+tree.__class__.__name__)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
64 meth(tree)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
65
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
66
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
67 ############### Unparsing methods ######################
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
68 # There should be one method per concrete grammar type #
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
69 # Constructors should be grouped by sum type. Ideally, #
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
70 # this would follow the order in the grammar, but #
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
71 # currently doesn't. #
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
72 ########################################################
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
73
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
74 def _Module(self, tree):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
75 for stmt in tree.body:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
76 self.dispatch(stmt)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
77
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
78 # stmt
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
79 def _Expr(self, tree):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
80 self.fill()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
81 self.dispatch(tree.value)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
82
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
83 def _Import(self, t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
84 self.fill("import ")
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
85 interleave(lambda: self.write(", "), self.dispatch, t.names)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
86
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
87 def _ImportFrom(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
88 self.fill("from ")
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
89 self.write("." * t.level)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
90 if t.module:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
91 self.write(t.module)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
92 self.write(" import ")
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
93 interleave(lambda: self.write(", "), self.dispatch, t.names)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
94
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
95 def _Assign(self, t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
96 self.fill()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
97 for target in t.targets:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
98 self.dispatch(target)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
99 self.write(" = ")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
100 self.dispatch(t.value)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
101
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
102 def _AugAssign(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
103 self.fill()
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
104 self.dispatch(t.target)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
105 self.write(" "+self.binop[t.op.__class__.__name__]+"= ")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
106 self.dispatch(t.value)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
107
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
108 def _Return(self, t):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
109 self.fill("return")
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
110 if t.value:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
111 self.write(" ")
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
112 self.dispatch(t.value)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
113
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
114 def _Pass(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
115 self.fill("pass")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
116
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
117 def _Break(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
118 self.fill("break")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
119
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
120 def _Continue(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
121 self.fill("continue")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
122
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
123 def _Delete(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
124 self.fill("del ")
62609
68b670f3614e Merged revisions 82338,82340-82341 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48771
diff changeset
125 interleave(lambda: self.write(", "), self.dispatch, t.targets)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
126
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
127 def _Assert(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
128 self.fill("assert ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
129 self.dispatch(t.test)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
130 if t.msg:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
131 self.write(", ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
132 self.dispatch(t.msg)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
133
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
134 def _Global(self, t):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
135 self.fill("global ")
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
136 interleave(lambda: self.write(", "), self.write, t.names)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
137
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
138 def _Nonlocal(self, t):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
139 self.fill("nonlocal ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
140 interleave(lambda: self.write(", "), self.write, t.names)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
141
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
142 def _Yield(self, t):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
143 self.write("(")
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
144 self.write("yield")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
145 if t.value:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
146 self.write(" ")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
147 self.dispatch(t.value)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
148 self.write(")")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
149
76803
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
150 def _YieldFrom(self, t):
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
151 self.write("(")
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
152 self.write("yield from")
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
153 if t.value:
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
154 self.write(" ")
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
155 self.dispatch(t.value)
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
156 self.write(")")
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
157
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
158 def _Raise(self, t):
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
159 self.fill("raise")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
160 if not t.exc:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
161 assert not t.cause
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
162 return
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
163 self.write(" ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
164 self.dispatch(t.exc)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
165 if t.cause:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
166 self.write(" from ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
167 self.dispatch(t.cause)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
168
76803
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
169 def _Try(self, t):
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
170 self.fill("try")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
171 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
172 self.dispatch(t.body)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
173 self.leave()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
174 for ex in t.handlers:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
175 self.dispatch(ex)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
176 if t.orelse:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
177 self.fill("else")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
178 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
179 self.dispatch(t.orelse)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
180 self.leave()
76803
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
181 if t.finalbody:
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
182 self.fill("finally")
62644
e6bb990ac9f6 Output try-except-finally statements where appropriate.
Mark Dickinson <dickinsm@gmail.com>
parents: 62643
diff changeset
183 self.enter()
76803
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
184 self.dispatch(t.finalbody)
62644
e6bb990ac9f6 Output try-except-finally statements where appropriate.
Mark Dickinson <dickinsm@gmail.com>
parents: 62643
diff changeset
185 self.leave()
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
186
48771
ea3d13cd7c1e Merged revisions 65780,65782,65785,65809,65812,65834,65846,65859,65861 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 42304
diff changeset
187 def _ExceptHandler(self, t):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
188 self.fill("except")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
189 if t.type:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
190 self.write(" ")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
191 self.dispatch(t.type)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
192 if t.name:
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
193 self.write(" as ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
194 self.write(t.name)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
195 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
196 self.dispatch(t.body)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
197 self.leave()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
198
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
199 def _ClassDef(self, t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
200 self.write("\n")
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
201 for deco in t.decorator_list:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
202 self.fill("@")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
203 self.dispatch(deco)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
204 self.fill("class "+t.name)
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
205 self.write("(")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
206 comma = False
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
207 for e in t.bases:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
208 if comma: self.write(", ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
209 else: comma = True
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
210 self.dispatch(e)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
211 for e in t.keywords:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
212 if comma: self.write(", ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
213 else: comma = True
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
214 self.dispatch(e)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
215 if t.starargs:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
216 if comma: self.write(", ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
217 else: comma = True
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
218 self.write("*")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
219 self.dispatch(t.starargs)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
220 if t.kwargs:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
221 if comma: self.write(", ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
222 else: comma = True
62637
158166ef6f06 Fix typo in unparsing of a class definition.
Mark Dickinson <dickinsm@gmail.com>
parents: 62623
diff changeset
223 self.write("**")
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
224 self.dispatch(t.kwargs)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
225 self.write(")")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
226
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
227 self.enter()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
228 self.dispatch(t.body)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
229 self.leave()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
230
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
231 def _FunctionDef(self, t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
232 self.write("\n")
48771
ea3d13cd7c1e Merged revisions 65780,65782,65785,65809,65812,65834,65846,65859,65861 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 42304
diff changeset
233 for deco in t.decorator_list:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
234 self.fill("@")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
235 self.dispatch(deco)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
236 self.fill("def "+t.name + "(")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
237 self.dispatch(t.args)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
238 self.write(")")
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
239 if t.returns:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
240 self.write(" -> ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
241 self.dispatch(t.returns)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
242 self.enter()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
243 self.dispatch(t.body)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
244 self.leave()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
245
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
246 def _For(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
247 self.fill("for ")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
248 self.dispatch(t.target)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
249 self.write(" in ")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
250 self.dispatch(t.iter)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
251 self.enter()
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
252 self.dispatch(t.body)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
253 self.leave()
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
254 if t.orelse:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
255 self.fill("else")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
256 self.enter()
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
257 self.dispatch(t.orelse)
62609
68b670f3614e Merged revisions 82338,82340-82341 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48771
diff changeset
258 self.leave()
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
259
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
260 def _If(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
261 self.fill("if ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
262 self.dispatch(t.test)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
263 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
264 self.dispatch(t.body)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
265 self.leave()
62643
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
266 # collapse nested ifs into equivalent elifs.
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
267 while (t.orelse and len(t.orelse) == 1 and
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
268 isinstance(t.orelse[0], ast.If)):
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
269 t = t.orelse[0]
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
270 self.fill("elif ")
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
271 self.dispatch(t.test)
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
272 self.enter()
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
273 self.dispatch(t.body)
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
274 self.leave()
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
275 # final else
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
276 if t.orelse:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
277 self.fill("else")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
278 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
279 self.dispatch(t.orelse)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
280 self.leave()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
281
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
282 def _While(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
283 self.fill("while ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
284 self.dispatch(t.test)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
285 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
286 self.dispatch(t.body)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
287 self.leave()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
288 if t.orelse:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
289 self.fill("else")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
290 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
291 self.dispatch(t.orelse)
62609
68b670f3614e Merged revisions 82338,82340-82341 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48771
diff changeset
292 self.leave()
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
293
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
294 def _With(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
295 self.fill("with ")
76803
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
296 interleave(lambda: self.write(", "), self.dispatch, t.items)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
297 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
298 self.dispatch(t.body)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
299 self.leave()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
300
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
301 # expr
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
302 def _Bytes(self, t):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
303 self.write(repr(t.s))
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
304
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
305 def _Str(self, tree):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
306 self.write(repr(tree.s))
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
307
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
308 def _Name(self, t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
309 self.write(t.id)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
310
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
311 def _Num(self, t):
62651
043ef04f64d2 Unparse infinite imaginary literals correctly. Add some more numeric tests.
Mark Dickinson <dickinsm@gmail.com>
parents: 62650
diff changeset
312 # Substitute overflowing decimal literal for AST infinities.
043ef04f64d2 Unparse infinite imaginary literals correctly. Add some more numeric tests.
Mark Dickinson <dickinsm@gmail.com>
parents: 62650
diff changeset
313 self.write(repr(t.n).replace("inf", INFSTR))
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
314
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
315 def _List(self, t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
316 self.write("[")
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
317 interleave(lambda: self.write(", "), self.dispatch, t.elts)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
318 self.write("]")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
319
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
320 def _ListComp(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
321 self.write("[")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
322 self.dispatch(t.elt)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
323 for gen in t.generators:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
324 self.dispatch(gen)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
325 self.write("]")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
326
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
327 def _GeneratorExp(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
328 self.write("(")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
329 self.dispatch(t.elt)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
330 for gen in t.generators:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
331 self.dispatch(gen)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
332 self.write(")")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
333
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
334 def _SetComp(self, t):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
335 self.write("{")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
336 self.dispatch(t.elt)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
337 for gen in t.generators:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
338 self.dispatch(gen)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
339 self.write("}")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
340
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
341 def _DictComp(self, t):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
342 self.write("{")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
343 self.dispatch(t.key)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
344 self.write(": ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
345 self.dispatch(t.value)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
346 for gen in t.generators:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
347 self.dispatch(gen)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
348 self.write("}")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
349
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
350 def _comprehension(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
351 self.write(" for ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
352 self.dispatch(t.target)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
353 self.write(" in ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
354 self.dispatch(t.iter)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
355 for if_clause in t.ifs:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
356 self.write(" if ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
357 self.dispatch(if_clause)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
358
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
359 def _IfExp(self, t):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
360 self.write("(")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
361 self.dispatch(t.body)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
362 self.write(" if ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
363 self.dispatch(t.test)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
364 self.write(" else ")
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
365 self.dispatch(t.orelse)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
366 self.write(")")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
367
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
368 def _Set(self, t):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
369 assert(t.elts) # should be at least one element
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
370 self.write("{")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
371 interleave(lambda: self.write(", "), self.dispatch, t.elts)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
372 self.write("}")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
373
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
374 def _Dict(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
375 self.write("{")
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
376 def write_pair(pair):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
377 (k, v) = pair
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
378 self.dispatch(k)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
379 self.write(": ")
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
380 self.dispatch(v)
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
381 interleave(lambda: self.write(", "), write_pair, zip(t.keys, t.values))
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
382 self.write("}")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
383
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
384 def _Tuple(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
385 self.write("(")
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
386 if len(t.elts) == 1:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
387 (elt,) = t.elts
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
388 self.dispatch(elt)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
389 self.write(",")
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
390 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
391 interleave(lambda: self.write(", "), self.dispatch, t.elts)
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
392 self.write(")")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
393
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
394 unop = {"Invert":"~", "Not": "not", "UAdd":"+", "USub":"-"}
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
395 def _UnaryOp(self, t):
62609
68b670f3614e Merged revisions 82338,82340-82341 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48771
diff changeset
396 self.write("(")
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
397 self.write(self.unop[t.op.__class__.__name__])
62609
68b670f3614e Merged revisions 82338,82340-82341 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48771
diff changeset
398 self.write(" ")
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
399 self.dispatch(t.operand)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
400 self.write(")")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
401
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
402 binop = { "Add":"+", "Sub":"-", "Mult":"*", "Div":"/", "Mod":"%",
62609
68b670f3614e Merged revisions 82338,82340-82341 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48771
diff changeset
403 "LShift":"<<", "RShift":">>", "BitOr":"|", "BitXor":"^", "BitAnd":"&",
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
404 "FloorDiv":"//", "Pow": "**"}
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
405 def _BinOp(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
406 self.write("(")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
407 self.dispatch(t.left)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
408 self.write(" " + self.binop[t.op.__class__.__name__] + " ")
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
409 self.dispatch(t.right)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
410 self.write(")")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
411
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
412 cmpops = {"Eq":"==", "NotEq":"!=", "Lt":"<", "LtE":"<=", "Gt":">", "GtE":">=",
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
413 "Is":"is", "IsNot":"is not", "In":"in", "NotIn":"not in"}
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
414 def _Compare(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
415 self.write("(")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
416 self.dispatch(t.left)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
417 for o, e in zip(t.ops, t.comparators):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
418 self.write(" " + self.cmpops[o.__class__.__name__] + " ")
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
419 self.dispatch(e)
62612
69f6d2130281 Merged revisions 82345 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62609
diff changeset
420 self.write(")")
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
421
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
422 boolops = {ast.And: 'and', ast.Or: 'or'}
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
423 def _BoolOp(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
424 self.write("(")
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
425 s = " %s " % self.boolops[t.op.__class__]
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
426 interleave(lambda: self.write(s), self.dispatch, t.values)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
427 self.write(")")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
428
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
429 def _Attribute(self,t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
430 self.dispatch(t.value)
62645
4fce25abfdd8 Better solution for attribute access on integer literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 62644
diff changeset
431 # Special case: 3.__abs__() is a syntax error, so if t.value
4fce25abfdd8 Better solution for attribute access on integer literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 62644
diff changeset
432 # is an integer literal then we need to either parenthesize
4fce25abfdd8 Better solution for attribute access on integer literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 62644
diff changeset
433 # it or add an extra space to get 3 .__abs__().
4fce25abfdd8 Better solution for attribute access on integer literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 62644
diff changeset
434 if isinstance(t.value, ast.Num) and isinstance(t.value.n, int):
4fce25abfdd8 Better solution for attribute access on integer literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 62644
diff changeset
435 self.write(" ")
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
436 self.write(".")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
437 self.write(t.attr)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
438
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
439 def _Call(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
440 self.dispatch(t.func)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
441 self.write("(")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
442 comma = False
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
443 for e in t.args:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
444 if comma: self.write(", ")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
445 else: comma = True
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
446 self.dispatch(e)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
447 for e in t.keywords:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
448 if comma: self.write(", ")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
449 else: comma = True
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
450 self.dispatch(e)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
451 if t.starargs:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
452 if comma: self.write(", ")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
453 else: comma = True
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
454 self.write("*")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
455 self.dispatch(t.starargs)
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
456 if t.kwargs:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
457 if comma: self.write(", ")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
458 else: comma = True
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
459 self.write("**")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
460 self.dispatch(t.kwargs)
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
461 self.write(")")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
462
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
463 def _Subscript(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
464 self.dispatch(t.value)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
465 self.write("[")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
466 self.dispatch(t.slice)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
467 self.write("]")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
468
76802
c80576303892 Issue #14965: Fix missing support for starred assignments in Tools/parser/unparse.py.
Mark Dickinson <mdickinson@enthought.com>
parents: 67221
diff changeset
469 def _Starred(self, t):
c80576303892 Issue #14965: Fix missing support for starred assignments in Tools/parser/unparse.py.
Mark Dickinson <mdickinson@enthought.com>
parents: 67221
diff changeset
470 self.write("*")
c80576303892 Issue #14965: Fix missing support for starred assignments in Tools/parser/unparse.py.
Mark Dickinson <mdickinson@enthought.com>
parents: 67221
diff changeset
471 self.dispatch(t.value)
c80576303892 Issue #14965: Fix missing support for starred assignments in Tools/parser/unparse.py.
Mark Dickinson <mdickinson@enthought.com>
parents: 67221
diff changeset
472
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
473 # slice
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
474 def _Ellipsis(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
475 self.write("...")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
476
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
477 def _Index(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
478 self.dispatch(t.value)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
479
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
480 def _Slice(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
481 if t.lower:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
482 self.dispatch(t.lower)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
483 self.write(":")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
484 if t.upper:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
485 self.dispatch(t.upper)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
486 if t.step:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
487 self.write(":")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
488 self.dispatch(t.step)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
489
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
490 def _ExtSlice(self, t):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
491 interleave(lambda: self.write(', '), self.dispatch, t.dims)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
492
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
493 # argument
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
494 def _arg(self, t):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
495 self.write(t.arg)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
496 if t.annotation:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
497 self.write(": ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
498 self.dispatch(t.annotation)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
499
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
500 # others
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
501 def _arguments(self, t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
502 first = True
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
503 # normal arguments
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
504 defaults = [None] * (len(t.args) - len(t.defaults)) + t.defaults
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
505 for a, d in zip(t.args, defaults):
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
506 if first:first = False
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
507 else: self.write(", ")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
508 self.dispatch(a)
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
509 if d:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
510 self.write("=")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
511 self.dispatch(d)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
512
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
513 # varargs, or bare '*' if no varargs but keyword-only arguments present
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
514 if t.vararg or t.kwonlyargs:
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
515 if first:first = False
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
516 else: self.write(", ")
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
517 self.write("*")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
518 if t.vararg:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
519 self.write(t.vararg)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
520 if t.varargannotation:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
521 self.write(": ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
522 self.dispatch(t.varargannotation)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
523
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
524 # keyword-only arguments
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
525 if t.kwonlyargs:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
526 for a, d in zip(t.kwonlyargs, t.kw_defaults):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
527 if first:first = False
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
528 else: self.write(", ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
529 self.dispatch(a),
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
530 if d:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
531 self.write("=")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
532 self.dispatch(d)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
533
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
534 # kwargs
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
535 if t.kwarg:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
536 if first:first = False
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
537 else: self.write(", ")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
538 self.write("**"+t.kwarg)
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
539 if t.kwargannotation:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
540 self.write(": ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
541 self.dispatch(t.kwargannotation)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
542
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
543 def _keyword(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
544 self.write(t.arg)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
545 self.write("=")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
546 self.dispatch(t.value)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
547
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
548 def _Lambda(self, t):
62623
fe44828ea7ed More unparse.py fixes:
Mark Dickinson <dickinsm@gmail.com>
parents: 62622
diff changeset
549 self.write("(")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
550 self.write("lambda ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
551 self.dispatch(t.args)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
552 self.write(": ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
553 self.dispatch(t.body)
62623
fe44828ea7ed More unparse.py fixes:
Mark Dickinson <dickinsm@gmail.com>
parents: 62622
diff changeset
554 self.write(")")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
555
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
556 def _alias(self, t):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
557 self.write(t.name)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
558 if t.asname:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
559 self.write(" as "+t.asname)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
560
76803
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
561 def _withitem(self, t):
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
562 self.dispatch(t.context_expr)
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
563 if t.optional_vars:
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
564 self.write(" as ")
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
565 self.dispatch(t.optional_vars)
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
566
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
567 def roundtrip(filename, output=sys.stdout):
62621
a4e1c56be532 unparse.py: respect coding cookie in input files
Mark Dickinson <dickinsm@gmail.com>
parents: 62614
diff changeset
568 with open(filename, "rb") as pyfile:
a4e1c56be532 unparse.py: respect coding cookie in input files
Mark Dickinson <dickinsm@gmail.com>
parents: 62614
diff changeset
569 encoding = tokenize.detect_encoding(pyfile.readline)[0]
a4e1c56be532 unparse.py: respect coding cookie in input files
Mark Dickinson <dickinsm@gmail.com>
parents: 62614
diff changeset
570 with open(filename, "r", encoding=encoding) as pyfile:
a4e1c56be532 unparse.py: respect coding cookie in input files
Mark Dickinson <dickinsm@gmail.com>
parents: 62614
diff changeset
571 source = pyfile.read()
62614
95f8b25e53ce unparse.py: Typo fix.
Mark Dickinson <dickinsm@gmail.com>
parents: 62613
diff changeset
572 tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
573 Unparser(tree, output)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
574
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
575
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
576
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
577 def testdir(a):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
578 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
579 names = [n for n in os.listdir(a) if n.endswith('.py')]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
580 except OSError:
42304
7d16b42fcdbd Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Collin Winter <collinw@gmail.com>
parents: 41554
diff changeset
581 print("Directory not readable: %s" % a, file=sys.stderr)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
582 else:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
583 for n in names:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
584 fullname = os.path.join(a, n)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
585 if os.path.isfile(fullname):
42304
7d16b42fcdbd Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Collin Winter <collinw@gmail.com>
parents: 41554
diff changeset
586 output = io.StringIO()
7d16b42fcdbd Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Collin Winter <collinw@gmail.com>
parents: 41554
diff changeset
587 print('Testing %s' % fullname)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
588 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
589 roundtrip(fullname, output)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
590 except Exception as e:
42304
7d16b42fcdbd Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Collin Winter <collinw@gmail.com>
parents: 41554
diff changeset
591 print(' Failed to compile, exception is %s' % repr(e))
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
592 elif os.path.isdir(fullname):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
593 testdir(fullname)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
594
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
595 def main(args):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
596 if args[0] == '--testdir':
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
597 for a in args[1:]:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
598 testdir(a)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
599 else:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
600 for a in args:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
601 roundtrip(a)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
602
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
603 if __name__=='__main__':
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
604 main(sys.argv[1:])