Fix parsing of valid tag names that collide with move/NAG tokens#65
Merged
Merged
Conversation
The PGN tokenizer (TokenStream.consumeToken) is context-free and tries the
move-number / move / NAG matchers before HEADER_ID. Immediately after a
BEGIN_HEADER ('['), the only valid token is the tag name, but a name matching
the NAG regex (e.g. N, RR) or the move regex (e.g. Nf3, e4) was tokenized as a
NAG/move first, so the header parser rejected it with "Missing or invalid PGN
game header ID". This made spec-valid tag pairs such as [New "..."] impossible
to parse.
Make the tokenizer header-context-aware: when the previously emitted token is
BEGIN_HEADER, match HEADER_ID first (and only), falling back to the existing
MISSING_PGN_HEADER_ID error at the same position when no tag name is present
(preserving the diagnostic for malformed input like ["Somewhere"]). _matchHeaderId
already matches exactly the spec-permitted tag-name characters, so only the
dispatch order changes.
Adds a resource-driven regression fixture (tag-name-token-collision) covering
New, bare N, RR, Nf3, e4, and New alongside a recognized Annotator tag.
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.
Closes #64
Summary
Fixes parsing of spec-valid PGN tag pairs whose tag name looks like a SAN move or a NAG token (e.g.
[New "..."],[N "..."],[RR "..."],[Nf3 "..."],[e4 "..."]), which previously threwMissing or invalid PGN game header ID.Root cause
TokenStream.consumeToken()is context-free and tries the move-number / move / NAG matchers beforeHEADER_ID.Right after a
BEGIN_HEADER([) the only valid token is the tag name, but a name matching the NAG regex (N,RR) or the move regex (Nf3,e4) was tokenized as a NAG/move first, so the header parser failed.Fix
Make the tokenizer header-context-aware: when the previously emitted token is
BEGIN_HEADER, matchHEADER_IDfirst (and only).If no tag name is present, throw the existing
MISSING_PGN_HEADER_IDerror at the same position, preserving the diagnostic for malformed input like["Somewhere"]._matchHeaderId(/(\w+)/) already matches exactly the spec-permitted tag-name characters (letters/digits/underscore, section 8.1), so no regex change is needed — only the dispatch order.Tests
Adds a resource-driven regression fixture
tag-name-token-collision(6 games coveringNew, bareN,RR,Nf3,e4, andNewalongside a recognizedAnnotator).13 content-tests fail without the fix; the whole suite (5616 tests) passes with it, and
npm test(lint + unit) is green.