Score a parser in Go
This page scores the standard library’s json.Valid against the JSON compact corpus embedded in the module.
Replace json.Valid with a call to your own parser.
Install
The module requires go1.26 or newer.
Load a corpus
jsoncorpus.CompactSuite returns the header and the cases of the compact corpus embedded in the module:
To use a corpus file you downloaded or built, open it and call suite.ReadAll:
You can build the full JSON corpus with jsoncorpus.Full().Write(w).
Declare your parser’s positions
The JSON grammar does not settle everything: a parser may refuse a byte order mark, or accept an unpaired
surrogate escape, and still conform to RFC 8259. Each such property has a tag. A stance.Table records your
parser’s position on every tag:
NameandBecauseappear in the reasons thatTable.Expectreturns.Atis the last stage your parser reaches.json.Validonly checks syntax, so it stops atstance.Parse, and the corpus does not ask it aboutnumber/out-of-range, which arises atstance.Construct.Speaksgives the stage of each tag. Use the vocabulary of the grammar under test.Standsgives your position on each tag:stand meaning effect on a case carrying the tag stance.Acceptsyour parser reads such documents the grammar’s verdict decides stance.Refusesyour parser rejects them on purpose the case expects a rejection stance.Eitheryour parser does not guarantee either answer the case is not scored stance.Undeclaredno position (the zero value) the case is not scored
Find the positions by measuring your parser, not by reading its documentation. jsoncorpus.DefaultLexer was
built that way, and two of its positions differed from what its authors expected.
Check the vocabulary first
Before you score anything, check that your table covers every tag the corpus uses:
A tag missing from your table counts as Undeclared, so every case carrying it becomes Undecided. Without
this check, a newer corpus with a new tag would score fewer of your cases and give no warning.
Score every case
Convert each suite.Case to a stance.Doc, ask the table for the expected outcome, and compare it with what
your parser does:
With the table above, json.Valid has no disagreement with the compact corpus.
Read the outcome
Table.Expect returns one of three outcomes, and a reason:
stance.Accept: a conforming parser with your positions reads the document.stance.Reject: it refuses the document.stance.Undecided: the case tells nothing about your parser. Skip it, and do not count it as a pass.
A case is Undecided when it carries a tag you did not declare or declared as Either, when the grammar’s
acceptance does not reach the stage your parser stops at, or when the bytes are opaque to the oracle.
With the table above, every Undecided case is opaque: its bytes are not valid UTF-8, json.Valid accepts
some such documents, and the oracle cannot say what they denote. The reason string names the cause:
Score several modes
A parser with a strict mode and a lenient mode needs two tables, and one corpus. Copy the table, change the positions that differ, and run the loop once per mode.
Select a subset
Each case lists the constructs it contains in Features. To run only part of a corpus, filter the
stance.Doc values with stance.With or stance.Without. Features never change the expected outcome.
Keep your results in your repository
This module does not ship a test harness or a record of known failures. Keep the scoring loop, your tables and your list of known disagreements in your own repository, next to your parser.