Repository navigation
feat(bindings): Python, C and WASM couldn't see positions or XPath - #108
Merged
Merged
Conversation
The core records where each node was written and parsec-xpath runs
XPath 1.0, but none of the three bindings could reach either.
- Python: HTMLParser(positions=True), Node.source_range,
end_tag_range, attribute_range(name) and position, and
HTMLParser.line_col. xpath() on the parser and on nodes returns what
lxml returns, with $variables as keywords.
- C: the PARSEC_SPANS flag, parsec_node_span, parsec_end_span,
parsec_attr_span and parsec_line_col; parsec_xpath_compile, _free,
_evaluate and a result object. ABI additions only.
- WASM: parse(html, { spans }), node.span, endSpan, attributeSpan()
and xpath() on documents and nodes.
Each binding builds the XPath index on the first query and drops it
on any change, so it's built once per version of the tree.
The core collects parse errors now (#106), but Python, C and WASM had no way to ask for them or read them. - Python: HTMLParser(errors=True) and parser.errors, a list of ParseError records with code, offset, line and column. - C: the PARSEC_ERRORS flag, parsec_errors_count and parsec_errors_get, which fills a parsec_syntax_error. - WASM: parse(html, { errors }) and doc.errors. The C header's preamble now names the XPath handles in its rules.
README, ROADMAP and the Python and WASM package READMEs now list XPath, source positions and parse errors for each binding, mark #23 done, and give the WebAssembly's new size: 868 KB, 361 KB gzipped.
Python and JavaScript got the core's UTF-8 byte offsets, so in text past ASCII, src[start:end] and html.slice(start, end) cut in the wrong place. Offsets now come in the units of what the caller passed: - Python: code points into a str; bytes into bytes, even where bytes that weren't UTF-8 were read as U+FFFD. line_col takes the same. - WASM: UTF-16 units into the string, past the BMP too. - C stays in UTF-8 bytes, which is what C passes in. Error offsets follow. For non-ASCII input with spans or errors on, the binding keeps the text and builds, on first use, a table of units per 64-byte block, so a lookup reads 64 bytes at most. Lines and columns are as before, the column counting characters.
jamesconwaydev
enabled auto-merge (squash)
November 19, 2026 14:57
This was referenced Nov 19, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The Python package shipped in #80 as "part of #23": source positions and XPath didn't exist yet, so it couldn't offer them. Now the core has spans (#102) and parse errors (#106), and
parsec-xpathexists (#104), but none of the three bindings could reach any of it. From Python, C or JavaScript you still couldn't ask where a node was written, which errors the parser recovered from, or run//a/@href.Change
Python (
bindings/python)HTMLParser(html, positions=True, errors=True).positionsrather than the core'sspans, since the accessors it turns on are about positions and the word reads plainly next toscriptingandmax_depth.node.source_range,node.end_tag_rangeandnode.attribute_range(name)give(start, end), the same pairre.Match.span()gives, in the units of what was passed in: code points into astr, bytes intobytes(even where bad bytes were read as U+FFFD). Sosrc[start:end]is the source.line_coland error offsets use the same units.node.positionis(line, column)from 1, as lxml'ssourcelineand editors count.parser.line_col(offset)converts any offset. AllNonewithoutpositions=True.parser.errorsis a list ofParseErrorrecords (code,offset,line,column;str()gives2:1: unexpected-end-tag). NotSyntaxError, which is Python's own exception; these are records, not exceptions.parser.xpath(query, **variables)andnode.xpath(...)return what lxml returns: a list for a node-set, with elements and comments asNodes and text and attribute values asstr, else astr,floatorbool. Keywords bind$variables, as in lxml. Bad queries raiseXPathError, aValueError.Mutex<Option<Arc<Index>>>.write()drops it under the write lock, and a query builds it under the read lock, so a query never sees an index of an older tree. Queries run detached from the interpreter, ascss()does, and the RwLock rules stay as they were.C (
crates/parsec-capi), additions only:PARSEC_SPANS(2) andPARSEC_ERRORS(4) forparsec_parse,_withand_fragment.parsec_node_span,parsec_end_span,parsec_attr_span(bool, out pointers) andparsec_line_col.parsec_errors_countandparsec_errors_get, filling aparsec_syntax_error { code, code_len, offset, line, column };codeis static.parsec_xpath_compile,_free,_evaluate, and aparsec_xpath_resultread with_type,_count,_item(node, or an attribute as owner plus index withPARSEC_NO_ATTRfor a plain node),_string,_number,_boolean,_free. The result is a copy, so it outlives the document. New statusPARSEC_ERR_XPATH(13).OnceLock, whichdoc_mut(every changing call) clears, so const readers on many threads can share it.WASM (
bindings/wasm)parse(html, { spans, errors }),node.span,endSpanandattributeSpan(name)(aSpanwithstart,end,line,column;startandendare UTF-16 indexes, sohtml.slice(start, end)is the source, past the BMP too),doc.errors(ParseError[]), anddoc.xpath()/node.xpath()typed asXPathResult = (Node | string)[] | string | number | boolean. Text nodes come back asNodes, as the DOM'sevaluategives them; attributes as strings, since there's noAttrhere.borrow_mut()drops the index, so every edit invalidates it without each edit having to remember.In all three, every node without a parent is a root of the index, so a query can start in a removed subtree.
Offsets in the caller's units. The core counts UTF-8 bytes. C keeps them. For non-ASCII input with spans or errors on, Python and WASM keep a copy of the text and build, on the first lookup, a table of units per 64-byte block; a lookup then reads at most 64 bytes. ASCII input, valid UTF-8
bytes, and parses without spans or errors keep nothing. Lines and columns are the core's, the column counting characters.Docs: README (Python, C and WASM bullets and comparison rows), ROADMAP (#23 done), and both package READMEs.
Closes #23
Tests
<p>café</p><b>日本</b>and an emoji, asstr, asbytesand as bytes that aren't UTF-8) check every slice equals the tag's source. New: positions, errors, lxml-shaped XPath results, XPath from nodes and after edits, and 8 threads querying while one edits.tests/capi.c: new positions (with the same non-ASCII slice checks on the UTF-8), syntax errors and XPath sections, including every NULL and bad-argument path. Passes locally under ASan and UBSan; the header matches cbindgen 0.29.4 and compiles as C++.wasm-pack test --node: 7 passed.tests/package.test.mjs: 9 passed, with spans (including the same non-ASCII and surrogate-pair slice checks), errors, XPath and the shipped types.cargo test --workspace: 257 passed, 0 failed. clippy andcargo denyclean for the workspace and both bindings.