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
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ jobs:
echo "$HOME/.elan/bin" >> "$GITHUB_PATH"
- name: Show toolchain
run: lake --version
# Default targets: the generator, the language server, and VerifiedSmoke
# (calc proofs, lexer/parser roundtrips). VerifiedMid and VerifiedHeavy are
# not built here; they are minutes-to-hours of native_decide.
# Default targets: the generator and its library, LeanParserGeneratorRuntime, the
# language server and LspDiagnosticsTest, the Asm and RustActions fixtures, and
# VerifiedSmoke (the calc, asm and spanuse proofs and the calc and
# spanuse roundtrips). VerifiedMid and VerifiedHeavy are not built here.
- name: Build default targets (includes VerifiedSmoke)
run: lake build
# Build the runtime library's static archive on its own. This is the
Expand Down
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ __pycache__/
.vscode/
*.swp

# local build logs and agent/tooling state
# local build logs
*.log
.kiro/
.research/
next.md
test/asm/large/
188 changes: 58 additions & 130 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,149 +6,77 @@ versions may change the CLI, the `.lig` syntax, and the generated module API.

## [Unreleased]

Changes since `0.1.0-alpha.1`.

### Added

- the list macro `separated_list_trailing(sep, X)`: zero or more `X` separated by `sep`,
with an optional trailing `sep`, built as a left-recursive `Array` (`Vec` in Rust). It
accepts what `separated_list` accepted before this release.
- a streaming parser option, `%option parseStr`. When a `%lexer` block is also present,
`<Base>Parser` also defines `parseStr : String → …`, which pulls one token at a time from the
byte lexer instead of building the token list. `<Base>Verified` relates `parseStr` to
`tokenize`, `parse_eq_dependent` and `parser_sound`. The stream stops at the first error,
lex or parse.
- `PosLexerAgrees` (the positioned byte lexer `lexPos` and `tokenize` succeed on the same
inputs with the same tokens and spans) is proved for every generated lexer, multi-mode
lexers included: `<Base>Lexer.posLexerAgrees`, from a Boolean check over every Unicode
scalar value, `<Base>Lexer.byteUnicodeValidated : Lexer.byteValidAll cbyte = true`
(`native_decide`, 70 ms for Cedar). Library: `Lexer/ByteUnicode.lean`,
`Lexer/ByteAgree.lean`.
- End-to-end theorems in `<Base>Verified` for `%option parseStr`:
`parseStr_eq_none_of_tokenize_error` (lexing fails → `parseStr` is `none`),
`runStream_ne_ok_of_tokenize_error`, `parseStr_sound_tokenize`,
and, for a lexer with one mode whose rules all stay, `parseStr_sound_tokenization` (an
accepted `parseStr s` comes with a maximal-munch `Lexer.Tokenization` of `s`, the spec
tokenizer's tokens and a parse tree) and `tokenize_parse_sound` (the same for `tokenize`
then `parse`). Library: `Lexer/LexerSound.lean`, `Runtime/ParseEnd.lean`.
- Lexer modules emit `noEofEmit` (no rule emits the `%eof` token) and, for one mode whose
rules all stay, `stay0`, `textIndep0` and `tokenize_sound`.
- Spans in the verified specification. `Runtime.SpanGrammar` (`Runtime/SpanGrammar.lean`):
a grammar whose parse trees have positioned tokens and whose actions receive the spans
of their right-hand-side symbols; `ptSpan`/`ptSem`, `ptSpan_endPos`, `ptSpan_startPos`,
`allSpanFacts`, and `SpanGrammar.parseCorrect`, `parseComplete`, `unambiguity`, which
transfer the generic theorems through the derived grammar `SpanGrammar.toGrammar`
(`ptSem_toGrammar`). The refinement simulation (`Runtime/Refinement.lean`) relates the
executable parser to it on real spans. `Span.fold`, `Span.listEnd`,
`Positioned.wordEnd`.
- String-level completeness and unambiguity. Library (`Lexer/Lexing.lean`): `Lexing`
(a maximal-munch tokenization with its steps), `Lexing.unique`, `tokenize_complete`,
`tokenize_lexing`, `tokenizeFlatStrPos_complete`; `PosLexing` (the positioned
tokenization: spans delimit lexemes, skip rules consume the bytes between tokens),
`tokenizeFlatStrPos_iff_posLexing`, `PosLexing.unique`, `PosLexing.spans_ordered`,
`lexPos_ok_iff`. Lexer modules (one mode, all rules stay): `tokenize_complete`,
`Lexes`, `tokenize_iff_lexes`, `lexPos_iff_lexes`, `lexes_unique`. `<Base>Verified`:
`parseStr_complete`, `parseStr_complete_lexing`, `parseStr_unambiguous`,
`parseStr_sound_lexes`, `parseStr_complete_lexes`, `parseStr_unambiguous_lexes`,
`parseStr_node_spans`.
- `spanuse.SpanUseVerified`, a grammar whose action reads a span, is in `VerifiedSmoke`.
- Soundness over the whole input. `<Base>Verified` (with `%option parseStr`, a lexer with
one mode whose rules all stay, and a valueless `%eof` token): `endMarker_last`
(`native_decide`: `$` is the last symbol of every start production, and no other
right-hand-side position holds `$` or the start symbol), `word_eq_of_lexes` (a start
tree whose word begins the stream of `ts`, for `Lexes s ts`, has the word `ts`) and
`parseStr_sound_exact` (an accepted `parseStr s` gives `tokenize s = .ok ts`,
`Lexes s ts` and a parse tree over exactly `ts`). Lexer modules: `lexes_eof_last`.
Library: `Runtime.EndMarkerLast`, `Runtime.parseTree_start_word`,
`Runtime.start_word_eq` (`Runtime/ParseEnd.lean`), `Lexer.PosLexing.eof_last`,
`Lexer.modeAct_emit_of_agrees` (`Lexer/Lexing.lean`).
- Node spans as text of the input. Library (`Lexer/SpanText.lean`, new):
`Lexer.AllNodeText`, `Lexer.NodeText`, `Lexer.allNodeText`,
`Lexer.PosLexing.allNodeText`; in `Lexer/Lexing.lean`: `Lexer.SpanIn`,
`Lexer.TokenLexeme`, `Lexer.PosLexing.tokenLexeme`.
- Lexer modules emit `tokenizeByte_toOption`: `tokenizeByte` succeeds exactly when
`tokenize` does, with the same token values. Library: `Lexer.tokenizeByte_eq_lexPos`
(`tokenizeByte` is `lexPos` without spans) and `Lexer.tokenizeByte_toOption`.
- `%option parseStr`: `<Base>Parser.parseStr : String → …` parses while pulling tokens from
the byte lexer, without a token list. It stops at the first error, lex or parse.
- String-level theorems in `<Base>Verified` for `%option parseStr`, with no hypothesis:
- `parseStr_eq`, `parseStr_eq_dependent`, `parseStr_sound`: `parseStr` agrees with
`tokenize` followed by `parse`.
- `parseStr_eq_none_of_tokenize_error`, `runStream_ne_ok_of_tokenize_error`: a lex error
is never an accept.
- `parseStr_sound_exact`: an accepted `parseStr s` gives `tokenize s = .ok ts`, the
positioned tokenization `Lexes s ts`, and a parse tree over exactly `ts`.
- `parseStr_complete`, `parseStr_complete_lexing`, `parseStr_complete_lexes`: a
tokenization of `s` plus a parse tree within the fuel make `parseStr` return its value.
- `parseStr_unambiguous`, `parseStr_unambiguous_lexes`: two derivations of `s` have the
same tokens and value.
- `parseStr_node_spans`: every leaf's span is its lexeme in `s`; every node's span is a
byte range of `s` that ends at the node's last token.
- Conditions: the theorems that use the `Tokenization`/`Lexes` specification need a
lexer with one mode whose rules all stay; the soundness and lex-failure theorems need a
`%eof` token without a value. See `docs/GUARANTEES.md`.
- Lexer modules: `posLexerAgrees` (the positioned byte lexer `lexPos` and `tokenize` give
the same result, for every lexer, multi-mode included), from the `native_decide` check
`byteUnicodeValidated` over every Unicode scalar value; `noEofEmit`; `tokenize_complete`,
`Lexes`, `lexes_unique`, `lexes_eof_last`; `tokenizeByte_toOption`.
- Spans in the verified specification (`Runtime/SpanGrammar.lean`): parse trees over
positioned tokens, actions that receive the spans of their right-hand side, and the
generic theorems transferred to them. Grammars whose actions read spans
(`test/spanuse`) now have all theorems.
- The list macro `separated_list_trailing(sep, X)`: zero or more `X` separated by `sep`,
with an optional trailing `sep` (BNFC's `separator`).
- `leanparsergenerator --list-errors <input.lig>`: prints the parser states that can reject
a token, as a template for per-state messages to `errorMessage`.

### Changed

- Breaking: the generated specification is a `Runtime.SpanGrammar` (`spanGrammarInst`);
`grammarInst` is its derived `Grammar`, with token type `Runtime.Positioned Tok`. Each
`semanticAction<p>` takes the right-hand-side spans first. `ParserStartTree`,
`toStream` and the dependent `parse` take positioned tokens. `parse_eq_dependent`,
`parser_sound`, `parser_complete`, `parser_err_free` and `parser_ne_err` are stated on
positioned tokens with their real spans (`E.parse input`, not
`E.parse (input.map (⟨·, Span.dummy⟩))`); the tree value is
`Runtime.SpanGrammar.ptSem ⟨0⟩ tree`. `parser_unambiguous` takes a start position
(default byte 0) and also equates the spans. `parseStr_*` theorems state
`toStream ts` instead of `toStream (ts.map (·.val))`. No generated theorem takes `hspan`.
- Breaking: the generated specification is over positioned tokens. `parse_eq_dependent`,
`parser_sound`, `parser_complete`, `parser_err_free` and `parser_ne_err` are stated for
the real token spans (`E.parse input`, not `E.parse (input.map (⟨·, Span.dummy⟩))`); a
tree's value is `Runtime.SpanGrammar.ptSem ⟨0⟩ tree`; `parser_unambiguous` also equates
spans and returns a conjunction. Each `semanticAction<p>` takes the right-hand-side
spans first. `%option roundtrip_parser` constructors build trees over
`Positioned.dummy` words.
- Breaking: the span of an empty right-hand side is the empty span at the end of the last
shifted token (byte 0 at the start), as in the Rust backend, instead of `Span.dummy`.
This changes `run` results and span values read by actions, not `parse` values of
span-free grammars.
- Breaking (runtime API): `ExecTable.sem` and `FlatTable.sem` take the stack below the
popped cells (`Nat → Stack V → List V → V`); generated parsers define
`sem := Runtime.Exec.spanSem semV` with `semV : Nat → List Span → List V → V`.
`StackOps` has a new field `endPos` with the law `endPos_eq`; generated stacks define
`Stk.endPos` and `Stk.withTop`. `Runtime.Refinement.ExecRefinement` is over a
`SpanGrammar`, with the new fields `semV` and `sem_agrees`, and `semantic_agrees` takes a
span list. `Runtime.Refinement.Values` is now `Runtime.SpanGrammar.Values`.
- Breaking: parse-tree constructors of `%option roundtrip_parser` build trees over
dummy-positioned words (`(w : List Tok).map Positioned.dummy`), their `_sem` lemmas are
stated for `SpanGrammar.ptSem pe`, and `Certificate.tree` is over
`(tokens ++ [EOF]).map Positioned.dummy` with `semantics : SpanGrammar.ptSem ⟨0⟩ tree = value`.
- The dependent `parse` is evaluated by `decide +kernel` in tests; plain `decide` does not
finish on the derived span semantics.
- Breaking: `parseStr_unambiguous` in `<Base>Verified` takes two derivations of `s`,
`tokenize s = .ok ts₁` with a tree over `ts₁` and `tokenize s = .ok ts₂` with a tree over
`ts₂`, and gives `ts₁ = ts₂` and equal values. It no longer takes two `Lexer.Lexing`
derivations and two trees over an unrelated `ts`; `Lexer.Lexing.unique` gives the
uniqueness of the steps.
- Breaking: `parseStr_node_spans` concludes `Lexer.AllNodeText … s ⟨0⟩ tree` (every leaf
is its lexeme in `s`, every node's span is a substring of `s` with `SpanFacts`) instead of
`Lexes s ts ∧ SpanGrammar.AllSpanFacts ⟨0⟩ tree`.
- Breaking: `parseStr_eq`, `parseStr_eq_dependent`, `parseStr_sound` and
`parseStr_of_tokenize_error` in `<Base>Verified` no longer take `hlex : PosLexerAgrees`.
A caller that passed `hlex` must drop that argument. `PosLexerAgrees` stays as an
`abbrev`, with `posLexerAgrees : PosLexerAgrees`.
- A `noEofEmit` proof for a rule that calls a helper falls back to `simp` with every
`%lexerheader` definition when unfolding the helper alone fails, so a helper that calls
another helper or branches on a `let` no longer makes the lexer fail to compile
(`docs/LEXER.md`).

- The list macros (`list`, `nonempty_list`, `separated_list`, `separated_nonempty_list`)
are left-recursive and produce an `Array` in Lean and a `Vec` in Rust, instead of a
`List` and a `VecDeque`. Grammars whose actions take a macro list as a `List` need
`.toList` (Lean) or `.into()` (Rust, to a `VecDeque`). The LR stack no longer grows with
the length of a list.
- `separated_list(sep, X)` no longer accepts a trailing separator (`a, b,`), as documented;
it is `ε` or `separated_nonempty_list(sep, X)`. A grammar that relies on the trailing
separator, for example one translated from BNFC's `separator`, uses
`separated_list_trailing(sep, X)` instead.
- Generated Lean lexers read their actions from a table, `fastActs`, with
`fastActs_agree` and `tokenize_eq_flat` proving the results unchanged. Lexing is
11–15% faster on the benchmarks.
shifted token, as in the Rust backend, not `Span.dummy`.
- Breaking (runtime API): `ExecTable.sem`/`FlatTable.sem` take the stack below the popped
cells; generated parsers define `sem := Runtime.Exec.spanSem semV`; `StackOps` has a new
field `endPos`.
- Breaking: the list macros are left-recursive and produce `Array` (Lean) / `Vec` (Rust)
instead of `List` / `VecDeque`. The LR stack no longer grows with the length of a list.
- Breaking: `separated_list(sep, X)` no longer accepts a trailing separator; use
`separated_list_trailing`.
- Generated lexers read their actions from a table (`fastActs`); lexing is 11–15% faster.
- The CLI's `wrote …` lines give the dotted module name (`module MyProject.ExprParser`).
- Tests evaluate the dependent `parse` with `decide +kernel`; plain `decide` does not finish.

### Removed

- `Runtime.Exec.SpanIndependent`, `SemSpanFree`, `SemSpanFree.of_split`,
`SpanIndependent.of_run`, `run_erase` and its helpers (`Runtime/ExecStream.lean`),
`Runtime.Exec.runFast_ok_dummy` (`Runtime/ParseEnd.lean`); in generated modules,
`semS`, `semSpanFree`, `parse_spanIndependent`, `SpanIndependent`, `spanIndependent`,
`parse_eq_dependent_spans`, the `hspan` arguments, and the dummy-span form of
`parse_eq_dependent`. They are not needed now that the specification has spans.
- Lexer modules no longer emit `byteValidated{m}` (`Lexer.byteValidate`, a
one-character-per-class check of the byte tables, `native_decide`). It does not imply
that the byte and flat lexers agree, and no theorem used it; `byteUnicodeValidated`
checks every character. It is removed rather than deprecated because nothing depends on
it and versions before 1.0 may change the generated module API. `Lexer.byteValidate`
stays in the library.
- Lexer modules no longer emit `byteValidated{m}`; `byteUnicodeValidated` replaces it.

### Fixed

- The language server reports the tool version (`0.1.0-alpha.1`) instead of `0.1.0`.

### Experimental

- Pull-based Rust API is always emitted: `TokenSource`, `SliceSource`, `NodeSink`,
- Pull-based Rust API, always emitted: `TokenSource`, `SliceSource`, `NodeSink`,
`StreamError`, `parse_from`, `parse_values_from` and, with a `%lexer`, `Lexer`
(`next_token`, `finish`), `parse_str` and `parse_values_str`. `tokenize`, `parse`
and `parse_values` keep their signatures and results. `tokenize` alone is 3–4%
slower, because it is now a loop over `Lexer::next_token`; lex+parse is unchanged.
(`next_token`, `finish`), `parse_str`, `parse_values_str`. `tokenize`, `parse` and
`parse_values` keep their signatures.

## [0.1.0-alpha.1] - 2026-11-27

Expand Down
7 changes: 2 additions & 5 deletions LeanParserGenerator/Construct/Analysis.lean
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ Construct the nullable and FIRST sets by a fixpoint over the grammar.

Both iterate a monotone fixpoint. They feed Pager closure and the generated
`nullableNt`/`firstNt` certificate annotations.

See tests in `test/pager/ConstructionRegressionTest.lean`.
-/
public import LeanParserGenerator.IR.Grammar

Expand All @@ -35,9 +33,8 @@ namespace Construct
open IR

/-- Sorted-insert into a sorted, duplicate-free list of nats (our set repr).
TODO: can we improve that repr and maintain the decidability of the generated
proof obligations?
-/
Another set representation would have to keep the generated proof obligations
decidable. -/
def insertNat (x : Nat) : List Nat → List Nat
| [] => [x]
| y :: ys =>
Expand Down
19 changes: 0 additions & 19 deletions LeanParserGenerator/Emit/Builder.lean
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ namespace Emit.Builder
/-- A Lean string literal for `value` (`reprStr` gives a `"…"` with standard escapes). -/
def strLit (value : String) : String := reprStr value

/-- A `Nat` literal. -/
def natLit (n : Nat) : String := toString n

/-- An `Option Nat` literal (`none` / `some n`). -/
def optNatLit : Option Nat → String
| none => "none"
| some n => s!"some {n}"

/-- Join fragments with a single separator. -/
def joinWith (sep : String) (parts : List String) : String := sep.intercalate parts

Expand Down Expand Up @@ -71,17 +63,6 @@ def publicizeImports (header : String) : String :=
else ln
"\n".intercalate ((header.splitOn "\n").map rewrite)

/-- A Lean array literal `#[a, b, c]` from already-rendered element strings. -/
def arrayLit (elems : List String) : String := "#[" ++ commaSep elems ++ "]"

/-- A `Nat` list literal `[1, 2, 3]`. -/
def natListLit (l : List Nat) : String := listLit (l.map natLit)

/-- A top-level array-table def: `def {name} : Array {ty} := #[{elems}]`. Used for
the flat action/goto/length tables. -/
def arrayDef (name ty : String) (elems : List String) : String :=
s!"def {name} : Array {ty} := " ++ arrayLit elems

/-- Names defined with `def` (or `abbrev`) in header text. -/
def headerDefs (header : String) : List String :=
let ws := (header.replace "\n" " ").splitOn " " |>.filter (!·.isEmpty)
Expand Down
7 changes: 0 additions & 7 deletions LeanParserGenerator/Emit/Codegen.lean
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,6 @@ def emitSem (s : Surface) (e : TyEnv) (augProd : Nat) :
let semDef ← `(command| def $(mkIdent `sem) : $semSig := $semBody)
return #[semV, semDef]

/-- Whether some action names a span variable (`<binder>_span` or `__s<i>`), so its value
may depend on token spans. -/
def actionsReadSpans (s : Surface) : Bool :=
s.rules.any fun r => r.alts.any fun alt =>
alt.elems.zipIdx.any fun (elem, i) =>
(alt.action.splitOn (spanVarName elem i)).length > 1

/-- The `Stk` cell constructor for a `V` constructor: `cUnit` for `unit`, `c<i>` for
`v<i>`. -/
def cellFor (ctor : String) : String :=
Expand Down
5 changes: 3 additions & 2 deletions LeanParserGenerator/Emit/CodegenLexer.lean
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,9 @@ def generateLexerFrom (surface : Surface) (moduleName : String) :
s!" ∀ s, ({moduleName}.fastActs.get 0 r).toAct s = ({moduleName}.fastActs.get 0 r).toAct \"\" :=\n" ++
s!" Lexer.FastActs.textIndep_of_forall₂ {moduleName}.fastActs ({moduleName}.cflat.textDep.getD 0 #[]) 0\n" ++
s!" {textChain}\n\n" ++
s!"/-- A successful `tokenize` is a maximal-munch tokenization of the input, and the spec\n" ++
s!" tokenizer `Lexer.tokenize` returns the same token values. -/\n" ++
s!"/-- If `tokenize` succeeds with `ts`, the input has a maximal-munch tokenization\n" ++
s!" (`Lexer.Tokenization`), and the spec tokenizer `Lexer.tokenize` returns the token values\n" ++
s!" of `ts`. -/\n" ++
s!"theorem {moduleName}.tokenize_sound \{input : String} \{ts : List (Runtime.Positioned {tokTy})}\n" ++
s!" (h : {moduleName}.tokenize input = .ok ts) :\n" ++
s!" Lexer.Tokenization {moduleName}.rules0 input.toList ∧\n" ++
Expand Down
Loading