Repository navigation
Expand file tree
/
Copy pathselector.py
More file actions
186 lines (138 loc) · 4.45 KB
/
Copy pathselector.py
File metadata and controls
186 lines (138 loc) · 4.45 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" java2python.lang.selector -> """
from java2python.lang import tokens
class Selector(object):
""" Selector -> base for selectors; provides operator methods.
"""
def __add__(self, other):
""" C + C => S
Produces a Sibling selector from this one and the right hand
side of the expression.
"""
return Sibling(self, other)
def __and__(self, other):
""" C & C => S
Produces a Descendant selector from this one and the right
hand side of the expression.
"""
return Descendant(self, other)
def __getitem__(self, key):
""" C[n] => S
Produces an Nth child selector from this one at the given key.
This is where we could support attribute selectors like C[type='void'].
"""
return Nth(self, key)
def __gt__(self, other):
""" C > C => S
Produces a Child selector from this one and the right hand
side of the expression.
"""
return Child(self, other)
def walk(self, tree):
for item in self(tree):
yield item
for child in tree.children:
for item in self.walk(child):
yield item
class Nth(Selector):
""" E[n] -> match any slice n of E
Similar to the :nth-child pseudo selector in CSS, but without the
support for keywords like 'odd', 'even', etc.
"""
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
Similar to the type selector in CSS.
"""
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):
val = '' if self.value is None else '={0}'.format(self.value)
return 'Type({0}{1}:{2})'.format(tokens.map[self.key], val, self.key)
class Token(Selector):
""" Token(T) select any token by matching attributes.
Similar to the type selector in CSS.
"""
# channel=None, index=None, input=None, line=None, start=None, stop=None, text=None, type=None
def __init__(self, **attrs):
self.attrs = attrs
if isinstance(attrs.get('type'), (basestring, )):
self.attrs['type'] = getattr(tokens, attrs.get('type'))
def __call__(self, tree):
items = self.attrs.items()
if all(getattr(tree.token, k)==v for k, v in items if v is not None):
yield tree
def __str__(self):
items = self.attrs.items()
keys = ('{}={}'.format(k, v) for k, v in items if v is not None)
return 'Token({})'.format(', '.join(keys))
class Star(Selector):
""" * select any
Similar to the * selector in CSS.
"""
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)