feat(parser): add token callbacks with a provenance reference - #731
Draft
jeswr wants to merge 3 commits into
Draft
feat(parser): add token callbacks with a provenance reference#731jeswr wants to merge 3 commits into
jeswr wants to merge 3 commits into
Conversation
Expose onToken and onTokenEnd on the existing parse callback object. Observe each token once around grammar or comment processing, including EOF, in synchronous and streaming modes. Keep the ordinary path free of observer checks and calls; stop future token dispatch after observer exceptions. Document lifecycle, coordinates, comment selection, return values, and error behavior. Add tests for ordering, streaming parity, cleanup, comments, and reuse. This commit is independent of the external provenance reference.
…on parser Adapt the provenance-occurrence-events proposal to the public token lifecycle. Keep the term location parser, compact provenance index, and EntityIndex adapter under examples/provenance with a runnable demo and no N3 package exports or additional core changes. Preserve current inverse-predicate semantics, frozen/interning RDF/JS factories, original lexical ranges, complete literal suffixes, nested compounds, and duplicate quad occurrences. Replace the private token dispatch override with onToken/onTokenEnd and retain a raw rdf:nil sentinel through the core grammar identity checks. Document the remaining private grammar/index dependencies and how to extract the preceding callback-only commit. Add 112 provenance tests, including stream splits, nested empty lists and N3 inverse regressions. The full suite passes 7,073 tests at 100% coverage; lint and Node/browser builds pass.
Pack the four public component ranges directly in add with one loop. Remove the separate offset-calculating helper while retaining atomic validation, defensive copying, the 16-number representation, and all parser and pending-occurrence paths. Seven alternating fresh-process samples on Node24 and Node25 were performance-neutral (median changes -0.6% and -1.2%). Larger representation and pending-storage rewrites were investigated but not retained because some workloads regressed. All 7,073 tests pass at 100% coverage and ESLint passes.
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.
This proposal adds public
onToken(token)andonTokenEnd(token)callbacks toParser.parse, then uses them in a complete term-location/provenance reference implementation. It continues the work in jeswr/N3.js#20 on top of the merged lexer ranges from #721.The draft keeps the callback API separate so it can be extracted and landed independently:
17301c3— public callbacks, contract tests, and documentation. This changes onlyN3Parser.js, the README, and a standalone callback test file. No observer calls or checks are added to the ordinary per-token path when callbacks are absent.Reference implementation and tests.
examples/provenancecontains the location parser, compact provenance index, an EntityIndex adapter, documentation, and a runnable demo. Nothing is added to N3's exports or published package, and this commit needs no further core parser, lexer, or Store changes.2efdb07— simplify public occurrence packing. Write the four component ranges directly in one loop, removing the separate offset-calculation helper. This retains the same 16-number storage, atomic validation, and copying behavior. Seven alternating fresh-process samples per runtime on Node 24/25 found performance-neutral public insertion times (median changes −0.6% and −1.2%).The reference is more complete than fork PR #16's term-symbol prototype: it handles frozen/interning factories, complete compound and literal-suffix ranges, current N3 emission rules, and ordered callbacks with partial-error behavior. Those guarantees require additional state. The packed representation retains its separate handling of unfinished ranges because simpler storage alternatives did not establish equivalent performance across the tested workloads.
The callbacks observe each token once around processing, including EOF and enabled comment tokens, across synchronous and streaming parsing. Return values are ignored.
onTokenEndruns for cleanup even if processing oronTokenthrows; consumer exceptions stop subsequent token dispatch. The README specifies ordering with quad/comment/completion callbacks, coordinate units, and error behavior.The reference replaces the private
_readTokenoverride with the public callbacks and retains the complete provenance behavior: duplicate occurrences with value-based quad lookup, original lexical spellings, literal suffixes, multiline ranges, nested lists/property lists/formulas/triple terms, named graphs, inverse predicates, and null ranges for generated terms. Frozen/interning RDF/JS factories work. The original proposal's parser changes have been adapted to current main, including the newer N3 inverse-predicate routing.Scope of the extension point: these callbacks make token observation public. The reference still subclasses private grammar/factory methods and uses EntityIndex internals to correlate terms with syntax. Its README explicitly records those dependencies and the need to pin/test a compatible N3 version; this is not a claim that the complete provenance implementation now uses only public APIs.
Validation:
Try the reference after installing dependencies:
node -r @babel/register examples/provenance/demo.js npm test -- --runInBand test/N3ProvenanceParser-test.jsTo land only the requested extension points, select the first commit; the full reference can remain a separate external experiment.