FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
jython/ast/asdl_java.py at v2.7.4b2 · jython/jython · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
jython
/
jython
Public
Notifications
You must be signed in to change notification settings
Fork
231
Star
1.5k
Code
Issues
94
Pull requests
5
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
jython
/
ast
/
asdl_java.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
360 lines (300 loc) · 11.8 KB
Breadcrumbs
jython
/
ast
/
asdl_java.py
Copy path
File metadata and controls
360 lines (300 loc) · 11.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"""Generate Java code from an ASDL description."""
# TO DO
# handle fields that have a type but no name
import
os
,
sys
,
traceback
import
asdl
TABSIZE
=
4
MAX_COL
=
76
def
reflow_lines
(
s
,
depth
):
"""Reflow the line s indented depth tabs.
Return a sequence of lines where no line extends beyond MAX_COL
when properly indented. The first line is properly indented based
exclusively on depth * TABSIZE. All following lines -- these are
the reflowed lines generated by this function -- start at the same
column as the first character beyond the opening { in the first
line.
"""
size
=
MAX_COL
-
depth
*
TABSIZE
if
len
(
s
)
<
size
:
return
[
s
]
lines
=
[]
cur
=
s
padding
=
""
while
len
(
cur
)
>
size
:
i
=
cur
.
rfind
(
' '
,
0
,
size
)
assert
i
!=
-
1
,
"Impossible line to reflow: %s"
%
`s`
lines
.
append
(
padding
+
cur
[:
i
])
if
len
(
lines
)
==
1
:
# find new size based on brace
j
=
cur
.
find
(
'{'
,
0
,
i
)
if
j
>=
0
:
j
+=
2
# account for the brace and the space after it
size
-=
j
padding
=
" "
*
j
cur
=
cur
[
i
+
1
:]
else
:
lines
.
append
(
padding
+
cur
)
return
lines
class
EmitVisitor
(
asdl
.
VisitorBase
):
"""Visit that emits lines"""
def
__init__
(
self
):
super
(
EmitVisitor
,
self
).
__init__
()
def
open
(
self
,
name
,
refersToSimpleNode
=
1
,
useDataOutput
=
0
):
self
.
file
=
open
(
"%s.java"
%
name
,
"w"
)
print
>>
self
.
file
,
"// Autogenerated AST node"
print
>>
self
.
file
,
'package org.python.parser.ast;'
if
refersToSimpleNode
:
print
>>
self
.
file
,
'import org.python.parser.SimpleNode;'
if
useDataOutput
:
print
>>
self
.
file
,
'import java.io.DataOutputStream;'
print
>>
self
.
file
,
'import java.io.IOException;'
print
>>
self
.
file
def
close
(
self
):
self
.
file
.
close
()
def
emit
(
self
,
s
,
depth
):
# XXX reflow long lines?
lines
=
reflow_lines
(
s
,
depth
)
for
line
in
lines
:
line
=
(
" "
*
TABSIZE
*
depth
)
+
line
+
"
\n
"
self
.
file
.
write
(
line
)
# This step will add a 'simple' boolean attribute to all Sum and Product
# nodes and add a 'typedef' link to each Field node that points to the
# Sum or Product node that defines the field.
class
AnalyzeVisitor
(
EmitVisitor
):
index
=
0
def
makeIndex
(
self
):
self
.
index
+=
1
return
self
.
index
def
visitModule
(
self
,
mod
):
self
.
types
=
{}
for
dfn
in
mod
.
dfns
:
self
.
types
[
str
(
dfn
.
name
)]
=
dfn
.
value
for
dfn
in
mod
.
dfns
:
self
.
visit
(
dfn
)
def
visitType
(
self
,
type
,
depth
=
0
):
self
.
visit
(
type
.
value
,
type
.
name
,
depth
)
def
visitSum
(
self
,
sum
,
name
,
depth
):
sum
.
simple
=
1
for
t
in
sum
.
types
:
if
t
.
fields
:
sum
.
simple
=
0
break
for
t
in
sum
.
types
:
if
not
sum
.
simple
:
t
.
index
=
self
.
makeIndex
()
self
.
visit
(
t
,
name
,
depth
)
def
visitProduct
(
self
,
product
,
name
,
depth
):
product
.
simple
=
0
product
.
index
=
self
.
makeIndex
()
for
f
in
product
.
fields
:
self
.
visit
(
f
,
depth
+
1
)
def
visitConstructor
(
self
,
cons
,
name
,
depth
):
for
f
in
cons
.
fields
:
self
.
visit
(
f
,
depth
+
1
)
def
visitField
(
self
,
field
,
depth
):
field
.
typedef
=
self
.
types
.
get
(
str
(
field
.
type
))
# The code generator itself.
#
class
JavaVisitor
(
EmitVisitor
):
def
visitModule
(
self
,
mod
):
for
dfn
in
mod
.
dfns
:
self
.
visit
(
dfn
)
def
visitType
(
self
,
type
,
depth
=
0
):
self
.
visit
(
type
.
value
,
type
.
name
,
depth
)
def
visitSum
(
self
,
sum
,
name
,
depth
):
if
sum
.
simple
:
self
.
simple_sum
(
sum
,
name
,
depth
)
else
:
self
.
sum_with_constructor
(
sum
,
name
,
depth
)
def
simple_sum
(
self
,
sum
,
name
,
depth
):
self
.
open
(
"%sType"
%
name
,
refersToSimpleNode
=
0
)
self
.
emit
(
"public interface %(name)sType {"
%
locals
(),
depth
)
for
i
in
range
(
len
(
sum
.
types
)):
type
=
sum
.
types
[
i
]
self
.
emit
(
"public static final int %s = %d;"
%
(
type
.
name
,
i
+
1
),
depth
+
1
)
self
.
emit
(
""
,
0
)
self
.
emit
(
"public static final String[] %sTypeNames = new String[] {"
%
name
,
depth
+
1
)
self
.
emit
(
'"<undef>",'
,
depth
+
2
)
for
type
in
sum
.
types
:
self
.
emit
(
'"%s",'
%
type
.
name
,
depth
+
2
)
self
.
emit
(
"};"
,
depth
+
1
)
self
.
emit
(
"}"
,
depth
)
self
.
close
()
def
sum_with_constructor
(
self
,
sum
,
name
,
depth
):
self
.
open
(
"%sType"
%
name
)
self
.
emit
(
"public abstract class %(name)sType extends SimpleNode {"
%
locals
(),
depth
)
self
.
emit
(
"}"
,
depth
)
self
.
close
()
for
t
in
sum
.
types
:
self
.
visit
(
t
,
name
,
depth
)
def
visitProduct
(
self
,
product
,
name
,
depth
):
self
.
open
(
"%sType"
%
name
,
useDataOutput
=
1
)
self
.
emit
(
"public class %(name)sType extends SimpleNode {"
%
locals
(),
depth
)
for
f
in
product
.
fields
:
self
.
visit
(
f
,
depth
+
1
)
self
.
emit
(
""
,
depth
)
self
.
javaMethods
(
product
,
name
,
"%sType"
%
name
,
product
.
fields
,
depth
+
1
)
self
.
emit
(
"}"
,
depth
)
self
.
close
()
def
visitConstructor
(
self
,
cons
,
name
,
depth
):
self
.
open
(
cons
.
name
,
useDataOutput
=
1
)
enums
=
[]
for
f
in
cons
.
fields
:
if
f
.
typedef
and
f
.
typedef
.
simple
:
enums
.
append
(
"%sType"
%
f
.
type
)
if
enums
:
s
=
"implements %s "
%
", "
.
join
(
enums
)
else
:
s
=
""
self
.
emit
(
"public class %s extends %sType %s{"
%
(
cons
.
name
,
name
,
s
),
depth
)
for
f
in
cons
.
fields
:
self
.
visit
(
f
,
depth
+
1
)
self
.
emit
(
""
,
depth
)
self
.
javaMethods
(
cons
,
cons
.
name
,
cons
.
name
,
cons
.
fields
,
depth
+
1
)
self
.
emit
(
"}"
,
depth
)
self
.
close
()
def
javaMethods
(
self
,
type
,
clsname
,
ctorname
,
fields
,
depth
):
# The java ctors
fpargs
=
", "
.
join
([
self
.
fieldDef
(
f
)
for
f
in
fields
])
self
.
emit
(
"public %s(%s) {"
%
(
ctorname
,
fpargs
),
depth
)
for
f
in
fields
:
self
.
emit
(
"this.%s = %s;"
%
(
f
.
name
,
f
.
name
),
depth
+
1
)
self
.
emit
(
"}"
,
depth
)
self
.
emit
(
""
,
0
)
if
fpargs
:
fpargs
+=
", "
self
.
emit
(
"public %s(%sSimpleNode parent) {"
%
(
ctorname
,
fpargs
),
depth
)
self
.
emit
(
"this(%s);"
%
", "
.
join
([
str
(
f
.
name
)
for
f
in
fields
]),
depth
+
1
)
self
.
emit
(
"this.beginLine = parent.beginLine;"
,
depth
+
1
);
self
.
emit
(
"this.beginColumn = parent.beginColumn;"
,
depth
+
1
);
self
.
emit
(
"}"
,
depth
)
self
.
emit
(
""
,
0
)
# The toString() method
self
.
emit
(
"public String toString() {"
,
depth
)
self
.
emit
(
'StringBuffer sb = new StringBuffer("%s[");'
%
clsname
,
depth
+
1
)
for
f
in
fields
:
self
.
emit
(
'sb.append("%s=");'
%
f
.
name
,
depth
+
1
)
if
not
self
.
bltinnames
.
has_key
(
str
(
f
.
type
))
and
f
.
typedef
.
simple
:
self
.
emit
(
"sb.append(dumpThis(this.%s, %sType.%sTypeNames));"
%
(
f
.
name
,
f
.
type
,
f
.
type
),
depth
+
1
)
else
:
self
.
emit
(
"sb.append(dumpThis(this.%s));"
%
f
.
name
,
depth
+
1
)
if
f
!=
fields
[
-
1
]:
self
.
emit
(
'sb.append(", ");'
,
depth
+
1
)
self
.
emit
(
'sb.append("]");'
,
depth
+
1
)
self
.
emit
(
"return sb.toString();"
,
depth
+
1
)
self
.
emit
(
"}"
,
depth
)
self
.
emit
(
""
,
0
)
# The pickle() method
self
.
emit
(
"public void pickle(DataOutputStream ostream) throws IOException {"
,
depth
)
self
.
emit
(
"pickleThis(%s, ostream);"
%
type
.
index
,
depth
+
1
);
for
f
in
fields
:
self
.
emit
(
"pickleThis(this.%s, ostream);"
%
f
.
name
,
depth
+
1
)
self
.
emit
(
"}"
,
depth
)
self
.
emit
(
""
,
0
)
# The accept() method
self
.
emit
(
"public Object accept(VisitorIF visitor) throws Exception {"
,
depth
)
if
clsname
==
ctorname
:
self
.
emit
(
'return visitor.visit%s(this);'
%
clsname
,
depth
+
1
)
else
:
self
.
emit
(
'traverse(visitor);'
%
clsname
,
depth
+
1
)
self
.
emit
(
'return null;'
%
clsname
,
depth
+
1
)
self
.
emit
(
"}"
,
depth
)
self
.
emit
(
""
,
0
)
# The visitChildren() method
self
.
emit
(
"public void traverse(VisitorIF visitor) throws Exception {"
,
depth
)
for
f
in
fields
:
if
self
.
bltinnames
.
has_key
(
str
(
f
.
type
)):
continue
if
f
.
typedef
.
simple
:
continue
if
f
.
seq
:
self
.
emit
(
'if (%s != null) {'
%
f
.
name
,
depth
+
1
)
self
.
emit
(
'for (int i = 0; i < %s.length; i++) {'
%
f
.
name
,
depth
+
2
)
self
.
emit
(
'if (%s[i] != null)'
%
f
.
name
,
depth
+
3
)
self
.
emit
(
'%s[i].accept(visitor);'
%
f
.
name
,
depth
+
4
)
self
.
emit
(
'}'
,
depth
+
2
)
self
.
emit
(
'}'
,
depth
+
1
)
else
:
self
.
emit
(
'if (%s != null)'
%
f
.
name
,
depth
+
1
)
self
.
emit
(
'%s.accept(visitor);'
%
f
.
name
,
depth
+
2
)
self
.
emit
(
'}'
,
depth
)
self
.
emit
(
""
,
0
)
def
visitField
(
self
,
field
,
depth
):
self
.
emit
(
"public %s;"
%
self
.
fieldDef
(
field
),
depth
)
bltinnames
=
{
'int'
:
'int'
,
'bool'
:
'boolean'
,
'identifier'
:
'String'
,
'string'
:
'String'
,
'object'
:
'Object'
,
# was PyObject
}
def
fieldDef
(
self
,
field
):
jtype
=
str
(
field
.
type
)
if
field
.
typedef
and
field
.
typedef
.
simple
:
jtype
=
'int'
else
:
jtype
=
self
.
bltinnames
.
get
(
jtype
,
jtype
+
'Type'
)
name
=
field
.
name
seq
=
field
.
seq
and
"[]"
or
""
return
"%(jtype)s%(seq)s %(name)s"
%
locals
()
class
VisitorVisitor
(
EmitVisitor
):
def
__init__
(
self
):
EmitVisitor
.
__init__
(
self
)
self
.
ctors
=
[]
def
visitModule
(
self
,
mod
):
for
dfn
in
mod
.
dfns
:
self
.
visit
(
dfn
)
self
.
open
(
"VisitorIF"
,
refersToSimpleNode
=
0
)
self
.
emit
(
'public interface VisitorIF {'
,
0
)
for
ctor
in
self
.
ctors
:
self
.
emit
(
"public Object visit%s(%s node) throws Exception;"
%
(
ctor
,
ctor
),
1
)
self
.
emit
(
'}'
,
0
)
self
.
close
()
self
.
open
(
"VisitorBase"
)
self
.
emit
(
'public abstract class VisitorBase implements VisitorIF {'
,
0
)
for
ctor
in
self
.
ctors
:
self
.
emit
(
"public Object visit%s(%s node) throws Exception {"
%
(
ctor
,
ctor
),
1
)
self
.
emit
(
"Object ret = unhandled_node(node);"
,
2
)
self
.
emit
(
"traverse(node);"
,
2
)
self
.
emit
(
"return ret;"
,
2
)
self
.
emit
(
'}'
,
1
)
self
.
emit
(
''
,
0
)
self
.
emit
(
"abstract protected Object unhandled_node(SimpleNode node) throws Exception;"
,
1
)
self
.
emit
(
"abstract public void traverse(SimpleNode node) throws Exception;"
,
1
)
self
.
emit
(
'}'
,
0
)
self
.
close
()
def
visitType
(
self
,
type
,
depth
=
1
):
self
.
visit
(
type
.
value
,
type
.
name
,
depth
)
def
visitSum
(
self
,
sum
,
name
,
depth
):
if
not
sum
.
simple
:
for
t
in
sum
.
types
:
self
.
visit
(
t
,
name
,
depth
)
def
visitProduct
(
self
,
product
,
name
,
depth
):
pass
def
visitConstructor
(
self
,
cons
,
name
,
depth
):
self
.
ctors
.
append
(
cons
.
name
)
class
ChainOfVisitors
:
def
__init__
(
self
,
*
visitors
):
self
.
visitors
=
visitors
def
visit
(
self
,
object
):
for
v
in
self
.
visitors
:
v
.
visit
(
object
)
if
__name__
==
"__main__"
:
mod
=
asdl
.
parse
(
sys
.
argv
[
1
])
if
not
asdl
.
check
(
mod
):
sys
.
exit
(
1
)
c
=
ChainOfVisitors
(
AnalyzeVisitor
(),
JavaVisitor
(),
VisitorVisitor
())
c
.
visit
(
mod
)
Back
|
FazBrowse Home
|
New Git URL