Repository navigation
Expand file tree
/
Copy pathselector.py
More file actions
165 lines (122 loc) · 3.59 KB
/
Copy pathselector.py
File metadata and controls
165 lines (122 loc) · 3.59 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from java2python.lang import tokens
class Selector(object):
""" Selector -> base for selectors; provides operator methods.
"""
def __add__(self, other):
return Sibling(self, other)
def __and__(self, other):
return Descendant(self, other)
def __getitem__(self, key):
return Nth(self, key)
def __gt__(self, other):
return Child(self, other)
class Nth(Selector):
""" E[n] match any slice n of E
"""
def __init__(self, e, key):
self.e, self.key = e, key
def __call__(self, tree):
for etree in self.e(tree):
try:
matches = tree.children[self.key]
except (IndexError, ):
return
if not isinstance(matches, (list, )):
matches = [matches]
for child in matches:
yield child
def __str__(self):
return 'Nth({0})[{1}]'.format(self.e, self.key)
class Child(Selector):
""" E > F select any F that is a child of E
"""
def __init__(self, e, f):
self.e, self.f = e, f
def __call__(self, tree):
for ftree in self.f(tree):
for etree in self.e(tree.parent):
yield ftree
def __str__(self):
return 'Child({0} > {1})'.format(self.e, self.f)
class Type(Selector):
""" Type(T) select any token of type T
"""
def __init__(self, key, value=None):
self.key = key if isinstance(key, int) else getattr(tokens, key)
self.value = value
def __call__(self, tree):
if tree.token.type == self.key:
if self.value is None or self.value == tree.token.text:
yield tree
def __str__(self):
## TODO: add value
return 'Type({0}:{1})'.format(tokens.map[self.key], self.key)
class Star(Selector):
""" * select any
"""
def __call__(self, tree):
yield tree
def __str__(self):
return 'Star(*)'
class Descendant(Selector):
""" E & F select any F that is a descendant of E
"""
def __init__(self, e, f):
self.e, self.f = e, f
def __call__(self, tree):
for ftree in self.f(tree):
root, ftree = ftree, ftree.parent
while ftree:
for etree in self.e(ftree):
yield root
ftree = ftree.parent
def __str__(self):
return 'Descendant({0} & {1})'.format(self.e, self.f)
class Sibling(Selector):
""" E + F select any F immediately preceded by a sibling E
"""
def __init__(self, e, f):
self.e, self.f = e, f
def __call__(self, node):
parent = node.parent
if not parent:
return
for ftree in self.f(node):
index = node.parent.children.index(ftree)
if not index:
return
previous = node.parent.children[index-1]
for child in self.e(previous):
yield ftree
def __str__(self):
return 'Sibling({0} + {1})'.format(self.e, self.f)
def walkTreeSelector(tree, selector):
for item in selector(tree):
yield item
for child in tree.children:
for item in walkTreeSelector(child, selector):
yield item
if __name__ == '__main__':
import sys
from java2python.config import Config
from java2python.compiler import buildAST
source = open(sys.argv[1]).read()
tree = buildAST(source, Config(()))
tree.dump(sys.stdout)
selectors = [
Type('EXPR'),
Type('QUALIFIED_TYPE_IDENT') > Type('IDENT'),
Type('CLASS')[2],
Type('METHOD_CALL') & Type('IDENT'),
Type('IDENT') + Type('IDENT'),
]
for index, selector in enumerate(selectors):
print '{0}: {1}\n ==== {2}'.format(index, selector.__doc__.strip(), selector)
for node in walkTreeSelector(tree, selector):
name = str(node)
ntype = tokens.map[node.type]
args = (name, '') if name == ntype else (ntype, name)
print '{0}---- match: {1} {2}'.format(*(' '*8, )+args)
print