Skip to content

Check a blamed prefix against the parser before naming it - #27

Closed
nertzy wants to merge 1 commit into
mainfrom
prefix-blame-check
Closed

nertzy wants to merge 1 commit into
mainfrom
prefix-blame-check

Conversation

@nertzy

@nertzy nertzy commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

The error for an undeclared prefix could name a prefix the query never used. This checks the name against the parser before blaming it, which closes the known limit left by #26.

The problem

The scan that finds an undeclared prefix reads characters and does not parse. A comparison written without spaces, such as ?a<'x>, reads to it as an IRI. It then takes the rest of the string or comment for query text:

Text Before Now
FILTER(?a<'x>ex:p'), then an unrelated syntax error Named ex:, from inside the string The parser's message
FILTER(?a<?b)#a>b ex:p, then an unrelated syntax error Named ex:, from inside the comment The parser's message

The chosen behavior

When the parser refuses a text and the scan finds a candidate prefix, the text is parsed a second time with that prefix declared. The prefix is named when the parser then accepts the text or refuses it differently. A name the parser never read as a prefix changes nothing, so the parser's own message stays.

The second parse runs only on text the parser has already refused. On a 227 KB query with the undeclared prefix on its last line, the failing call took about 26 ms on one machine. A successful parse of the same query took about 20 ms.

An error the parser reports without a position is never checked. The parser reports one only for text whose syntax it accepted, so no prefix is behind it. It also names whichever of several shared blank nodes a randomly ordered set yields first, so two parses of the same text can word that error differently. Before this guard, an update with two shared blank nodes and FILTER(?a<'x>ex:p') named ex: in 97 of 200 parses.

This check replaces the comparison between the name's position and the position in the parser's message, which it makes redundant.

One trade-off

A prefix is no longer named when declaring it leaves the parser's message exactly as it was. That happens when a second syntax error follows the prefix within a few characters, because the parser reports both errors at the same place with the same words.

Of 18 texts I tried that have an undeclared prefix and a second error, 16 still name the prefix. The two that do not are { ?s ex:p} and { ex:a }. Their message is now the parser's, which is vaguer and not wrong. The README says so.

Known limits

  • The position can be wrong when the prefix is right. In FILTER(?a<'x>ex:p') . ?s ex:q ?o the query does use ex: undeclared, so it is named, but the position points at the copy inside the string.
  • Only the first candidate is checked. When it is a false one, a different undeclared prefix later in the text is not named, and the parser's message stays.

Both need the unspaced comparison followed by a string or comment that holds something shaped like a prefixed name.

Each change started from a failing test.

Copilot AI balanced review requested due to automatic review settings November 17, 2026 20:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Randomized shared-blank-node diagnostics can cause the prefix check to falsely blame ex: inside a string, requiring preservation of positionless parser errors and regression coverage.

1 open issue
What changed in this PR

This PR adds parser-backed validation before naming an undeclared SPARQL prefix, addressing the false-prefix diagnostics left by #26.

Changes:

  • Reparse failed queries and updates with the candidate prefix declared before replacing the parser’s error.
  • Add regression tests for false candidates inside strings and comments, and prefixes followed by another error.
  • Document the fallback behavior and clarify the scanner’s role.
File Description
test/​test_query.rb Adds regression coverage for prefix diagnostics.
README.md Explains when the parser’s original message is retained.
ext/​oxigraph/​src/​query.rs Adds shared parser-backed candidate validation for queries and updates.
ext/​oxigraph/​src/​prefix.rs Clarifies in comments that scanned names require parser validation.
ext/​oxigraph/​src/​error.rs Formats errors using the validated candidate instead of a position comparison.

Review effort: Balanced

Comment thread ext/oxigraph/src/query.rs Resolved
The scan that finds an undeclared prefix reads characters and does not
parse. A comparison written without spaces, such as ?a<'x>, reads to it as
an IRI, and it then takes the rest of the string or comment for query text.
The error named a prefix that the query never used.

The text is now parsed a second time with the candidate prefix declared.
The prefix is named when the parser then accepts the text or refuses it
differently. A name the parser never read as a prefix changes nothing, and
the parser's own message stays. The second parse runs only for text the
parser has already refused.

That check replaces the comparison of the name's position with the position
in the parser's message. A prefix that another syntax error follows within a
few characters may no longer be named, because declaring it can leave the
parser's message as it was. The README says so.

An error the parser reports without a position is left alone. The parser
reports one only for text whose syntax it accepted, so no prefix is behind
it. It also names whichever of several shared blank nodes a randomly ordered
set yields first, so two parses of one text could word the error differently
and confirm a prefix that the text never used.
@nertzy

nertzy commented Nov 17, 2026

Copy link
Copy Markdown
Owner Author

Superseded by #28, which removes the prefix scan instead of adding a second parse to check it.

@nertzy nertzy closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants