Skip to content

parser: generated ANTLR front-ends and an SPL2 subset - #1

Merged
be-thomas merged 8 commits into
mainfrom
feat/antlr-parsers
Oct 26, 2026
Merged

be-thomas merged 8 commits into
mainfrom
feat/antlr-parsers

Conversation

@be-thomas

Copy link
Copy Markdown
Owner

What this changes

Replaces the hand-written classic-SPL lexer and recursive-descent parser with
ANTLR 4.13.2 grammars and generated Go, and adds a working SPL2 subset. The
hand parser is deleted; the public Parse, Lex, and Token contracts are
preserved.

Parsers

  • pkg/parser/spl1/grammar/SPL1.g4 describes the whole classic-SPL surface:
    commands, search expressions, field-reference expressions, and the
    literal-versus-expression split on the right of a comparison.
  • pkg/parser/spl2/grammar/SPL2.g4 describes the subset: from, where, and
    stats, with from <dataset> lowering to the same index = "<dataset>"
    shape the SPL1 front-end produces.
  • Generated code is committed under pkg/parser/spl1/gen and
    pkg/parser/spl2/gen, so go build, go install, Docker, and release
    builds need neither Java nor the ANTLR jar. .gitattributes marks the trees
    as generated.
  • pkg/parser/internal/antlrpos maps ANTLR rune indexes to byte-based
    ast.Pos. pkg/parser/internal/antlrsupport records the first syntax error
    as one positioned diag.Error and aborts the parse.
  • tight.go retypes a few tokens in the stream before parsing, because
    ALL(*) prediction cannot use semantic predicates to choose between the two
    meanings of a word. The passes cover value-run starts, a wildcard that
    touches the preceding term, and not after an operator (field name) versus
    at the start of an expression (negation).

Dialect plumbing

  • compiler.Options.SPLVersion: 0/1 select classic SPL, 2 selects SPL2,
    anything else is refused with unsupported-spl-version at 1:1.
  • --spl-version 1|2 on transpile and ast; invalid values exit 2.
  • Conformance cases gain options.spl_version; new conformance/cases/spl2/
    and conformance/cases/search/wildcard-term-before-stage.* cases.

Tooling, dependencies, docs

  • ADR 0004 records the dependency exception: the pinned ANTLR Go runtime
    v4.13.1 and its indirect golang.org/x/exp.
  • scripts/generate-parsers.sh pins the ANTLR 4.13.2 jar by SHA-256 and
    regenerates both grammars; make generate / make generate-check wrap it,
    and CI runs generate-check in a Java-only job.
  • Docker caches module downloads in their own layer.
  • README, CONTRIBUTING, CONFORMANCE, COMPATIBILITY, ARCHITECTURE, ROADMAP,
    CHANGELOG, SECURITY, and THIRD_PARTY_NOTICES describe the generated parsers
    and the SPL2 subset.

Tests run

  • make check (format, vet, all tests, build)
  • make generate-check (both regenerated trees match the committed ones;
    generation is idempotent)
  • go test -race ./...
  • GOMAXPROCS=1 go test ./...
  • go test ./conformance -count=1, then make conformance-update with no
    changes to existing goldens
  • go test ./pkg/parser/spl1 -fuzz FuzzParseSPL1 -fuzztime 30s and the same
    for FuzzParseSPL2; no failures
  • Differential run of the old hand parser against the new front-end over every
    SPL1 conformance case and a 35-case not matrix: result codes and positions
    match; AST JSON is byte-identical for accepted queries
  • CLI smoke tests from the README, --spl-version 2 on transpile and ast,
    invalid --spl-version exits 2, and --strict still fails on warnings
  • Cross-builds for linux/darwin/windows on amd64/arm64
  • Docker image build and docker run ... transpile

Open questions

  1. Syntax-error wording. Generated-parser rejections now read, for example,
    unexpected "," instead of the hand parser's expected ')', found ','.
    The documented contract is the syntax code and its position; the wording
    is not. Is the new wording acceptable?
  2. Positions on some malformed expressions. For inputs such as a + (b,
    a AND (b, y-(x, and by*index], ALL(*) prediction can commit to a rule
    whose failure is reported slightly left of the hand parser's position,
    because predicates are not used to prune the simulation. Codes and
    fail-fast ordering are unchanged. Is that tradeoff acceptable, or should a
    follow-up add a pre-pass for these shapes?
  3. SPL2 scope. The subset is from, where, and stats; namespaced
    datasets (datamodel: / lookup:) and count(*) are refused with
    positioned diagnostics rather than guessed. Confirm this is the right
    boundary for the first SPL2 release.

Add pinned ANTLR 4.13.2 generation for the classic SPL and SPL2 grammars and
commit the generated Go, so builds, Docker, and releases need neither Java nor
the ANTLR jar. make generate rewrites both gen trees; make generate-check fails
on drift and runs in CI.

The ANTLR Go runtime is the only new module.
The combined grammar owns commands, expressions, precedence, and the
search-literal versus field-reference rule sets. A small Go driver sequences
stages, classifies command names, and lowers each stage as it is parsed so the
existing fail-fast diagnostic priority is preserved.

SourceMap converts ANTLR rune indexes to byte-based ast.Pos, the error listener
turns the first grammar rejection into a positioned diagnostic, and tight.go
resolves adjacent wildcard and hyphen runs before prediction. Lexer parity
tests and fuzz targets guard the duplicated lexical rules.
SPL2 gets its own ANTLR grammar and lowering for from, where, and stats. A
pipeline must start at a dataset, bare words are field references on both sides
of a comparison, AND is explicit, and a wildcard outside a dataset name is a
syntax error. from main lowers to the same IR as index=main, so emitters and
mappings apply unchanged. Namespaced datasets are refused with
unsupported-dataset.

compiler.Options.SPLVersion and ParseVersion select the dialect; the CLI
exposes --spl-version 1|2 and rejects any other value as a usage error. The
conformance runner accepts options.spl_version and fails on unknown values.
Cover the subset end to end: plain, wildcard, and hyphenated datasets;
where and stats lowering; a PromQL time-series case with its warnings; and
refusals for classic SPL syntax, namespaced datasets, a wildcard expression,
an unsupported command, and a second from stage.
Describe the ANTLR grammars, committed generated code, SourceMap, stage driver,
and visitor lowering; add ADR 0004 amending the empty-module-graph decision;
and record the single runtime dependency in the notices, security policy, and
README. Document the SPL2 input dialect, --spl-version, spl_version conformance
metadata, the new diagnostics, and the regeneration workflow.
A wildcard that touches the preceding word belongs to that term, so
`error* | stats count` no longer reads the star as multiplication with a
missing operand. The driver retypes such a star as STAR_TERM, and the
term-position multiplication rule excludes it. `atSearchMul` keeps the
term position's operator set separate from the expression one.

`not` is a field name after an arithmetic or comparison operator
(`eval x=1+not`) but the negation operator at the start of an expression
(`eval x=not(2)`). The driver retypes the former as NOT_WORD, including
after an `eval` that begins the pipeline with no leading pipe.

Out-of-range numbers are rejected in the lexical pass again, so a bad
number in a later stage still wins over an earlier semantic error, and a
malformed numeric literal quotes the whole letter run instead of its
first byte.

Adds regression tests for both token passes and a conformance case for a
wildcard term immediately before a stage.
Compose interpolates every service in the files it reads, including one
behind a profile, so the required SPLUNK_PASSWORD broke `make e2e` for
everyone. Move the optional Splunk oracle into docker-compose.splunk.yml
and start it only when the reference check is wanted.

The engine error checks also grepped the raw response text. A row value
can be the word "error" (for example an eval result) without the engine
failing, which made eval-sort-head fail on a successful query. Parse the
top-level JSON field instead.

Verified with `make check` and `make e2e` (8 passed, 0 failed).

@be-thomas be-thomas left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Reviewed the full diff at 257c293. Independently verified:

  • make check passes.
  • make generate-check regenerates both parsers with no drift.
  • go test ./conformance -update leaves the working tree clean (no golden churn).
  • About 2,200 differential queries (directed plus generated) against main, comparing AST, exit code, and diagnostic code/line/column. The known position-shift cases (a + (b, a AND (b, y-(x, by*index]) reproduce exactly and are not re-reported.

Five correctness defects are inline: silent operator loss (STAR_TERM), rejected keyword sort fields, star-as-term in value expressions (both accepts and rejects), not after a wildcard star, and one fail-fast ordering regression in bin.

Comment thread pkg/parser/spl1/build.go Resolved
Comment thread pkg/parser/spl1/grammar/SPL1.g4 Resolved
Comment thread pkg/parser/spl1/grammar/SPL1.g4 Resolved
Comment thread pkg/parser/spl1/tight.go Resolved
Comment thread pkg/parser/spl1/build.go Resolved
A star that follows a completed operand is a multiplication operator, and
the grammar now says so with its own STAR_OP token. A semantic predicate
cannot carry that decision: SLL prediction simulates the implicit-AND
alternative whenever both sides start with a star and never asks. Operand
positions accept operandStar only, so `a * *` parses as `a * TRUE`, while
`where (a*)` reports the missing operand at the `)`.

Multiplicative operator lowering accepts every star spelling, so
`eval x=a*b` and `eval pct=count/total*100` multiply instead of losing the
operator.

`sort` treats a word after a sign as a field name, so `sort -limit` and
`sort -not` no longer read as the limit keyword or the negation operator.

`bin` refuses a second field at that field's token, so a later argument
error cannot overtake it.

A `not` after an assignment's `=` negates the value that follows, so
`eval y=not * b` is `NOT TRUE AND b`.
@be-thomas
be-thomas merged commit 1ce4937 into main Oct 26, 2026
5 checks passed
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.

1 participant