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
115 changes: 112 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,106 @@ versions may change the CLI, the `.lig` syntax, and the generated module API.
- 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` under the hypothesis `PosLexerAgrees`,
and under `SpanIndependent` when an action reads a span. The stream stops at the first
error, lex or parse.
`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`.

### 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 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
Expand All @@ -33,6 +127,21 @@ versions may change the CLI, the `.lig` syntax, and the generated module API.
`fastActs_agree` and `tokenize_eq_flat` proving the results unchanged. Lexing is
11–15% faster on the benchmarks.

### 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.

### Experimental

- Pull-based Rust API is always emitted: `TokenSource`, `SliceSource`, `NodeSink`,
Expand Down
9 changes: 9 additions & 0 deletions LeanParserGenerator/Emit/Builder.lean
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,13 @@ def natListLit (l : List Nat) : String := listLit (l.map natLit)
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)
let rec go : List String → List String
| "def" :: n :: rest | "abbrev" :: n :: rest => n :: go rest
| _ :: rest => go rest
| [] => []
go ws

end Emit.Builder
Loading