Skip to content

Commit 9e3f79d

Browse files
committed
fix: create well-spanned syntax on verso blocks
TODO
1 parent 70158ef commit 9e3f79d

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/verso/Verso/Doc/Concrete.lean

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ elab "#doc" "(" genre:term ")" title:str "=>" text:completeDocument eoi : term =
161161
Term.elabTerm (← `( ($(doc) : Part $genre))) none
162162

163163

164-
scoped syntax (name := addBlockCmd) block term:max : command
165-
scoped syntax (name := addLastBlockCmd) block term:max str : command
164+
scoped syntax (name := addBlockCmd) block : command
165+
scoped syntax (name := addLastBlockCmd) block : command
166166

167167
/-!
168168
Unlike `#doc` expressions and `#docs` commands, which are elaborated all at once, `#doc` commands
@@ -246,7 +246,7 @@ private meta partial def getTailContext? (source : String) (stx : Syntax) : Opti
246246
Parses each top-level block as either an `addBlockCmd` or an `addLastBlockCmd`. (This is what
247247
Verso uses to replace the command parser.)
248248
-/
249-
private meta def versoBlockCommandFn (genre : Term) (title : String) : ParserFn := fun c s =>
249+
private meta def versoBlockCommandFn : ParserFn := fun c s =>
250250
let iniSz := s.stackSize
251251
let lastPos? := lastVersoEndPosExt.getState c.env
252252
let s := lastPos? |>.map s.setPos |>.getD s
@@ -255,10 +255,8 @@ private meta def versoBlockCommandFn (genre : Term) (title : String) : ParserFn
255255
else
256256
let s := ignoreFn (manyFn blankLine) c s
257257
let s := updateTrailing c s
258-
let s := s.pushSyntax genre
259258
let i := s.pos
260259
if c.atEnd i then
261-
let s := s.pushSyntax (Syntax.mkStrLit title)
262260
s.mkNode ``addLastBlockCmd iniSz
263261
else
264262
s.mkNode ``addBlockCmd iniSz
@@ -281,6 +279,7 @@ be used to thread state between the separate top level blocks. These environment
281279
the state that needs to exist across top-level-block parsing events.
282280
-/
283281
public meta structure DocElabEnvironment where
282+
genreSyntax : Term := ⟨.missing⟩
284283
ctx : DocElabContext := ⟨.missing, mkConst ``Unit, .always, .none⟩
285284
docState : DocElabM.State := { highlightDeduplicationTable := some {} }
286285
partState : PartElabM.State := .init (.node .none nullKind #[])
@@ -323,18 +322,17 @@ in `elabDoc` across three functions: the prelude in `startDoc`, the loop body in
323322
and the postlude in `finishDoc`.
324323
-/
325324

326-
private meta def startDoc (genreSyntax : Term) (title: StrLit) : Command.CommandElabM String := do
325+
private meta def startDoc (genreSyntax : Term) (title: StrLit) : Command.CommandElabM Unit := do
327326
let env ← getEnv
328327
let titleParts ← stringToInlines title
329328
let titleString := inlinesToString env titleParts
330329
let ctx ← Command.runTermElabM fun _ => DocElabContext.fromGenreTerm genreSyntax
331330
let initDocState : DocElabM.State := { highlightDeduplicationTable := .some {} }
332331
let initPartState : PartElabM.State := .init (.node .none nullKind titleParts)
333332

334-
modifyEnv (docEnvironmentExt.setState · ⟨ctx, initDocState, initPartState⟩)
333+
modifyEnv (docEnvironmentExt.setState · ⟨genreSyntax, ctx, initDocState, initPartState⟩)
335334
runPartElabInEnv <| do
336335
PartElabM.setTitle titleString (← titleParts.mapM (elabInline ⟨·⟩))
337-
return titleString
338336

339337
private meta def runVersoBlock (block : TSyntax `block) : Command.CommandElabM Unit := do
340338
runPartElabInEnv <| partCommand block
@@ -344,29 +342,30 @@ private meta def runVersoBlock (block : TSyntax `block) : Command.CommandElabM U
344342
saveRefsInEnv
345343

346344
open PartElabM in
347-
private meta def finishDoc (genreSyntax : Term) (title : StrLit) : Command.CommandElabM Unit:= do
345+
private meta def finishDoc : Command.CommandElabM Unit:= do
348346
let endPos := (← getFileMap).source.rawEndPos
349347
runPartElabInEnv <| do closePartsUntil 0 endPos
350348

351349
let versoEnv := docEnvironmentExt.getState (← getEnv)
352350
let finished := versoEnv.partState.partContext.toPartFrame.close endPos
353351

354-
let n := mkIdentFrom title (← currentDocName)
355-
let doc ← Command.runTermElabM fun _ => finished.toVersoDoc genreSyntax versoEnv.ctx versoEnv.docState versoEnv.partState
356-
let ty ← ``(VersoDoc $genreSyntax)
352+
let n := mkIdent (← currentDocName)
353+
let doc ← Command.runTermElabM fun _ => finished.toVersoDoc versoEnv.genreSyntax versoEnv.ctx versoEnv.docState versoEnv.partState
354+
355+
let ty ← ``(VersoDoc $versoEnv.genreSyntax)
357356
Command.elabCommand (← `(def $n : $ty := $doc))
358357

359358
syntax (name := replaceDoc) "#doc " "(" term ") " str " =>" : command
360359
elab_rules : command
361360
| `(command|#doc ( $genreSyntax:term ) $title:str =>%$tok) => open Lean Parser Elab Command in do
362361
elabCommand <| ← `(open scoped Lean.Doc.Syntax)
363362

364-
let titleString ← startDoc genreSyntax title
363+
startDoc genreSyntax title
365364

366365
-- Sets up basic incremental evaluation of documents by replacing Lean's command-by-command parser
367366
-- with a top-level-block parser.
368367
modifyEnv fun env => originalCatParserExt.setState env (categoryParserFnExtension.getState env)
369-
replaceCategoryParser `command (versoBlockCommandFn genreSyntax titleString)
368+
replaceCategoryParser `command versoBlockCommandFn
370369

371370
if let some stopPos := tok.getTailPos? then
372371
let txt ← getFileMap
@@ -387,7 +386,7 @@ elab_rules : command
387386
-- Edge case: if there's no blocks after the =>, the replacement command parser won't get called,
388387
-- so we detect that case and call finishDoc.
389388
if stopPos.extract txt.source txt.source.rawEndPos |>.all Char.isWhitespace then
390-
finishDoc genreSyntax title
389+
finishDoc
391390

392391
open Command in
393392
/--
@@ -403,16 +402,16 @@ private meta def updatePos (b : TSyntax `block) : CommandElabM Unit := do
403402

404403
@[command_elab addBlockCmd]
405404
public meta def elabVersoBlock : Command.CommandElab
406-
| `(addBlockCmd| $b:block $_:term) => do
405+
| `(addBlockCmd| $b:block) => do
407406
updatePos b
408407
runVersoBlock b
409408
| _ => throwUnsupportedSyntax
410409

411410
@[command_elab addLastBlockCmd]
412411
public meta def elabVersoLastBlock : Command.CommandElab
413-
| `(addLastBlockCmd| $b:block $genre:term $title:str) => do
412+
| `(addLastBlockCmd| $b:block) => do
414413
updatePos b
415414
runVersoBlock b
416415
-- Finish up the document
417-
finishDoc genre title
416+
finishDoc
418417
| _ => throwUnsupportedSyntax

0 commit comments

Comments
 (0)