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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ versions may change the CLI, the `.lig` syntax, and the generated module API.
`roundtrip_lexer` without a `%lexer` block; a lexer rule that matches only the empty
string (one warning instead of two). The warning for a lexer rule that can never win
gives the line of the rule that shadows it.
- A warning when the `eof` rule of the `%lexer` block is not the `%eof` constructor itself
(`Tok.EOF` or `.EOF`), for example a call to a helper function. Such a lexer compiles but
has no `lexes_eof_last`, `word_eq_of_lexes` or `parseStr_sound_exact`.
- The generated modules name the `.lig` in their first comment, and each action is
preceded by a comment with its `.lig` line.
- `--list-errors` no longer lists the `%eof` token next to "end of input".
Expand Down
10 changes: 10 additions & 0 deletions LeanParserGenerator/Frontend/Diagnostics.lean
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The server layer converts byte ranges to LSP line/character positions with
-/
public import LeanParserGenerator.Frontend.File
public import LeanParserGenerator.Emit.Codegen
public import LeanParserGenerator.Emit.CodegenLexer
public import LeanParserGenerator.Lexer.DFA


Expand Down Expand Up @@ -354,6 +355,15 @@ def semanticDiagnostics (s : Surface) : Array LigDiagnostic := Id.run do
reportedLex := reportedLex.push t.name
ds := ds.push (LigDiagnostic.ofSpan t.span .warning
s!"token `{t.name}` is used by the grammar, but no lexer action or `%lexerheader` mentions it, so the lexer never produces it and no input can match the productions that use it")
-- The theorems `lexes_eof_last`, `word_eq_of_lexes` and `parseStr_sound_exact` are emitted
-- only when the `eof` rule is written as the `%eof` constructor itself (`Tok.EOF` or
-- `.EOF`). The check is textual, so an action that builds the token through a helper
-- function silently loses them unless the user is told.
if let some spec := s.lexer then
if let some action := spec.eof then
if !hasError && (Emit.eofCtor s).isSome && !Emit.isEofAction s.tokenType s.eof action then
ds := ds.push (LigDiagnostic.ofSpan s.eofSpan .warning
s!"the `eof` rule of the `%lexer` block is `{action.trimAscii.toString}`, not the constructor `{s.tokenType}.{s.eof}`, so `lexes_eof_last`, `word_eq_of_lexes` and `parseStr_sound_exact` are not generated. Write the rule as `eof => \{ {s.tokenType}.{s.eof} }` to get them")
return ds ++ lexerRuleDiagnostics s ++ deadLexerRuleDiagnostics s

/-! ### Top-level entry point -/
Expand Down
2 changes: 1 addition & 1 deletion docs/LEXER.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ A lexer module also emits `noEofEmit` (no rule emits the `%eof` token) when that
- `tokenize_complete`: if the input has a maximal-munch `Lexer.Lexing` with steps `steps` (each step a rule and a length, `Lexer/Lexing.lean`), `tokenize` succeeds and its token values are `Lexer.lexingTokens lexer lexer.start input.toList steps`. `Lexer.Lexing.unique` states that the steps are determined by the input;
- `Lexes s ts`, the positioned tokenization of `s` (`Lexer.PosLexing`): each token's span `⟨p.offset, q.offset⟩` delimits its lexeme `s.extract p q`, the maximal-munch match at `p`; a rule whose action emits nothing (a skip) consumes the bytes between two tokens; the end-of-input token has the empty span at the end. `Lexer.PosLexing.spans_ordered` states that the spans lie in the input and that each ends at or before the next starts;
- `tokenize_iff_lexes : tokenize s = .ok ts ↔ Lexes s ts`, `lexPos_iff_lexes` (the same for the byte lexer `lexPos`, through `posLexerAgrees`), and `lexes_unique`;
- `lexes_eof_last`, when the `%eof` token carries no value and the `eof` rule emits it (`eof => { Tok.EOF }` or `eof => { .EOF }`): `Lexes s ts` gives `ts = ts0 ++ [⟨EOF, Span.point s.endPos.offset⟩]` with no `EOF` in `ts0` (from `noEofEmit`). The verified module uses it to show that an accepting parse tree covers all of `ts`.
- `lexes_eof_last`, when the `%eof` token carries no value and the `eof` rule emits it (`eof => { Tok.EOF }` or `eof => { .EOF }`): `Lexes s ts` gives `ts = ts0 ++ [⟨EOF, Span.point s.endPos.offset⟩]` with no `EOF` in `ts0` (from `noEofEmit`). The verified module uses it to show that an accepting parse tree covers all of `ts`. Any other form of the `eof` action, such as a call to a helper function, compiles but omits `lexes_eof_last` and the theorems built on it, and the generator warns.

`noEofEmit` is one proof per rule. A constructor application is closed by `nofun`. For a rule whose action calls a helper `h` (for example keyword folding), the proof tries two tactics in order:

Expand Down
11 changes: 11 additions & 0 deletions test/cli/CliTest.lean
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,17 @@ def main : IO UInt32 := do
let d ← fresh [("g.lig", "\uFEFF" ++ lexerGrammar)]
let r ← run d ["g.lig"]
check "a byte order mark is skipped" (r.code == 0 && (← has d "GParser.lean")) r.err
checkWarning "an eof action that is not the %eof constructor"
(lexerGrammar.replace "eof => { Tok.EOF }" "eof => { mkEof }")
"the `eof` rule of the `%lexer` block is `mkEof`, not the constructor `Tok.EOF`, so `lexes_eof_last`" "5:"
for (name, act) in [("Tok.EOF", "Tok.EOF"), (".EOF", ".EOF"), ("a parenthesized Tok.EOF", "(Tok.EOF)")] do
let d ← fresh [("g.lig", lexerGrammar.replace "eof => { Tok.EOF }" s!"eof => \{ {act} }")]
let r ← run d ["g.lig"]
check s!"the eof action {name} prints no warning" (r.code == 0 && r.err.isEmpty) r.err
let d ← fresh [("g.lig", lexerGrammar.replace " eof => { Tok.EOF }\n" "")]
let r ← run d ["g.lig"]
check "a lexer without an eof rule prints no eof-action warning"
(r.code == 0 && !contains r.err "`eof` rule of the `%lexer` block is") r.err
checkWarning "a token that no lexer action mentions"
(lexerGrammar.replace "{ Tok.PLUS }" "{ Tok.NUM 0 }")
"token `PLUS` is used by the grammar, but no lexer action" "3:8"
Expand Down