Mercurial > genshi > genshi-test
annotate genshi/path.py @ 586:5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
author | cmlenz |
---|---|
date | Wed, 08 Aug 2007 22:21:21 +0000 |
parents | cccb6c748609 |
children | aa5762c7b7f1 f0bb2c5ea0ff |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
66
822089ae65ce
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
61
diff
changeset
|
3 # Copyright (C) 2006 Edgewall Software |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
230 | 8 # are also available at http://genshi.edgewall.org/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
230 | 12 # history and logs, available at http://genshi.edgewall.org/log/. |
1 | 13 |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
14 """Basic support for evaluating XPath expressions against streams. |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
15 |
230 | 16 >>> from genshi.input import XML |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
17 >>> doc = XML('''<doc> |
516 | 18 ... <items count="4"> |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
19 ... <item status="new"> |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
20 ... <summary>Foo</summary> |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
21 ... </item> |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
22 ... <item status="closed"> |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
23 ... <summary>Bar</summary> |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
24 ... </item> |
516 | 25 ... <item status="closed" resolution="invalid"> |
26 ... <summary>Baz</summary> | |
27 ... </item> | |
28 ... <item status="closed" resolution="fixed"> | |
29 ... <summary>Waz</summary> | |
30 ... </item> | |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
31 ... </items> |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
32 ... </doc>''') |
516 | 33 >>> print doc.select('items/item[@status="closed" and ' |
34 ... '(@resolution="invalid" or not(@resolution))]/summary/text()') | |
35 BarBaz | |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
36 |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
37 Because the XPath engine operates on markup streams (as opposed to tree |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
38 structures), it only implements a subset of the full XPath 1.0 language. |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
39 """ |
1 | 40 |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
41 from math import ceil, floor |
1 | 42 import re |
43 | |
230 | 44 from genshi.core import Stream, Attrs, Namespace, QName |
45 from genshi.core import START, END, TEXT, COMMENT, PI | |
1 | 46 |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
47 __all__ = ['Path', 'PathSyntaxError'] |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
386
diff
changeset
|
48 __docformat__ = 'restructuredtext en' |
1 | 49 |
50 | |
114 | 51 class Axis(object): |
52 """Defines constants for the various supported XPath axes.""" | |
53 | |
54 ATTRIBUTE = 'attribute' | |
55 CHILD = 'child' | |
56 DESCENDANT = 'descendant' | |
57 DESCENDANT_OR_SELF = 'descendant-or-self' | |
58 SELF = 'self' | |
59 | |
60 def forname(cls, name): | |
61 """Return the axis constant for the given name, or `None` if no such | |
62 axis was defined. | |
63 """ | |
64 return getattr(cls, name.upper().replace('-', '_'), None) | |
65 forname = classmethod(forname) | |
66 | |
67 | |
68 ATTRIBUTE = Axis.ATTRIBUTE | |
69 CHILD = Axis.CHILD | |
70 DESCENDANT = Axis.DESCENDANT | |
71 DESCENDANT_OR_SELF = Axis.DESCENDANT_OR_SELF | |
72 SELF = Axis.SELF | |
73 | |
74 | |
1 | 75 class Path(object): |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
76 """Implements basic XPath support on streams. |
1 | 77 |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
78 Instances of this class represent a "compiled" XPath expression, and provide |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
79 methods for testing the path against a stream, as well as extracting a |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
80 substream matching that path. |
1 | 81 """ |
82 | |
139
54131cbb91a5
Implement position reporting for XPath syntax errors. Closes #20.
cmlenz
parents:
137
diff
changeset
|
83 def __init__(self, text, filename=None, lineno=-1): |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
84 """Create the path object from a string. |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
85 |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
386
diff
changeset
|
86 :param text: the path expression |
498 | 87 :param filename: the name of the file in which the path expression was |
88 found (used in error messages) | |
89 :param lineno: the line on which the expression was found | |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
90 """ |
1 | 91 self.source = text |
139
54131cbb91a5
Implement position reporting for XPath syntax errors. Closes #20.
cmlenz
parents:
137
diff
changeset
|
92 self.paths = PathParser(text, filename, lineno).parse() |
1 | 93 |
94 def __repr__(self): | |
137 | 95 paths = [] |
96 for path in self.paths: | |
97 steps = [] | |
98 for axis, nodetest, predicates in path: | |
99 steps.append('%s::%s' % (axis, nodetest)) | |
100 for predicate in predicates: | |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
101 steps[-1] += '[%s]' % predicate |
137 | 102 paths.append('/'.join(steps)) |
103 return '<%s "%s">' % (self.__class__.__name__, '|'.join(paths)) | |
1 | 104 |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
105 def select(self, stream, namespaces=None, variables=None): |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
106 """Returns a substream of the given stream that matches the path. |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
107 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
108 If there are no matches, this method returns an empty stream. |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
109 |
230 | 110 >>> from genshi.input import XML |
33 | 111 >>> xml = XML('<root><elem><child>Text</child></elem></root>') |
61 | 112 |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
113 >>> print Path('.//child').select(xml) |
33 | 114 <child>Text</child> |
115 | |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
116 >>> print Path('.//child/text()').select(xml) |
33 | 117 Text |
118 | |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
386
diff
changeset
|
119 :param stream: the stream to select from |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
386
diff
changeset
|
120 :param namespaces: (optional) a mapping of namespace prefixes to URIs |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
386
diff
changeset
|
121 :param variables: (optional) a mapping of variable names to values |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
386
diff
changeset
|
122 :return: the substream matching the path, or an empty stream |
498 | 123 :rtype: `Stream` |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
124 """ |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
125 if namespaces is None: |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
126 namespaces = {} |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
127 if variables is None: |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
128 variables = {} |
1 | 129 stream = iter(stream) |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
130 def _generate(): |
1 | 131 test = self.test() |
305 | 132 for event in stream: |
133 result = test(event, namespaces, variables) | |
1 | 134 if result is True: |
305 | 135 yield event |
330
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
136 if event[0] is START: |
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
137 depth = 1 |
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
138 while depth > 0: |
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
139 subevent = stream.next() |
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
140 if subevent[0] is START: |
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
141 depth += 1 |
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
142 elif subevent[0] is END: |
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
143 depth -= 1 |
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
144 yield subevent |
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
145 test(subevent, namespaces, variables, |
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
146 updateonly=True) |
1 | 147 elif result: |
148 yield result | |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
149 return Stream(_generate()) |
1 | 150 |
38
fec9f4897415
Fix for #2 (incorrect context node in path expressions). Still some paths that produce incorrect results, but the common case seems to work now.
cmlenz
parents:
37
diff
changeset
|
151 def test(self, ignore_context=False): |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
152 """Returns a function that can be used to track whether the path matches |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
153 a specific stream event. |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
154 |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
386
diff
changeset
|
155 The function returned expects the positional arguments ``event``, |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
386
diff
changeset
|
156 ``namespaces`` and ``variables``. The first is a stream event, while the |
305 | 157 latter two are a mapping of namespace prefixes to URIs, and a mapping |
306 | 158 of variable names to values, respectively. In addition, the function |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
386
diff
changeset
|
159 accepts an ``updateonly`` keyword argument that default to ``False``. If |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
386
diff
changeset
|
160 it is set to ``True``, the function only updates its internal state, |
306 | 161 but does not perform any tests or return a result. |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
162 |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
163 If the path matches the event, the function returns the match (for |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
386
diff
changeset
|
164 example, a `START` or `TEXT` event.) Otherwise, it returns ``None``. |
33 | 165 |
230 | 166 >>> from genshi.input import XML |
33 | 167 >>> xml = XML('<root><elem><child id="1"/></elem><child id="2"/></root>') |
168 >>> test = Path('child').test() | |
305 | 169 >>> for event in xml: |
170 ... if test(event, {}, {}): | |
386 | 171 ... print event[0], repr(event[1]) |
172 START (QName(u'child'), Attrs([(QName(u'id'), u'2')])) | |
498 | 173 |
174 :param ignore_context: if `True`, the path is interpreted like a pattern | |
175 in XSLT, meaning for example that it will match | |
176 at any depth | |
177 :return: a function that can be used to test individual events in a | |
178 stream against the path | |
179 :rtype: ``function`` | |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
25
diff
changeset
|
180 """ |
333
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
181 paths = [(p, len(p), [0], [], [0] * len(p)) for p in [ |
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
182 (ignore_context and [_DOTSLASHSLASH] or []) + p for p in self.paths |
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
183 ]] |
1 | 184 |
306 | 185 def _test(event, namespaces, variables, updateonly=False): |
330
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
186 kind, data, pos = event[:3] |
259
6f11ad260890
Fix bug in evaluating XPath expressions using the union operator `|`, which caused any path but the first to get out of sync with the event stream, and the whole thing returning too few results.
cmlenz
parents:
250
diff
changeset
|
187 retval = None |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
188 for steps, size, cursors, cutoff, counter in paths: |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
189 # Manage the stack that tells us "where we are" in the stream |
211
0a14c2a06be3
Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents:
179
diff
changeset
|
190 if kind is END: |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
191 if cursors: |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
192 cursors.pop() |
211
0a14c2a06be3
Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents:
179
diff
changeset
|
193 continue |
223 | 194 elif kind is START: |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
195 cursors.append(cursors and cursors[-1] or 0) |
259
6f11ad260890
Fix bug in evaluating XPath expressions using the union operator `|`, which caused any path but the first to get out of sync with the event stream, and the whole thing returning too few results.
cmlenz
parents:
250
diff
changeset
|
196 |
306 | 197 if updateonly or retval or not cursors: |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
198 continue |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
199 cursor = cursors[-1] |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
200 depth = len(cursors) |
1 | 201 |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
202 if cutoff and depth + int(kind is not START) > cutoff[0]: |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
203 continue |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
204 |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
205 ctxtnode = not ignore_context and kind is START \ |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
206 and depth == 2 |
259
6f11ad260890
Fix bug in evaluating XPath expressions using the union operator `|`, which caused any path but the first to get out of sync with the event stream, and the whole thing returning too few results.
cmlenz
parents:
250
diff
changeset
|
207 matched = None |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
208 while 1: |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
209 # Fetch the next location step |
137 | 210 axis, nodetest, predicates = steps[cursor] |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
211 |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
212 # If this is the start event for the context node, and the |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
213 # axis of the location step doesn't include the current |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
214 # element, skip the test |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
215 if ctxtnode and (axis is CHILD or axis is DESCENDANT): |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
216 break |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
217 |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
218 # Is this the last step of the location path? |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
219 last_step = cursor + 1 == size |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
220 |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
221 # Perform the actual node test |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
222 matched = nodetest(kind, data, pos, namespaces, variables) |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
223 |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
224 # The node test matched |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
225 if matched: |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
226 |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
227 # Check all the predicates for this step |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
228 if predicates: |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
229 for predicate in predicates: |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
230 pretval = predicate(kind, data, pos, namespaces, |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
231 variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
232 if type(pretval) is float: # FIXME <- need to |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
233 # check this for |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
234 # other types that |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
235 # can be coerced to |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
236 # float |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
237 counter[cursor] += 1 |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
238 if counter[cursor] != int(pretval): |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
239 pretval = False |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
240 if not pretval: |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
241 matched = None |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
242 break |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
243 |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
244 # Both the node test and the predicates matched |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
245 if matched: |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
246 if last_step: |
217
d8b195b22a44
Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents:
216
diff
changeset
|
247 if not ctxtnode or kind is not START \ |
d8b195b22a44
Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents:
216
diff
changeset
|
248 or axis is ATTRIBUTE or axis is SELF: |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
249 retval = matched |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
250 elif not ctxtnode or axis is SELF \ |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
251 or axis is DESCENDANT_OR_SELF: |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
252 cursor += 1 |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
253 cursors[-1] = cursor |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
254 cutoff[:] = [] |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
255 |
333
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
256 if kind is START: |
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
257 if last_step and not (axis is DESCENDANT or |
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
258 axis is DESCENDANT_OR_SELF): |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
259 cutoff[:] = [depth] |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
260 |
333
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
261 elif steps[cursor][0] is ATTRIBUTE: |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
262 # If the axis of the next location step is the |
363
caf7b68ab5dc
Parse template includes at parse time to avoid some runtime overhead.
cmlenz
parents:
333
diff
changeset
|
263 # attribute axis, we need to move on to processing |
caf7b68ab5dc
Parse template includes at parse time to avoid some runtime overhead.
cmlenz
parents:
333
diff
changeset
|
264 # that step without waiting for the next markup |
caf7b68ab5dc
Parse template includes at parse time to avoid some runtime overhead.
cmlenz
parents:
333
diff
changeset
|
265 # event |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
266 continue |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
267 |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
268 # We're done with this step if it's the last step or the |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
269 # axis isn't "self" |
384 | 270 if not matched or last_step or not ( |
271 axis is SELF or axis is DESCENDANT_OR_SELF): | |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
272 break |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
273 |
333
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
274 if (retval or not matched) and kind is START and \ |
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
275 not (axis is DESCENDANT or axis is DESCENDANT_OR_SELF): |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
276 # If this step is not a closure, it cannot be matched until |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
277 # the current element is closed... so we need to move the |
114 | 278 # cursor back to the previous closure and retest that |
279 # against the current element | |
333
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
280 backsteps = [(i, k, d, p) for i, (k, d, p) |
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
281 in enumerate(steps[:cursor]) |
215 | 282 if k is DESCENDANT or k is DESCENDANT_OR_SELF] |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
283 backsteps.reverse() |
333
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
284 for cursor, axis, nodetest, predicates in backsteps: |
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
285 if nodetest(kind, data, pos, namespaces, variables): |
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
286 cutoff[:] = [] |
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
287 break |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
288 cursors[-1] = cursor |
1 | 289 |
259
6f11ad260890
Fix bug in evaluating XPath expressions using the union operator `|`, which caused any path but the first to get out of sync with the event stream, and the whole thing returning too few results.
cmlenz
parents:
250
diff
changeset
|
290 return retval |
1 | 291 |
292 return _test | |
293 | |
294 | |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
295 class PathSyntaxError(Exception): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
296 """Exception raised when an XPath expression is syntactically incorrect.""" |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
297 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
298 def __init__(self, message, filename=None, lineno=-1, offset=-1): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
299 if filename: |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
300 message = '%s (%s, line %d)' % (message, filename, lineno) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
301 Exception.__init__(self, message) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
302 self.filename = filename |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
303 self.lineno = lineno |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
304 self.offset = offset |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
305 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
306 |
137 | 307 class PathParser(object): |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
308 """Tokenizes and parses an XPath expression.""" |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
309 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
310 _QUOTES = (("'", "'"), ('"', '"')) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
311 _TOKENS = ('::', ':', '..', '.', '//', '/', '[', ']', '()', '(', ')', '@', |
179
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
169
diff
changeset
|
312 '=', '!=', '!', '|', ',', '>=', '>', '<=', '<', '$') |
163
9df6f057efd3
Support for XPath number literals including decimal places.
cmlenz
parents:
162
diff
changeset
|
313 _tokenize = re.compile('("[^"]*")|(\'[^\']*\')|((?:\d+)?\.\d+)|(%s)|([^%s\s]+)|\s+' % ( |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
314 '|'.join([re.escape(t) for t in _TOKENS]), |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
315 ''.join([re.escape(t[0]) for t in _TOKENS]))).findall |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
316 |
139
54131cbb91a5
Implement position reporting for XPath syntax errors. Closes #20.
cmlenz
parents:
137
diff
changeset
|
317 def __init__(self, text, filename=None, lineno=-1): |
54131cbb91a5
Implement position reporting for XPath syntax errors. Closes #20.
cmlenz
parents:
137
diff
changeset
|
318 self.filename = filename |
54131cbb91a5
Implement position reporting for XPath syntax errors. Closes #20.
cmlenz
parents:
137
diff
changeset
|
319 self.lineno = lineno |
163
9df6f057efd3
Support for XPath number literals including decimal places.
cmlenz
parents:
162
diff
changeset
|
320 self.tokens = filter(None, [dqstr or sqstr or number or token or name |
9df6f057efd3
Support for XPath number literals including decimal places.
cmlenz
parents:
162
diff
changeset
|
321 for dqstr, sqstr, number, token, name in |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
322 self._tokenize(text)]) |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
323 self.pos = 0 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
324 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
325 # Tokenizer |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
326 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
327 at_end = property(lambda self: self.pos == len(self.tokens) - 1) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
328 cur_token = property(lambda self: self.tokens[self.pos]) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
329 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
330 def next_token(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
331 self.pos += 1 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
332 return self.tokens[self.pos] |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
333 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
334 def peek_token(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
335 if not self.at_end: |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
336 return self.tokens[self.pos + 1] |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
337 return None |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
338 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
339 # Recursive descent parser |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
340 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
341 def parse(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
342 """Parses the XPath expression and returns a list of location path |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
343 tests. |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
344 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
345 For union expressions (such as `*|text()`), this function returns one |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
346 test for each operand in the union. For patch expressions that don't |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
347 use the union operator, the function always returns a list of size 1. |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
348 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
349 Each path test in turn is a sequence of tests that correspond to the |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
350 location steps, each tuples of the form `(axis, testfunc, predicates)` |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
351 """ |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
352 paths = [self._location_path()] |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
353 while self.cur_token == '|': |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
354 self.next_token() |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
355 paths.append(self._location_path()) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
356 if not self.at_end: |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
357 raise PathSyntaxError('Unexpected token %r after end of expression' |
139
54131cbb91a5
Implement position reporting for XPath syntax errors. Closes #20.
cmlenz
parents:
137
diff
changeset
|
358 % self.cur_token, self.filename, self.lineno) |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
359 return paths |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
360 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
361 def _location_path(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
362 steps = [] |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
363 while True: |
215 | 364 if self.cur_token.startswith('/'): |
365 if self.cur_token == '//': | |
366 steps.append((DESCENDANT_OR_SELF, NodeTest(), [])) | |
367 elif not steps: | |
368 raise PathSyntaxError('Absolute location paths not ' | |
369 'supported', self.filename, | |
370 self.lineno) | |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
371 self.next_token() |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
372 |
137 | 373 axis, nodetest, predicates = self._location_step() |
374 if not axis: | |
145
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
139
diff
changeset
|
375 axis = CHILD |
137 | 376 steps.append((axis, nodetest, predicates)) |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
377 |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
378 if self.at_end or not self.cur_token.startswith('/'): |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
379 break |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
380 |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
381 return steps |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
382 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
383 def _location_step(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
384 if self.cur_token == '@': |
114 | 385 axis = ATTRIBUTE |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
386 self.next_token() |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
387 elif self.cur_token == '.': |
114 | 388 axis = SELF |
137 | 389 elif self.cur_token == '..': |
139
54131cbb91a5
Implement position reporting for XPath syntax errors. Closes #20.
cmlenz
parents:
137
diff
changeset
|
390 raise PathSyntaxError('Unsupported axis "parent"', self.filename, |
54131cbb91a5
Implement position reporting for XPath syntax errors. Closes #20.
cmlenz
parents:
137
diff
changeset
|
391 self.lineno) |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
392 elif self.peek_token() == '::': |
114 | 393 axis = Axis.forname(self.cur_token) |
394 if axis is None: | |
139
54131cbb91a5
Implement position reporting for XPath syntax errors. Closes #20.
cmlenz
parents:
137
diff
changeset
|
395 raise PathSyntaxError('Unsupport axis "%s"' % axis, |
54131cbb91a5
Implement position reporting for XPath syntax errors. Closes #20.
cmlenz
parents:
137
diff
changeset
|
396 self.filename, self.lineno) |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
397 self.next_token() |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
398 self.next_token() |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
399 else: |
137 | 400 axis = None |
401 nodetest = self._node_test(axis or CHILD) | |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
402 predicates = [] |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
403 while self.cur_token == '[': |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
404 predicates.append(self._predicate()) |
137 | 405 return axis, nodetest, predicates |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
406 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
407 def _node_test(self, axis=None): |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
408 test = prefix = None |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
409 next_token = self.peek_token() |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
410 if next_token in ('(', '()'): # Node type test |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
411 test = self._node_type() |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
412 |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
413 elif next_token == ':': # Namespace prefix |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
414 prefix = self.cur_token |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
415 self.next_token() |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
416 localname = self.next_token() |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
417 if localname == '*': |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
418 test = QualifiedPrincipalTypeTest(axis, prefix) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
419 else: |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
420 test = QualifiedNameTest(axis, prefix, localname) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
421 |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
422 else: # Name test |
137 | 423 if self.cur_token == '*': |
424 test = PrincipalTypeTest(axis) | |
425 elif self.cur_token == '.': | |
426 test = NodeTest() | |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
427 else: |
137 | 428 test = LocalNameTest(axis, self.cur_token) |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
429 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
430 if not self.at_end: |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
431 self.next_token() |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
432 return test |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
433 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
434 def _node_type(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
435 name = self.cur_token |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
436 self.next_token() |
137 | 437 |
438 args = [] | |
439 if self.cur_token != '()': | |
440 # The processing-instruction() function optionally accepts the | |
441 # name of the PI as argument, which must be a literal string | |
442 self.next_token() # ( | |
443 if self.cur_token != ')': | |
444 string = self.cur_token | |
445 if (string[0], string[-1]) in self._QUOTES: | |
446 string = string[1:-1] | |
447 args.append(string) | |
448 | |
449 cls = _nodetest_map.get(name) | |
450 if not cls: | |
139
54131cbb91a5
Implement position reporting for XPath syntax errors. Closes #20.
cmlenz
parents:
137
diff
changeset
|
451 raise PathSyntaxError('%s() not allowed here' % name, self.filename, |
54131cbb91a5
Implement position reporting for XPath syntax errors. Closes #20.
cmlenz
parents:
137
diff
changeset
|
452 self.lineno) |
137 | 453 return cls(*args) |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
454 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
455 def _predicate(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
456 assert self.cur_token == '[' |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
457 self.next_token() |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
458 expr = self._or_expr() |
121
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
114
diff
changeset
|
459 if self.cur_token != ']': |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
114
diff
changeset
|
460 raise PathSyntaxError('Expected "]" to close predicate, ' |
139
54131cbb91a5
Implement position reporting for XPath syntax errors. Closes #20.
cmlenz
parents:
137
diff
changeset
|
461 'but found "%s"' % self.cur_token, |
54131cbb91a5
Implement position reporting for XPath syntax errors. Closes #20.
cmlenz
parents:
137
diff
changeset
|
462 self.filename, self.lineno) |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
463 if not self.at_end: |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
464 self.next_token() |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
465 return expr |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
466 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
467 def _or_expr(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
468 expr = self._and_expr() |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
469 while self.cur_token == 'or': |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
470 self.next_token() |
137 | 471 expr = OrOperator(expr, self._and_expr()) |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
472 return expr |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
473 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
474 def _and_expr(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
475 expr = self._equality_expr() |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
476 while self.cur_token == 'and': |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
477 self.next_token() |
137 | 478 expr = AndOperator(expr, self._equality_expr()) |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
479 return expr |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
480 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
481 def _equality_expr(self): |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
482 expr = self._relational_expr() |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
483 while self.cur_token in ('=', '!='): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
484 op = _operator_map[self.cur_token] |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
485 self.next_token() |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
486 expr = op(expr, self._relational_expr()) |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
487 return expr |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
488 |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
489 def _relational_expr(self): |
516 | 490 expr = self._sub_expr() |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
491 while self.cur_token in ('>', '>=', '<', '>='): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
492 op = _operator_map[self.cur_token] |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
493 self.next_token() |
516 | 494 expr = op(expr, self._sub_expr()) |
495 return expr | |
496 | |
497 def _sub_expr(self): | |
498 token = self.cur_token | |
499 if token != '(': | |
500 return self._primary_expr() | |
501 self.next_token() | |
502 expr = self._or_expr() | |
503 if self.cur_token != ')': | |
504 raise PathSyntaxError('Expected ")" to close sub-expression, ' | |
505 'but found "%s"' % self.cur_token, | |
506 self.filename, self.lineno) | |
507 self.next_token() | |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
508 return expr |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
509 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
510 def _primary_expr(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
511 token = self.cur_token |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
512 if len(token) > 1 and (token[0], token[-1]) in self._QUOTES: |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
513 self.next_token() |
137 | 514 return StringLiteral(token[1:-1]) |
163
9df6f057efd3
Support for XPath number literals including decimal places.
cmlenz
parents:
162
diff
changeset
|
515 elif token[0].isdigit() or token[0] == '.': |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
516 self.next_token() |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
517 return NumberLiteral(as_float(token)) |
179
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
169
diff
changeset
|
518 elif token == '$': |
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
169
diff
changeset
|
519 token = self.next_token() |
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
169
diff
changeset
|
520 self.next_token() |
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
169
diff
changeset
|
521 return VariableReference(token) |
121
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
114
diff
changeset
|
522 elif not self.at_end and self.peek_token().startswith('('): |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
523 return self._function_call() |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
524 else: |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
525 axis = None |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
526 if token == '@': |
114 | 527 axis = ATTRIBUTE |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
528 self.next_token() |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
77
diff
changeset
|
529 return self._node_test(axis) |
137 | 530 |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
531 def _function_call(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
532 name = self.cur_token |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
533 if self.next_token() == '()': |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
534 args = [] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
535 else: |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
536 assert self.cur_token == '(' |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
537 self.next_token() |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
538 args = [self._or_expr()] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
539 while self.cur_token == ',': |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
540 self.next_token() |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
541 args.append(self._or_expr()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
542 if not self.cur_token == ')': |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
543 raise PathSyntaxError('Expected ")" to close function argument ' |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
544 'list, but found "%s"' % self.cur_token, |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
545 self.filename, self.lineno) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
546 self.next_token() |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
547 cls = _function_map.get(name) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
548 if not cls: |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
549 raise PathSyntaxError('Unsupported function "%s"' % name, |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
550 self.filename, self.lineno) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
551 return cls(*args) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
552 |
137 | 553 |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
554 # Type coercion |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
555 |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
556 def as_scalar(value): |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
557 """Convert value to a scalar. If a single element Attrs() object is passed |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
558 the value of the single attribute will be returned.""" |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
559 if isinstance(value, Attrs): |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
560 assert len(value) == 1 |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
561 return value[0][1] |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
562 else: |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
563 return value |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
564 |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
565 def as_float(value): |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
566 # FIXME - if value is a bool it will be coerced to 0.0 and consequently |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
567 # compared as a float. This is probably not ideal. |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
568 return float(as_scalar(value)) |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
569 |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
570 def as_long(value): |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
571 return long(as_scalar(value)) |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
572 |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
573 def as_string(value): |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
574 value = as_scalar(value) |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
575 if value is False: |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
576 return u'' |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
577 return unicode(value) |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
578 |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
579 def as_bool(value): |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
580 return bool(as_scalar(value)) |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
581 |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
582 |
137 | 583 # Node tests |
584 | |
585 class PrincipalTypeTest(object): | |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
586 """Node test that matches any event with the given principal type.""" |
137 | 587 __slots__ = ['principal_type'] |
588 def __init__(self, principal_type): | |
589 self.principal_type = principal_type | |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
590 def __call__(self, kind, data, pos, namespaces, variables): |
137 | 591 if kind is START: |
592 if self.principal_type is ATTRIBUTE: | |
593 return data[1] or None | |
594 else: | |
595 return True | |
596 def __repr__(self): | |
597 return '*' | |
598 | |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
599 class QualifiedPrincipalTypeTest(object): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
600 """Node test that matches any event with the given principal type in a |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
601 specific namespace.""" |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
602 __slots__ = ['principal_type', 'prefix'] |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
603 def __init__(self, principal_type, prefix): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
604 self.principal_type = principal_type |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
605 self.prefix = prefix |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
606 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
607 namespace = Namespace(namespaces.get(self.prefix)) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
608 if kind is START: |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
609 if self.principal_type is ATTRIBUTE and data[1]: |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
610 return Attrs([(name, value) for name, value in data[1] |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
611 if name in namespace]) or None |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
612 else: |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
613 return data[0] in namespace |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
614 def __repr__(self): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
615 return '%s:*' % self.prefix |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
616 |
137 | 617 class LocalNameTest(object): |
364
41d6eb7885ec
Fix for #77: match templates were matching their own output.
cmlenz
parents:
363
diff
changeset
|
618 """Node test that matches any event with the given principal type and |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
619 local name. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
620 """ |
137 | 621 __slots__ = ['principal_type', 'name'] |
622 def __init__(self, principal_type, name): | |
623 self.principal_type = principal_type | |
624 self.name = name | |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
625 def __call__(self, kind, data, pos, namespaces, variables): |
137 | 626 if kind is START: |
627 if self.principal_type is ATTRIBUTE and self.name in data[1]: | |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
628 return Attrs([(self.name, data[1].get(self.name))]) |
137 | 629 else: |
630 return data[0].localname == self.name | |
631 def __repr__(self): | |
632 return self.name | |
633 | |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
634 class QualifiedNameTest(object): |
364
41d6eb7885ec
Fix for #77: match templates were matching their own output.
cmlenz
parents:
363
diff
changeset
|
635 """Node test that matches any event with the given principal type and |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
636 qualified name. |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
637 """ |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
638 __slots__ = ['principal_type', 'prefix', 'name'] |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
639 def __init__(self, principal_type, prefix, name): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
640 self.principal_type = principal_type |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
641 self.prefix = prefix |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
642 self.name = name |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
643 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
644 qname = QName('%s}%s' % (namespaces.get(self.prefix), self.name)) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
645 if kind is START: |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
646 if self.principal_type is ATTRIBUTE and qname in data[1]: |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
647 return Attrs([(self.name, data[1].get(self.name))]) |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
648 else: |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
649 return data[0] == qname |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
650 def __repr__(self): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
651 return '%s:%s' % (self.prefix, self.name) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
652 |
137 | 653 class CommentNodeTest(object): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
654 """Node test that matches any comment events.""" |
137 | 655 __slots__ = [] |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
656 def __call__(self, kind, data, pos, namespaces, variables): |
330
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
657 return kind is COMMENT |
137 | 658 def __repr__(self): |
659 return 'comment()' | |
660 | |
661 class NodeTest(object): | |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
662 """Node test that matches any node.""" |
137 | 663 __slots__ = [] |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
664 def __call__(self, kind, data, pos, namespaces, variables): |
137 | 665 if kind is START: |
666 return True | |
667 return kind, data, pos | |
668 def __repr__(self): | |
669 return 'node()' | |
670 | |
671 class ProcessingInstructionNodeTest(object): | |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
672 """Node test that matches any processing instruction event.""" |
137 | 673 __slots__ = ['target'] |
674 def __init__(self, target=None): | |
675 self.target = target | |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
676 def __call__(self, kind, data, pos, namespaces, variables): |
330
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
677 return kind is PI and (not self.target or data[0] == self.target) |
137 | 678 def __repr__(self): |
679 arg = '' | |
680 if self.target: | |
681 arg = '"' + self.target + '"' | |
682 return 'processing-instruction(%s)' % arg | |
683 | |
684 class TextNodeTest(object): | |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
685 """Node test that matches any text event.""" |
137 | 686 __slots__ = [] |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
687 def __call__(self, kind, data, pos, namespaces, variables): |
330
1dbeaef463e7
XPath tests should never return event tuples, just values or booleans.
cmlenz
parents:
326
diff
changeset
|
688 return kind is TEXT |
137 | 689 def __repr__(self): |
690 return 'text()' | |
691 | |
692 _nodetest_map = {'comment': CommentNodeTest, 'node': NodeTest, | |
693 'processing-instruction': ProcessingInstructionNodeTest, | |
694 'text': TextNodeTest} | |
695 | |
696 # Functions | |
697 | |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
698 class Function(object): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
699 """Base class for function nodes in XPath expressions.""" |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
700 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
701 class BooleanFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
702 """The `boolean` function, which converts its argument to a boolean |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
703 value. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
704 """ |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
705 __slots__ = ['expr'] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
706 def __init__(self, expr): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
707 self.expr = expr |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
708 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
709 val = self.expr(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
710 return as_bool(val) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
711 def __repr__(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
712 return 'boolean(%r)' % self.expr |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
713 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
714 class CeilingFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
715 """The `ceiling` function, which returns the nearest lower integer number |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
716 for the given number. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
717 """ |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
718 __slots__ = ['number'] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
719 def __init__(self, number): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
720 self.number = number |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
721 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
722 number = self.number(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
723 return ceil(as_float(number)) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
724 def __repr__(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
725 return 'ceiling(%r)' % self.number |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
726 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
727 class ConcatFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
728 """The `concat` function, which concatenates (joins) the variable number of |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
729 strings it gets as arguments. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
730 """ |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
731 __slots__ = ['exprs'] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
732 def __init__(self, *exprs): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
733 self.exprs = exprs |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
734 def __call__(self, kind, data, pos, namespaces, variables): |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
735 strings = [] |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
736 for item in [expr(kind, data, pos, namespaces, variables) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
737 for expr in self.exprs]: |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
738 strings.append(as_string(item)) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
739 return u''.join(strings) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
740 def __repr__(self): |
169 | 741 return 'concat(%s)' % ', '.join([repr(expr) for expr in self.exprs]) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
742 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
743 class ContainsFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
744 """The `contains` function, which returns whether a string contains a given |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
745 substring. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
746 """ |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
747 __slots__ = ['string1', 'string2'] |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
748 def __init__(self, string1, string2): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
749 self.string1 = string1 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
750 self.string2 = string2 |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
751 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
752 string1 = self.string1(kind, data, pos, namespaces, variables) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
753 string2 = self.string2(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
754 return as_string(string2) in as_string(string1) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
755 def __repr__(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
756 return 'contains(%r, %r)' % (self.string1, self.string2) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
757 |
534
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
758 class MatchesFunction(Function): |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
759 """The `matches` function, which returns whether a string matches a regular |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
760 expression. |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
761 """ |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
762 __slots__ = ['string1', 'string2'] |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
763 flag_mapping = {'s': re.S, 'm': re.M, 'i': re.I, 'x': re.X} |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
764 |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
765 def __init__(self, string1, string2, flags=''): |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
766 self.string1 = string1 |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
767 self.string2 = string2 |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
768 self.flags = self._map_flags(flags) |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
769 def __call__(self, kind, data, pos, namespaces, variables): |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
770 string1 = as_string(self.string1(kind, data, pos, namespaces, variables)) |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
771 string2 = as_string(self.string2(kind, data, pos, namespaces, variables)) |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
772 return re.search(string2, string1, self.flags) |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
773 def _map_flags(self, flags): |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
774 return reduce(lambda a, b: a | b, |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
775 [self.flag_map[flag] for flag in flags], re.U) |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
776 def __repr__(self): |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
777 return 'contains(%r, %r)' % (self.string1, self.string2) |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
778 |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
779 class FalseFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
780 """The `false` function, which always returns the boolean `false` value.""" |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
781 __slots__ = [] |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
782 def __call__(self, kind, data, pos, namespaces, variables): |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
783 return False |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
784 def __repr__(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
785 return 'false()' |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
786 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
787 class FloorFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
788 """The `ceiling` function, which returns the nearest higher integer number |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
789 for the given number. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
790 """ |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
791 __slots__ = ['number'] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
792 def __init__(self, number): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
793 self.number = number |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
794 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
795 number = self.number(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
796 return floor(as_float(number)) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
797 def __repr__(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
798 return 'floor(%r)' % self.number |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
799 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
800 class LocalNameFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
801 """The `local-name` function, which returns the local name of the current |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
802 element. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
803 """ |
137 | 804 __slots__ = [] |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
805 def __call__(self, kind, data, pos, namespaces, variables): |
137 | 806 if kind is START: |
234 | 807 return data[0].localname |
137 | 808 def __repr__(self): |
809 return 'local-name()' | |
810 | |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
811 class NameFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
812 """The `name` function, which returns the qualified name of the current |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
813 element. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
814 """ |
137 | 815 __slots__ = [] |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
816 def __call__(self, kind, data, pos, namespaces, variables): |
137 | 817 if kind is START: |
234 | 818 return data[0] |
137 | 819 def __repr__(self): |
820 return 'name()' | |
821 | |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
822 class NamespaceUriFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
823 """The `namespace-uri` function, which returns the namespace URI of the |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
824 current element. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
825 """ |
137 | 826 __slots__ = [] |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
827 def __call__(self, kind, data, pos, namespaces, variables): |
137 | 828 if kind is START: |
234 | 829 return data[0].namespace |
137 | 830 def __repr__(self): |
831 return 'namespace-uri()' | |
832 | |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
833 class NotFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
834 """The `not` function, which returns the negated boolean value of its |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
835 argument. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
836 """ |
137 | 837 __slots__ = ['expr'] |
838 def __init__(self, expr): | |
839 self.expr = expr | |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
840 def __call__(self, kind, data, pos, namespaces, variables): |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
841 return not as_bool(self.expr(kind, data, pos, namespaces, variables)) |
137 | 842 def __repr__(self): |
843 return 'not(%s)' % self.expr | |
844 | |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
845 class NormalizeSpaceFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
846 """The `normalize-space` function, which removes leading and trailing |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
847 whitespace in the given string, and replaces multiple adjacent whitespace |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
848 characters inside the string with a single space. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
849 """ |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
850 __slots__ = ['expr'] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
851 _normalize = re.compile(r'\s{2,}').sub |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
852 def __init__(self, expr): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
853 self.expr = expr |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
854 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
855 string = self.expr(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
856 return self._normalize(' ', as_string(string).strip()) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
857 def __repr__(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
858 return 'normalize-space(%s)' % repr(self.expr) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
859 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
860 class NumberFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
861 """The `number` function that converts its argument to a number.""" |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
862 __slots__ = ['expr'] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
863 def __init__(self, expr): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
864 self.expr = expr |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
865 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
866 val = self.expr(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
867 return as_float(val) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
868 def __repr__(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
869 return 'number(%r)' % self.expr |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
870 |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
871 class RoundFunction(Function): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
872 """The `round` function, which returns the nearest integer number for the |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
873 given number. |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
874 """ |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
875 __slots__ = ['number'] |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
876 def __init__(self, number): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
877 self.number = number |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
878 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
879 number = self.number(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
880 return round(as_float(number)) |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
881 def __repr__(self): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
882 return 'round(%r)' % self.number |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
883 |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
884 class StartsWithFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
885 """The `starts-with` function that returns whether one string starts with |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
886 a given substring. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
887 """ |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
888 __slots__ = ['string1', 'string2'] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
889 def __init__(self, string1, string2): |
282
db5bdeca9192
Fix `starts-with()` XPath function so that it actually compares the two strings. Closes #61.
cmlenz
parents:
259
diff
changeset
|
890 self.string1 = string1 |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
891 self.string2 = string2 |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
892 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
893 string1 = self.string1(kind, data, pos, namespaces, variables) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
894 string2 = self.string2(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
895 return as_string(string1).startswith(as_string(string2)) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
896 def __repr__(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
897 return 'starts-with(%r, %r)' % (self.string1, self.string2) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
898 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
899 class StringLengthFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
900 """The `string-length` function that returns the length of the given |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
901 string. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
902 """ |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
903 __slots__ = ['expr'] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
904 def __init__(self, expr): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
905 self.expr = expr |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
906 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
907 string = self.expr(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
908 return len(as_string(string)) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
909 def __repr__(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
910 return 'string-length(%r)' % self.expr |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
911 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
912 class SubstringFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
913 """The `substring` function that returns the part of a string that starts |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
914 at the given offset, and optionally limited to the given length. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
915 """ |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
916 __slots__ = ['string', 'start', 'length'] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
917 def __init__(self, string, start, length=None): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
918 self.string = string |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
919 self.start = start |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
920 self.length = length |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
921 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
922 string = self.string(kind, data, pos, namespaces, variables) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
923 start = self.start(kind, data, pos, namespaces, variables) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
924 length = 0 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
925 if self.length is not None: |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
926 length = self.length(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
927 return string[as_long(start):len(as_string(string)) - as_long(length)] |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
928 def __repr__(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
929 if self.length is not None: |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
930 return 'substring(%r, %r, %r)' % (self.string, self.start, |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
931 self.length) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
932 else: |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
933 return 'substring(%r, %r)' % (self.string, self.start) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
934 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
935 class SubstringAfterFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
936 """The `substring-after` function that returns the part of a string that |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
937 is found after the given substring. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
938 """ |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
939 __slots__ = ['string1', 'string2'] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
940 def __init__(self, string1, string2): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
941 self.string1 = string1 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
942 self.string2 = string2 |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
943 def __call__(self, kind, data, pos, namespaces, variables): |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
944 string1 = as_string(self.string1(kind, data, pos, namespaces, variables)) |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
945 string2 = as_string(self.string2(kind, data, pos, namespaces, variables)) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
946 index = string1.find(string2) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
947 if index >= 0: |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
948 return string1[index + len(string2):] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
949 return u'' |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
950 def __repr__(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
951 return 'substring-after(%r, %r)' % (self.string1, self.string2) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
952 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
953 class SubstringBeforeFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
954 """The `substring-before` function that returns the part of a string that |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
955 is found before the given substring. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
956 """ |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
957 __slots__ = ['string1', 'string2'] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
958 def __init__(self, string1, string2): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
959 self.string1 = string1 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
960 self.string2 = string2 |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
961 def __call__(self, kind, data, pos, namespaces, variables): |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
962 string1 = as_string(self.string1(kind, data, pos, namespaces, variables)) |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
963 string2 = as_string(self.string2(kind, data, pos, namespaces, variables)) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
964 index = string1.find(string2) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
965 if index >= 0: |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
966 return string1[:index] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
967 return u'' |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
968 def __repr__(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
969 return 'substring-after(%r, %r)' % (self.string1, self.string2) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
970 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
971 class TranslateFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
972 """The `translate` function that translates a set of characters in a |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
973 string to target set of characters. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
974 """ |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
975 __slots__ = ['string', 'fromchars', 'tochars'] |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
976 def __init__(self, string, fromchars, tochars): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
977 self.string = string |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
978 self.fromchars = fromchars |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
979 self.tochars = tochars |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
980 def __call__(self, kind, data, pos, namespaces, variables): |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
981 string = as_string(self.string(kind, data, pos, namespaces, variables)) |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
982 fromchars = as_string(self.fromchars(kind, data, pos, namespaces, variables)) |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
983 tochars = as_string(self.tochars(kind, data, pos, namespaces, variables)) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
984 table = dict(zip([ord(c) for c in fromchars], |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
985 [ord(c) for c in tochars])) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
986 return string.translate(table) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
987 def __repr__(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
988 return 'translate(%r, %r, %r)' % (self.string, self.fromchars, |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
989 self.tochars) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
990 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
991 class TrueFunction(Function): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
992 """The `true` function, which always returns the boolean `true` value.""" |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
993 __slots__ = [] |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
994 def __call__(self, kind, data, pos, namespaces, variables): |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
995 return True |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
996 def __repr__(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
997 return 'true()' |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
998 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
999 _function_map = {'boolean': BooleanFunction, 'ceiling': CeilingFunction, |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
1000 'concat': ConcatFunction, 'contains': ContainsFunction, |
534
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
1001 'matches': MatchesFunction, 'false': FalseFunction, 'floor': |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
1002 FloorFunction, 'local-name': LocalNameFunction, 'name': |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
1003 NameFunction, 'namespace-uri': NamespaceUriFunction, |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
1004 'normalize-space': NormalizeSpaceFunction, 'not': NotFunction, |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1005 'number': NumberFunction, 'round': RoundFunction, |
534
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
1006 'starts-with': StartsWithFunction, 'string-length': |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
1007 StringLengthFunction, 'substring': SubstringFunction, |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
1008 'substring-after': SubstringAfterFunction, 'substring-before': |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
1009 SubstringBeforeFunction, 'translate': TranslateFunction, |
cccb6c748609
Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents:
518
diff
changeset
|
1010 'true': TrueFunction} |
137 | 1011 |
179
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
169
diff
changeset
|
1012 # Literals & Variables |
137 | 1013 |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
1014 class Literal(object): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
1015 """Abstract base class for literal nodes.""" |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
1016 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
1017 class StringLiteral(Literal): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1018 """A string literal node.""" |
137 | 1019 __slots__ = ['text'] |
1020 def __init__(self, text): | |
1021 self.text = text | |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1022 def __call__(self, kind, data, pos, namespaces, variables): |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
1023 return self.text |
137 | 1024 def __repr__(self): |
1025 return '"%s"' % self.text | |
1026 | |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
1027 class NumberLiteral(Literal): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1028 """A number literal node.""" |
137 | 1029 __slots__ = ['number'] |
1030 def __init__(self, number): | |
1031 self.number = number | |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1032 def __call__(self, kind, data, pos, namespaces, variables): |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
1033 return self.number |
137 | 1034 def __repr__(self): |
1035 return str(self.number) | |
1036 | |
179
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
169
diff
changeset
|
1037 class VariableReference(Literal): |
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
169
diff
changeset
|
1038 """A variable reference node.""" |
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
169
diff
changeset
|
1039 __slots__ = ['name'] |
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
169
diff
changeset
|
1040 def __init__(self, name): |
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
169
diff
changeset
|
1041 self.name = name |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1042 def __call__(self, kind, data, pos, namespaces, variables): |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
1043 return variables.get(self.name) |
179
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
169
diff
changeset
|
1044 def __repr__(self): |
215 | 1045 return str(self.name) |
179
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
169
diff
changeset
|
1046 |
137 | 1047 # Operators |
1048 | |
1049 class AndOperator(object): | |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1050 """The boolean operator `and`.""" |
137 | 1051 __slots__ = ['lval', 'rval'] |
1052 def __init__(self, lval, rval): | |
1053 self.lval = lval | |
1054 self.rval = rval | |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1055 def __call__(self, kind, data, pos, namespaces, variables): |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
1056 lval = as_bool(self.lval(kind, data, pos, namespaces, variables)) |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1057 if not lval: |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1058 return False |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1059 rval = self.rval(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
1060 return as_bool(rval) |
137 | 1061 def __repr__(self): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1062 return '%s and %s' % (self.lval, self.rval) |
137 | 1063 |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1064 class EqualsOperator(object): |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1065 """The equality operator `=`.""" |
137 | 1066 __slots__ = ['lval', 'rval'] |
1067 def __init__(self, lval, rval): | |
1068 self.lval = lval | |
1069 self.rval = rval | |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1070 def __call__(self, kind, data, pos, namespaces, variables): |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
1071 lval = as_scalar(self.lval(kind, data, pos, namespaces, variables)) |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
1072 rval = as_scalar(self.rval(kind, data, pos, namespaces, variables)) |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1073 return lval == rval |
137 | 1074 def __repr__(self): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1075 return '%s=%s' % (self.lval, self.rval) |
137 | 1076 |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1077 class NotEqualsOperator(object): |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1078 """The equality operator `!=`.""" |
137 | 1079 __slots__ = ['lval', 'rval'] |
1080 def __init__(self, lval, rval): | |
1081 self.lval = lval | |
1082 self.rval = rval | |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1083 def __call__(self, kind, data, pos, namespaces, variables): |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
1084 lval = as_scalar(self.lval(kind, data, pos, namespaces, variables)) |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
1085 rval = as_scalar(self.rval(kind, data, pos, namespaces, variables)) |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1086 return lval != rval |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1087 def __repr__(self): |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1088 return '%s!=%s' % (self.lval, self.rval) |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1089 |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1090 class OrOperator(object): |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1091 """The boolean operator `or`.""" |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1092 __slots__ = ['lval', 'rval'] |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1093 def __init__(self, lval, rval): |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1094 self.lval = lval |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1095 self.rval = rval |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1096 def __call__(self, kind, data, pos, namespaces, variables): |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
1097 lval = as_bool(self.lval(kind, data, pos, namespaces, variables)) |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
156
diff
changeset
|
1098 if lval: |
137 | 1099 return True |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1100 rval = self.rval(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
1101 return as_bool(rval) |
137 | 1102 def __repr__(self): |
1103 return '%s or %s' % (self.lval, self.rval) | |
1104 | |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1105 class GreaterThanOperator(object): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1106 """The relational operator `>` (greater than).""" |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1107 __slots__ = ['lval', 'rval'] |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1108 def __init__(self, lval, rval): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1109 self.lval = lval |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1110 self.rval = rval |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1111 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1112 lval = self.lval(kind, data, pos, namespaces, variables) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1113 rval = self.rval(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
1114 return as_float(lval) > as_float(rval) |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1115 def __repr__(self): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1116 return '%s>%s' % (self.lval, self.rval) |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1117 |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1118 class GreaterThanOrEqualOperator(object): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1119 """The relational operator `>=` (greater than or equal).""" |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1120 __slots__ = ['lval', 'rval'] |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1121 def __init__(self, lval, rval): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1122 self.lval = lval |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1123 self.rval = rval |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1124 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1125 lval = self.lval(kind, data, pos, namespaces, variables) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1126 rval = self.rval(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
1127 return as_float(lval) >= as_float(rval) |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1128 def __repr__(self): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1129 return '%s>=%s' % (self.lval, self.rval) |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1130 |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1131 class LessThanOperator(object): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1132 """The relational operator `<` (less than).""" |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1133 __slots__ = ['lval', 'rval'] |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1134 def __init__(self, lval, rval): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1135 self.lval = lval |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1136 self.rval = rval |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1137 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1138 lval = self.lval(kind, data, pos, namespaces, variables) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1139 rval = self.rval(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
1140 return as_float(lval) < as_float(rval) |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1141 def __repr__(self): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1142 return '%s<%s' % (self.lval, self.rval) |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1143 |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1144 class LessThanOrEqualOperator(object): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1145 """The relational operator `<=` (less than or equal).""" |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1146 __slots__ = ['lval', 'rval'] |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1147 def __init__(self, lval, rval): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1148 self.lval = lval |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1149 self.rval = rval |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1150 def __call__(self, kind, data, pos, namespaces, variables): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1151 lval = self.lval(kind, data, pos, namespaces, variables) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
223
diff
changeset
|
1152 rval = self.rval(kind, data, pos, namespaces, variables) |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
516
diff
changeset
|
1153 return as_float(lval) <= as_float(rval) |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1154 def __repr__(self): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1155 return '%s<=%s' % (self.lval, self.rval) |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1156 |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1157 _operator_map = {'=': EqualsOperator, '!=': NotEqualsOperator, |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1158 '>': GreaterThanOperator, '>=': GreaterThanOrEqualOperator, |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
161
diff
changeset
|
1159 '<': LessThanOperator, '>=': LessThanOrEqualOperator} |
333
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
1160 |
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
1161 |
b70563ade748
Fix XPath traversal in match templates. Previously, `div/p` would be treated the same as `div//p`, i.e. it would match all descendants and not just the immediate children.
cmlenz
parents:
330
diff
changeset
|
1162 _DOTSLASHSLASH = (DESCENDANT_OR_SELF, PrincipalTypeTest(None), ()) |