Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ANTLR output is generated; review the .g4 grammar instead.
pkg/parser/*/gen/** linguist-generated=true
pkg/parser/*/gen/** -diff

*.g4 linguist-language=ANTLR
*.g4 text eol=lf

go.sum text eol=lf
*.sh text eol=lf
22 changes: 21 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
run: make fmt-check

- name: Vet
run: go vet ./...
run: make vet

- name: Test
run: go test -race ./...
Expand All @@ -48,6 +48,26 @@ jobs:
./openspl transpile -t esql 'index=web status>=500 | stats count by host'
./openspl transpile -t promql --metric http_requests_total 'index=metrics | stats count by host'

generated:
name: generated parsers
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'

- name: Cache the pinned ANTLR jar
uses: actions/cache@v4
with:
path: ~/.cache/openspl
key: antlr-4.13.2

- name: Check generated code matches the grammars
run: make generate-check

commit-message:
name: commit messages
runs-on: ubuntu-latest
Expand Down
27 changes: 26 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@ All notable changes to this project are documented here. The format follows

## [Unreleased]

### Added

- Parsing for both dialects is generated from ANTLR grammars
(`pkg/parser/spl1/grammar/SPL1.g4`, `pkg/parser/spl2/grammar/SPL2.g4`). The
generated Go is committed, so builds and releases need neither Java nor the
ANTLR jar; `make generate-check` fails on drift.
- A working SPL2 subset: `from <dataset>`, `where`, and `stats`, selected with
`--spl-version 2` (or `compiler.Options.SPLVersion`). `from main` lowers to
the same IR as `index=main`, so all emitters and mappings apply unchanged.
- `unsupported-dataset` for namespaced SPL2 datasets and
`unsupported-spl-version` for unknown dialect numbers.
- Conformance cases under `cases/spl2/` and an `options.spl_version` metadata
key; unknown versions fail the case instead of silently running as classic
SPL.
- Fuzz targets `FuzzParseSPL1` and `FuzzParseSPL2`, and a lexer-parity test
that tokenizes one corpus with both generated lexers.

### Changed

- The module graph is no longer empty: the binary links the ANTLR Go runtime
and its transitive `golang.org/x/exp` helper. [ADR 0004](docs/adr/0004-antlr-parsers.md)
records the exception and the pinned version pair.
- SPL2 no longer returns `spl2-not-implemented`; constructs outside the subset
are refused with a positioned `unsupported-command`, `unknown-command`, or
`unsupported-dataset` diagnostic.

### Fixed

- ES|QL aggregations now use the valid `alias = COUNT(*)` form instead of the
Expand Down Expand Up @@ -44,7 +70,6 @@ All notable changes to this project are documented here. The format follows

### Planned

- SPL2 front-end
- Docker parity harness results
- `dedup`, `top`, `rare`, `rename`, `fillnull`
- Batch migration reports
Expand Down
8 changes: 6 additions & 2 deletions CONFORMANCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ conformance/
│ ├── shaping/ table, fields, sort, head
│ ├── mapping/ custom field and index mappings
│ ├── promql/ PromQL-specific output and refusals
│ ├── spl2/ SPL2 subset (`spl_version: 2`)
│ └── errors/ parser and unsupported-construct refusals
├── fixtures/
│ ├── mappings/ mapping files referenced by case metadata
Expand Down Expand Up @@ -56,7 +57,8 @@ exercised by that case. A case must be exercised by at least one target.
"options": {
"metric": "http_requests_total",
"span": "1m",
"index": "logs-*"
"index": "logs-*",
"spl_version": 2
},
"targets": {
"esql": {
Expand All @@ -75,7 +77,8 @@ exercised by that case. A case must be exercised by at least one target.
|---|---|
| `description` | Free text. Never asserted. |
| `mapping` | Mapping file, relative to the case file. |
| `options` | `metric`, `span`, and `index` passed to the compiler. |
| `options` | `metric`, `span`, `index`, and `spl_version` passed to the compiler. |
| `options.spl_version` | Input dialect. `1` (or absent) is classic SPL, `2` is the SPL2 subset. Any other value fails the case rather than silently falling back. |
| `targets.<target>.diagnostics` | The complete warning/info list with exact positions. Order is line, then column, then severity (`error`, `info`, `warning`), then code. |
| `targets.<target>.error` | The target must refuse the query with this error code and position. No golden file may exist. |

Expand Down Expand Up @@ -110,6 +113,7 @@ Directories are the suite grouping, so Go's `-run` selects an area:
```sh
go test ./conformance -run 'TestConformance/promql' -v
go test ./conformance -run 'TestConformance/errors' -v
go test ./conformance -run 'TestConformance/spl2' -v
```

Cases and their per-target checks run in parallel, bounded by `GOMAXPROCS`, so
Expand Down
42 changes: 35 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ make check # format check, vet, tests, build
```

Requirements: Go 1.24+ and `make`. Docker is optional, for the engine harness.
Java 17 is needed only to regenerate the parser sources, not to build or test.

## Generated parsers

Both dialects are ANTLR grammars. Generated Go is committed, so a normal build
never needs Java or the ANTLR jar.

| Path | Contents |
|---|---|
| `pkg/parser/spl1/grammar/SPL1.g4` | Classic SPL grammar (source of truth) |
| `pkg/parser/spl2/grammar/SPL2.g4` | SPL2 subset grammar (source of truth) |
| `pkg/parser/spl1/gen`, `pkg/parser/spl2/gen` | Generated lexer, parser, visitor; never hand-edited |

```bash
make generate # rewrite the gen/ trees (needs Java 17)
make generate-check # fail if gen/ is stale; runs in CI
```

Edit the `.g4`, run `make generate`, and commit both the grammar and the
regenerated output. A change that edits only `gen/` is rejected by review and
by `make generate-check`.

## Repository layout

Expand All @@ -22,8 +43,12 @@ Requirements: Go 1.24+ and `make`. Docker is optional, for the engine harness.
| `cmd/openspl` | CLI entry point |
| `pkg/ast` | Unified IR |
| `pkg/diag` | Positioned diagnostics |
| `pkg/parser/spl1` | Classic SPL front-end |
| `pkg/parser/spl2` | SPL2 front-end (reserved) |
| `pkg/parser/spl1` | Classic SPL front-end: stage driver, visitor, `Lex` adapter |
| `pkg/parser/spl2` | SPL2 subset front-end |
| `pkg/parser/commands` | Implemented and known command-name tables, per dialect |
| `pkg/parser/internal/antlrpos` | Byte-accurate `ast.Pos` source map |
| `pkg/parser/internal/antlrsupport` | Error listener and bail/recover |
| `pkg/parser/internal/lexerparity` | Test-only SPL1/SPL2 lexical parity gate |
| `pkg/mapping` | Field, index, and label mapping |
| `pkg/emitter/esql` | ES\|QL back-end |
| `pkg/emitter/promql` | PromQL back-end |
Expand Down Expand Up @@ -65,8 +90,11 @@ Reject mixed label matchers in PromQL

1. **IR:** add a stage or expression node in `pkg/ast` if no existing node fits.
Every node implements `StageName()`/`Position()` or `exprNode()`/`Position()`.
2. **Parser:** lex and parse the command in `pkg/parser/spl1`. If the command is
recognized but not implemented, add it to `knownCommands` so users get
2. **Grammar:** add the command's rule to the relevant `.g4`, run
`make generate`, and lower the new rule context in that front-end's
`build.go`. The Go driver in `parse.go` only sequences stages and classifies
command names; it is not a second parser. If the command is recognized but
not implemented, add it to `pkg/parser/commands` so users get
`unsupported-command` rather than a misleading syntax error.
3. **Emitters:** implement the ES|QL mapping and decide the PromQL tier
(supported or refused). An unsupported stage must return a diagnostic with a
Expand Down Expand Up @@ -124,15 +152,15 @@ Do not parse SPL in the emitter. If the IR lacks something, add an IR node.
| Errors | `diag.NewError` with a stable code and source position |
| Warnings | `warnf` with a code that appears in `docs/COMPATIBILITY.md` |
| Naming | Short, package-local; exported API documented |
| Dependencies | Standard library only; justify any exception in an ADR |
| Dependencies | Standard library plus the pinned ANTLR runtime; any further exception needs an ADR |
| Tests | Table-driven; assert codes and exact output |

## Pull request checklist

- [ ] `make check` passes
- [ ] `make check` and `make generate-check` pass
- [ ] New behavior has tests and, when relevant, a conformance case
- [ ] Diagnostic codes are documented
- [ ] No new third-party dependencies
- [ ] No new third-party dependencies beyond the ANTLR runtime ([ADR 0004](docs/adr/0004-antlr-parsers.md))
- [ ] Commit messages follow the hard rule
- [ ] The PR description explains the semantic mapping and its risks

Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ ARG COMMIT=none
ARG DATE=unknown

WORKDIR /src
COPY go.mod ./
COPY go.mod go.sum ./
RUN go mod download
COPY . .

RUN CGO_ENABLED=0 go build -trimpath \
Expand Down
27 changes: 24 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LDFLAGS := -s -w \
-X github.com/be-thomas/openspl/pkg/version.Commit=$(COMMIT) \
-X github.com/be-thomas/openspl/pkg/version.Date=$(DATE)

.PHONY: all build test race vet fmt fmt-check check hooks conformance conformance-update e2e clean help
.PHONY: all build test race vet fmt fmt-check check hooks generate generate-check conformance conformance-update e2e clean help

all: check

Expand All @@ -20,8 +20,14 @@ test:
race:
go test -race ./...

# Generated ANTLR parsers trip vet's unreachable analyzer: upstream codegen
# emits a defensive `goto` after a `return` in every rule. They are vetted with
# that one analyzer disabled; hand-written packages get the full set.
vet:
go vet ./...
@handwritten="$$(go list ./... | grep -v '/gen$$' || true)"; \
if [ -n "$$handwritten" ]; then go vet $$handwritten; fi
@generated="$$(go list ./... | grep '/gen$$' || true)"; \
if [ -n "$$generated" ]; then go vet -unreachable=false $$generated; fi

fmt:
gofmt -w .
Expand All @@ -36,10 +42,23 @@ check: fmt-check vet test build
@echo "ok: format, vet, tests, build"

hooks:
chmod +x .githooks/commit-msg .githooks/pre-commit scripts/check-commit-message.sh conformance/engines/run.sh
chmod +x .githooks/commit-msg .githooks/pre-commit scripts/check-commit-message.sh scripts/generate-parsers.sh conformance/engines/run.sh
git config core.hooksPath .githooks
@echo "installed: core.hooksPath=.githooks"

# Regenerate the ANTLR parsers. Needs Java 17+; builds never do.
generate:
./scripts/generate-parsers.sh

generate-check:
./scripts/generate-parsers.sh
@changed="$$(git status --porcelain -- pkg/parser/spl1/gen pkg/parser/spl2/gen)"; \
if [ -n "$$changed" ]; then \
echo "generated parsers are out of date; run make generate and commit the result"; \
echo "$$changed"; exit 1; \
fi
@echo "generated parsers are current"

conformance:
go test ./conformance -count=1

Expand All @@ -63,6 +82,8 @@ help:
'fmt-check fail if sources are not formatted' \
'check format check + vet + test + build' \
'hooks install the commit hooks' \
'generate regenerate the ANTLR parsers (needs Java 17+)' \
'generate-check fail if committed parsers differ from the grammars' \
'conformance run the offline conformance suite' \
'conformance-update regenerate goldens and expectations' \
'e2e run the Docker engine harness' \
Expand Down
40 changes: 31 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[![CI](https://github.com/be-thomas/openspl/actions/workflows/ci.yml/badge.svg)](https://github.com/be-thomas/openspl/actions/workflows/ci.yml)
[![Go](https://img.shields.io/badge/Go-1.24+-00ADD8?logo=go&logoColor=white)](https://go.dev)
[![Dependencies](https://img.shields.io/badge/dependencies-0-34d399)](go.mod)
[![Dependencies](https://img.shields.io/badge/dependencies-1_runtime-34d399)](go.mod)
[![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE)
[![Status](https://img.shields.io/badge/status-alpha-orange)](#project-status)
[![PRs](https://img.shields.io/badge/PRs-welcome-22d3ee)](CONTRIBUTING.md)
Expand All @@ -17,7 +17,7 @@
<h1>OpenSPL — Splunk SPL to Elastic ES|QL &amp; PromQL Transpiler</h1>

<p><strong>Translate Splunk SPL into Elastic ES|QL or Prometheus PromQL.</strong><br />
Deterministic. Offline. Zero dependencies. Every warning points at the exact token.</p>
Deterministic. Offline. No runtime services. Every warning points at the exact token.</p>
</div>

---
Expand Down Expand Up @@ -67,9 +67,11 @@ sum by (host) (increase(splunk_events_total{job="metrics"}[5m]))
|---|---|---|---|
| **`esql`** | Elasticsearch ES\|QL | ✅ Supported | Logs, search, stats, timecharts, regex extraction |
| **`promql`** | Prometheus | ✅ Supported | Counters and rate-style time-series aggregations |
| `spl2` | Splunk SPL2 | 🧪 Planned | Next-generation Splunk pipelines |
| `kusto` | Azure Data Explorer | 💡 Proposed | Microsoft observability stacks |

SPL2 is an **input dialect**, not a target. Pass `--spl-version 2` to parse the
supported subset (`from`, `where`, `stats`) and emit either target above.

### Fidelity contract

| Tier | Meaning | Example |
Expand Down Expand Up @@ -145,6 +147,7 @@ openspl transpile --strict -f savedsearch.spl
| `--index name` | Override the index selected in the query | from SPL |
| `--metric name` | PromQL metric name | `splunk_events_total` |
| `--span duration` | PromQL range window or timechart fallback | `5m` |
| `--spl-version 1\|2` | Input dialect: classic SPL or the SPL2 subset | `1` |
| `-f, --file file` | Read the query from a file (`-` = stdin) | stdin |
| `--json` | Emit JSON with `ok`, `query`, and diagnostics | off |
| `--strict` | Treat warnings as errors | off |
Expand Down Expand Up @@ -177,6 +180,24 @@ openspl transpile --strict -f savedsearch.spl

**Recognized but not implemented** — `join`, `lookup`, `transaction`, `eventstats`, `streamstats`, `dedup`, `top`, `rare`, `chart`, `append`, `multisearch`, subsearches, and more. These return `unsupported-command` with a precise position rather than a misleading parse.

### SPL2 subset

With `--spl-version 2`, a pipeline must start at `from` and may continue with
`where` and `stats`:

```console
$ openspl transpile --spl-version 2 'from main | where status=500 | stats count() AS count BY host'

FROM main
| WHERE status == 500
| STATS count = COUNT(*) BY `host.name`
```

SPL2 semantics are not classic SPL: a bare word is a field reference on both
sides of a comparison, and conditions need an explicit `AND`. Namespaced
datasets (`datamodel:…`, `lookup:…`) and every command outside the subset are
refused with a positioned diagnostic.

Full matrix → [docs/COMPATIBILITY.md](docs/COMPATIBILITY.md)

---
Expand All @@ -185,8 +206,8 @@ Full matrix → [docs/COMPATIBILITY.md](docs/COMPATIBILITY.md)

```mermaid
flowchart LR
A["SPL v1<br/>classic Splunk"] --> P1["spl1 parser"]
B["SPL v2<br/>planned"] --> P2["spl2 parser"]
A["SPL v1<br/>classic Splunk"] --> P1["spl1 ANTLR grammar"]
B["SPL v2<br/>supported subset"] --> P2["spl2 ANTLR grammar"]
P1 --> IR["Unified IR<br/>pkg/ast"]
P2 --> IR
IR --> M["Mapping<br/>ECS · indexes · labels"]
Expand All @@ -201,8 +222,9 @@ flowchart LR

| Layer | Package | Responsibility |
|---|---|---|
| Front-end | `pkg/parser/spl1` | Lex + parse classic SPL into IR. Knows nothing about targets. |
| Front-end | `pkg/parser/spl2` | Reserved SPL2 grammar. |
| Front-end | `pkg/parser/spl1` | ANTLR grammar and generated parser for classic SPL; lowers into IR. |
| Front-end | `pkg/parser/spl2` | ANTLR grammar for the SPL2 subset (`from`, `where`, `stats`). |
| Generated | `pkg/parser/*/gen` | Committed ANTLR output; edit the `.g4`, never these files. |
| IR | `pkg/ast` | One typed pipeline shared by every language. |
| Mapping | `pkg/mapping` | Splunk field → ECS field, index patterns, Prometheus labels. |
| Back-end | `pkg/emitter/esql` | IR → ES\|QL. |
Expand Down Expand Up @@ -279,7 +301,7 @@ Only time-series aggregations. PromQL has no concept of raw log rows, so OpenSPL
<details>
<summary><strong>Does OpenSPL call an LLM or send my queries anywhere?</strong></summary>

No. It is a Go compiler with zero external dependencies. Queries are processed in memory, and the process makes no network calls. This matters when searches contain internal hostnames, customer identifiers, or credentials.
No. Queries are processed in memory, and the process makes no network calls. The binary links one third-party module, the ANTLR Go parser runtime; there are no services, plugins, or telemetry. This matters when searches contain internal hostnames, customer identifiers, or credentials.
</details>

<details>
Expand All @@ -291,7 +313,7 @@ OpenSPL is deterministic: the same input and mapping produce the same bytes. Eve
<details>
<summary><strong>Does it support SPL2?</strong></summary>

Not yet. SPL2 lives in its own front-end package so its grammar and semantics do not leak into classic SPL. Until it ships, the package returns `spl2-not-implemented`.
Partially: `--spl-version 2` accepts `from`, `where`, and `stats`, emitting the same targets as classic SPL. SPL2 lives in its own ANTLR grammar and front-end so its semantics do not leak into classic SPL. Every other SPL2 command is recognized and refused with a position; see [docs/COMPATIBILITY.md](docs/COMPATIBILITY.md).
</details>

<details>
Expand Down
Loading
Loading