blob: 9ab0737e6cd1241b288c03147757a8330647595b [file] [edit]
/-
Copyright (c) 2023-2025 Lean FRO LLC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: David Thrane Christiansen
-/
module
prelude
public import Lean.DocString.Syntax
import Init.While
import Init.Data.Range.Polymorphic.Iterators
import Init.Data.String.Csimp
set_option linter.missingDocs true
namespace Lean.Doc.Parser
open Lean Parser
local instance : Coe Char ParserFn where
coe := chFn
/--
An atomic parser that parses even more atomically than the default one. This can be needed when
doing extensive error recovery. While `Lean.Parser.atomicFn` resets the position, this additionally
rewinds `p`'s recovered errors and syntax stack.
-/
def atomicFn' (p : ParserFn) : ParserFn := fun c s =>
let iniPos := s.pos
let iniSz := s.stxStack.size
let iniErrs := s.recoveredErrors
match p c s with
| ⟨stack, lhsPrec, _, cache, some msg, _⟩ =>
⟨stack.shrink iniSz, lhsPrec, iniPos, cache, some msg, iniErrs⟩
| other => other
partial def atLeastAux (n : Nat) (p : ParserFn) : ParserFn := fun c s => Id.run do
let iniSz := s.stackSize
let iniPos := s.pos
let mut s := p c s
if s.hasError then
return if iniPos == s.pos && n == 0 then s.restore iniSz iniPos else s
if iniPos == s.pos then
return s.mkUnexpectedError "invalid 'atLeast' parser combinator application, parser did not consume anything"
if s.stackSize > iniSz + 1 then
s := s.mkNode nullKind iniSz
atLeastAux (n - 1) p c s
def atLeastFn (n : Nat) (p : ParserFn) : ParserFn := fun c s =>
let iniSz := s.stackSize
let s := atLeastAux n p c s
s.mkNode nullKind iniSz
/--
A parser that does nothing.
-/
public def skipFn : ParserFn := fun _ s => s
def eatSpaces := takeWhileFn (· == ' ')
def repFn : Nat → ParserFn → ParserFn
| 0, _ => skipFn
| n+1, p => p >> repFn n p
/--
Describes a character for an error message. Newline and tab become “newline” and “tab”, while other
characters are quoted.
-/
def describeChar (ch : Char) : String :=
if ch == '\n' then "newline"
else if ch == '\t' then "tab"
else s!"'{ch}'"
/--
Reads one character that satisfies `p`. If the character does not satisfy `p`, the error describes
the unexpected character and mentions `expected` as what would have been read in its place.
-/
def expectFn (p : Char → Bool) (expected : String) : ParserFn := fun c s =>
let i := s.pos
if h : c.atEnd i then s.mkEOIError [expected]
else if p (c.get' i h) then s.next' c i h
else s.mkUnexpectedError s!"unexpected {describeChar (c.get' i h)}" [expected]
/--
Reads the character `c` as a token, consuming the whitespace after it when `trailingWs` is set. If
the expected character is not present, the resulting error describes it and gives `c` as what was
expected.
-/
def expectChFn (c : Char) (trailingWs := false) : ParserFn :=
rawFn (expectFn (· == c) s!"'{c}'") trailingWs
/--
Runs `p`. An error that `p` reports at the position where it started becomes `expected msg`, so
that the message names what was being read rather than what an inner parser expected. An error
after `p` consumed input is unchanged.
-/
def expectedFn (msg : String) (p : ParserFn) : ParserFn := fun c s =>
let iniPos := s.pos
let s := p c s
if s.hasError && s.pos == iniPos then
s.setError { expected := [msg] }
else s
partial def atMostAux (n : Nat) (p : ParserFn) (msg : String) : ParserFn :=
fun c s => Id.run do
let iniSz := s.stackSize
let iniPos := s.pos
if n == 0 then return notFollowedByFn p msg c s
let mut s := p c s
if s.hasError then
return if iniPos == s.pos then s.restore iniSz iniPos else s
if iniPos == s.pos then
return s.mkUnexpectedError "invalid 'atMost' parser combinator application, parser did not \
consume anything"
if s.stackSize > iniSz + 1 then
s := s.mkNode nullKind iniSz
atMostAux (n - 1) p msg c s
def atMostFn (n : Nat) (p : ParserFn) (msg : String) : ParserFn := fun c s =>
let iniSz := s.stackSize
let s := atMostAux n p msg c s
s.mkNode nullKind iniSz
/--
Whether `c` may appear in the name of a footnote or a link reference. A name is written literally,
so a backslash denotes itself elsewhere and is not part of a name.
-/
-- `closeRefNameWith` describes these characters in the message it reports. If this set changes,
-- make sure to update it as well.
public def isRefNameChar : Char → Bool
| '[' | ']' | '^' | '\\' | '\n' | '\t' => false
| _ => true
/-- Like `satisfyFn`, but allows any escape sequence through -/
partial def satisfyEscFn (p : Char → Bool)
(errorMsg : String := "unexpected character") :
ParserFn := fun c s =>
let i := s.pos
if h : c.atEnd i then s.mkEOIError
else if c.get' i h == '\\' then
let s := s.next' c i h
let i := s.pos
if h : c.atEnd i then s.mkEOIError
else s.next' c i h
else if p (c.get' i h) then s.next' c i h
else s.mkUnexpectedError errorMsg
partial def takeUntilEscFn (p : Char → Bool) : ParserFn := fun c s =>
let i := s.pos
if h : c.atEnd i then s
else if c.get' i h == '\\' then
let s := s.next' c i h
let i := s.pos
if h : c.atEnd i then s.mkEOIError
else takeUntilEscFn p c (s.next' c i h)
else if p (c.get' i h) then s
else takeUntilEscFn p c (s.next' c i h)
/--
Parses as `p`, but discards the result.
-/
public def ignoreFn (p : ParserFn) : ParserFn := fun c s =>
let iniSz := s.stxStack.size
let s' := p c s
s'.shrinkStack iniSz
/--
Extends the trailing whitespace of the last token in `stx` to `stopPos`, returning the extended
syntax and a flag. The flag is `true` when `stx` contains a token that can be extended. It is
`false` otherwise, and `stx` comes back unchanged.
-/
partial def extendLastTrailing (input : String) (stopPos : String.Pos.Raw) (stx : Syntax) :
Syntax × Bool :=
(go stx).run false
where
/-- The traversal sets the state to `true` when it extends a token, and stops there. -/
go : Syntax → StateM Bool Syntax
| .node info k args => do
let mut args := args
let mut i := args.size
while i > 0 do
i := i - 1
args ← args.modifyM i go
if ← get then break
return .node info k args
| .atom (.original lead pos trail stop) val => do
set true
return .atom (.original lead pos ⟨input, trail.startPos, stopPos⟩ stop) val
| .ident (.original lead pos trail stop) raw x pre => do
set true
return .ident (.original lead pos ⟨input, trail.startPos, stopPos⟩ stop) raw x pre
| stx => return stx
/--
Extends the trailing whitespace of the most recent token among the stack elements above `base` to
`stopPos`. The flag is `true` when one of those elements contains a token. It is `false` when none
does, and the stack comes back unchanged.
-/
def extendTrailingAbove (input : String) (base : Nat) (stopPos : String.Pos.Raw) (s : ParserState) :
ParserState × Bool := Id.run do
-- The most recent token can sit below tokenless elements (an empty optional, a recovery stub).
-- The loop pops elements until one can record the whitespace, and restores them after. Elements
-- are popped prior to updating them to encourage in-place updates.
let mut s := s
let mut saved : Array Syntax := #[]
let mut extended := false
while s.stxStack.size > base do
let top := s.stxStack.back
s := s.popSyntax
let (top, ext) := extendLastTrailing input stopPos top
if ext then
s := s.pushSyntax top
extended := true
break
else
saved := saved.push top
let mut i := saved.size
while i > 0 do
i := i - 1
s := s.pushSyntax saved[i]!
return (s, extended)
/--
Runs `ws` and records what it consumes as the trailing whitespace of the most recent token among
the stack elements above `base`. On ordinary parses every token records its own trailing whitespace
as it is parsed, so `wsFallback` consumes nothing. It matters only for error recovery. When the
token that would have recorded the whitespace is lost, `wsFallback` still records the input, so that
the surrounding parse can reprint it.
-/
def wsFallback (ws : ParserFn) (base : Nat) : ParserFn := fun c s =>
let startPos := s.pos
let s := ws c s
if s.hasError || s.pos == startPos then s
else (extendTrailingAbove c.inputString base s.pos s).1
/--
Runs `ws` and adds what it consumes to the trailing whitespace of the last token of the element on
top of the stack, so it runs directly after the parser that pushed that element. If that element
contains no token, the input `ws` consumes goes unrecorded and the tree stops covering the source.
-/
def withTrailing (ws : ParserFn) : ParserFn := fun c s =>
wsFallback ws (s.stxStack.size - 1) c s
/--
Consumes the whitespace that separates a block from what follows it: the rest of the current line,
any blank lines, and the indentation of the line where content resumes.
Tabs are not valid whitespace characters, so they are not consumed. The next block actually throws
the error.
-/
public def lineTailWsFn : ParserFn := takeWhileFn (fun ch => ch == ' ' || ch == '\n')
/--
Consumes up to `col` space characters, so that a line's indentation becomes part of the whitespace
between tokens instead of line content.
-/
def eatIndent (col : Nat) : ParserFn := fun c s => Id.run do
let mut s := s
for _ in 0...col do
let i := s.pos
if h : c.atEnd i then return s
else if c.get' i h == ' ' then s := s.next' c i h
else return s
return s
/--
Records the boundary spaces of the inline code content on top of the syntax stack as whitespace.
Where the content begins and ends with a space and contains another character, the first line's
opening space becomes the trailing whitespace of the opening delimiter and the last line's closing
space becomes the trailing whitespace of the last line's token, so that the tokens' text is the code
the element denotes.
-/
def codeBoundarySpacesToWs : ParserFn := fun c s =>
match s.stxStack.back with
| .node info ``versoCode #[.node info' `null lines] =>
let text := lines.foldl (fun acc l => acc ++ (Syntax.isLit? versoCodeLineKind l).getD "") ""
-- The check guarantees a space at each end of the text. The line parser never produces an empty
-- token, so those spaces are the first token's first character and the last token's last one.
if !versoCodeBoundarySpaces text then s
else if !lines.all originalLine then
s.mkError "internal error: expected code lines with original source info"
else
-- A single line is both the first and the last line, and receives both edits in turn.
let newLines := lines.modify 0 dropFirst |>.modify (lines.size - 1) dropLast
match lines[0]?.bind lineStart with
| none => s.mkError "internal error: expected code lines with original source info"
| some start =>
-- The opening delimiter is the element below the content, and records the space that
-- follows it.
let s := s.popSyntax
let (s, extended) := extendTrailingAbove c.inputString (s.stxStack.size - 1) (start + ' ') s
if extended then s.pushSyntax (.node info ``versoCode #[.node info' `null newLines])
else s.mkError "internal error: expected a token below the inline code content"
| other => s.mkError s!"internal error: expected inline code content on stack top, got {other}"
where
originalLine : Syntax → Bool
| .node _ ``versoCodeLine #[.atom (.original ..) _] => true
| _ => false
lineStart : Syntax → Option String.Pos.Raw
| .node _ _ #[.atom (.original (pos := start) ..) _] => some start
| _ => none
dropFirst : Syntax → Syntax
| .node info k #[.atom (.original leading start trailing stop) val] =>
.node info k #[.atom
(.original { leading with startPos := start + ' ', stopPos := start + ' ' } (start + ' ')
trailing stop)
(val.drop 1).copy]
| stx => stx
dropLast : Syntax → Syntax
| .node info k #[.atom (.original leading start trailing stop) val] =>
.node info k #[.atom
(.original leading start { trailing with startPos := trailing.startPos - ' ' } (stop - ' '))
(val.dropEnd 1).copy]
| stx => stx
/--
Whether the character before the current position is a newline. Any parser may have consumed that
newline, including the enclosing parser before it invoked this one.
-/
def afterLineEnd (c : ParserContext) (s : ParserState) : Bool :=
s.pos > 0 && c.get (c.prev s.pos) == '\n'
/--
The trailing whitespace of a line of code: the indentation of the line that follows, up to `col`
characters. Indentation past `col` is code content, and so is the indentation of a line that the
token before it did not end.
-/
def codeLineTailWs (col : Nat) : ParserFn := fun c s =>
if afterLineEnd c s then eatIndent col c s else s
/--
Whether the most recent token's trailing whitespace ends at the current position and contains a
newline, which means that the token consumed its line's end.
-/
def consumedLineEnd (s : ParserState) : Bool := Id.run do
let mut i := s.stxStack.size
while i > 0 do
i := i - 1
if let .original _ _ trail _ := (s.stxStack.get! i).getTailInfo then
return trail.stopPos == s.pos && !trail.all (· != '\n')
return false
/--
Whether the line the parser is on has ended before the current position, either because the
character before it is a newline or because the most recent token's trailing whitespace reaches it
and contains one.
-/
def atNewLine (c : ParserContext) (s : ParserState) : Bool :=
afterLineEnd c s || consumedLineEnd s
/--
Runs `p` unless the most recent token consumed its line's end as trailing whitespace.
-/
def unlessLineEndConsumed (p : ParserFn) : ParserFn := fun c s =>
if consumedLineEnd s then s else p c s
/--
If the current position is a newline, consumes it and runs `blanks`. Otherwise consumes nothing.
-/
def optNlWs (blanks : ParserFn := skipFn) : ParserFn := fun c s =>
if h : c.atEnd s.pos then s
else if c.get' s.pos h == '\n' then blanks c (s.next' c s.pos h)
else s
/--
Consumes the rest of the document if it consists only of whitespace. Otherwise, does nothing.
-/
def docEndWs : ParserFn := fun c s =>
if s.pos < c.endPos &&
(Substring.Raw.mk c.inputString s.pos c.endPos).all Char.isWhitespace then
s.setPos c.endPos
else s
def asTokenAux (trailing : ParserFn) (startPos : String.Pos.Raw) : ParserFn := fun c s =>
let stopPos := s.pos
let leading := c.mkEmptySubstringAt startPos
let val := c.extract startPos stopPos
-- The token consumes its own trailing whitespace as it is built. If the whitespace parser fails,
-- `asTokenAux` pushes the token with empty trailing whitespace and keeps the error.
let s := trailing c s
let trail :=
if s.hasError then c.mkEmptySubstringAt stopPos
else c.substring stopPos s.pos
s.pushSyntax <| .atom (SourceInfo.original leading startPos trail stopPos) val
/--
Matches an arbitrary parser and pushes the consumed input as a `Syntax.atom`. The `trailing` parser
runs after the token's content, and the input it consumes becomes the token's trailing whitespace.
-/
public def asTokenFn (p : ParserFn) (trailing : ParserFn := skipFn) : ParserFn := fun c s =>
let startPos := s.pos
let iniSz := s.stxStack.size
let s := p c s
if s.hasError then s
else asTokenAux trailing startPos c (s.shrinkStack iniSz)
def _root_.Lean.Parser.ParserContext.currentColumn
(c : ParserContext) (s : ParserState) : Nat :=
c.fileMap.toPosition s.pos |>.column
def withCurrentColumn (p : Nat → ParserFn) : ParserFn := fun c s =>
p (c.currentColumn s) c s
/--
Runs `p` with `pos` as the saved position. The column checks `checkIndentGe` and `checkIndentEq`
compare the column where they run against the column of that position.
-/
def withPositionAt (pos : String.Pos.Raw) (p : ParserFn) : ParserFn :=
adaptCacheableContextFn ({ · with savedPos? := pos }) p
/-- Runs `p` with the current position saved. -/
def withPositionHere (p : ParserFn) : ParserFn := fun c s => withPositionAt s.pos p c s
/--
Parses a marker and the contents it introduces.
`marker` reads the marker as a token, `ws` reads the whitespace between the marker and the contents
as that token's trailing whitespace, and `contents` reads the contents with the position just after
the marker saved, which is the column they line up with.
The marker is read atomically.
-/
def afterMarker (marker ws contents : ParserFn) : ParserFn :=
atomicFn' marker >> withPositionHere (withTrailing ws >> contents)
/--
Runs `p` with no saved position. A column check succeeds when there is none, so `p` starts
unconstrained and stays so until something inside it saves a position.
-/
def withNoPosition (p : ParserFn) : ParserFn :=
adaptCacheableContextFn ({ · with savedPos? := none }) p
/-- Fails unless the current column is at least `col`. -/
def checkIndent (col : Nat) : ParserFn := fun c s =>
if c.currentColumn s ≥ col then s else s.mkErrorAt s!"indentation at {col}" s.pos
/--
Fails unless the current column is at least the saved position's, reporting `what` and that column.
Succeeds when no position is saved.
-/
def checkIndentGe (what : String) : ParserFn := fun c s =>
match c.savedPos? with
| none => s
| some savedPos =>
let col := c.fileMap.toPosition savedPos |>.column
if c.currentColumn s ≥ col then s
else s.mkError s!"{what} with indentation at least {col}"
/--
Fails unless the current column is the saved position's, reporting `what` and that column. Succeeds
when no position is saved.
-/
def checkIndentEq (what : String) : ParserFn := fun c s =>
match c.savedPos? with
| none => s
| some savedPos =>
let col := c.fileMap.toPosition savedPos |>.column
if c.currentColumn s == col then s
else s.mkError s!"{what} at column {col}"
/-- Runs `p` with the position at which it starts. -/
def withStartPos (p : String.Pos.Raw → ParserFn) : ParserFn := fun c s => p s.pos c s
/--
An atom containing the input between `startPos` and `stopPos`, at that range, with no leading or
trailing whitespace.
-/
def atomAt (c : ParserContext) (startPos stopPos : String.Pos.Raw) : Syntax :=
.atom
(.original (c.mkEmptySubstringAt startPos) startPos (c.mkEmptySubstringAt stopPos) stopPos)
(c.extract startPos stopPos)
/--
The error that reports the construct `what` as unterminated. It points at the opening delimiter,
which extends from `openPos` to `openStop`, and expects those characters again as the closer.
`nameOpenerLine` adds the line the opener is on to the message.
-/
def unterminatedError (c : ParserContext) (openPos openStop : String.Pos.Raw) (what : String)
(nameOpenerLine := false) : Error :=
let opener := atomAt c openPos openStop
let what :=
if nameOpenerLine then s!"{what} opened on line {(c.fileMap.toPosition openPos).line}"
else what
{ unexpectedTk := opener, unexpected := s!"unterminated {what}",
expected := [s!"'{opener.getAtomVal}'"] }
/--
At the end of the input, reports the construct `what` as unterminated with `unterminatedError`.
Elsewhere this parser succeeds and consumes nothing.
-/
def unterminatedAtEnd (openPos openStop : String.Pos.Raw) (what : String)
(nameOpenerLine := false) : ParserFn := fun c s =>
if c.atEnd s.pos then
s.setError (unterminatedError c openPos openStop what nameOpenerLine) |>.pushSyntax .missing
else s
/-- The end of the sequence of `ch` that begins at `pos`. -/
partial def findCharSeqEnd (ictx : InputContext) (ch : Char) (pos : String.Pos.Raw) :
String.Pos.Raw :=
if ictx.atEnd pos || ictx.get pos != ch then pos else findCharSeqEnd ictx ch (ictx.next pos)
/--
When inline code has failed to parse, finds the sequence of backticks to report as "too many
backticks". The result is the start and end of that sequence.
The search covers only the opener's line, from `start`, which is the end of the opener, to the end
of that line. Inline code may continue onto later lines, where a longer sequence of backticks is
usually the opening fence of a code block. An error there would blame a valid block and be
confusing. An over-long sequence on the opener's line is a miscounted closer.
A sequence with fewer than `count` backticks, where `count` is the length of the opener, is part of
the content. The first sequence with at least `count` backticks is where the code would close, and
it is the result if it is longer than `count`. Otherwise the result is `none`, and the caller
reports the code as unterminated.
-/
def longerBackticks? (c : ParserContext) (count : Nat) (start : String.Pos.Raw) :
Option (String.Pos.Raw × String.Pos.Raw) := do
let mut i := start
while !c.atEnd i && c.get i != '\n' do
if c.get i == '`' then
let seqStop := findCharSeqEnd c '`' i
let n := i.byteDistance seqStop
if n ≥ count then
guard (n > count)
return (i, seqStop)
i := seqStop
else
i := c.next i
failure
/--
The error for inline code whose opener, which extends from `openPos` to `openStop` and has `count`
backticks, has no closer. If there is a sequence of backticks on the opener's line that is longer
than the opener, then it is reported as the error; otherwise, the error is an unterminated inline
code at the opener.
-/
def unclosedCodeError (c : ParserContext) (openPos openStop : String.Pos.Raw) (count : Nat) :
Error :=
match longerBackticks? c count openStop with
| some (seqStart, seqStop) =>
{ unexpectedTk := atomAt c seqStart seqStop,
unexpected := s!"too many backticks: expected {count} but got {seqStart.byteDistance seqStop}" }
| none => unterminatedError c openPos openStop "inline code"
/--
At the end of the input, reports the construct `what` as unterminated, naming the line its opening
`delimiter` is on (the delimiter is at `openPos`). Elsewhere, this parser succeeds and consumes
nothing.
-/
def unterminatedDelimiterAtEnd (openPos : String.Pos.Raw) (delimiter what : String) : ParserFn :=
unterminatedAtEnd openPos (openPos + delimiter) what (nameOpenerLine := true)
/--
Whether a nestable block may open at the current position.
This is the case when the current line contains nothing but spaces and nestable block markers prior to the current position.
-/
def atBlockStart (c : ParserContext) (s : ParserState) : Bool :=
let position := c.fileMap.toPosition s.pos
let lineStart := c.fileMap.lineStart position.line
Id.run do
let mut iter := c.inputString.pos! lineStart
while h : iter.offset < s.pos && ¬iter.IsAtEnd && iter.offset < c.endPos do
have h : ¬iter.IsAtEnd := by simp at h; exact h.1.2
if (iter.get h).isDigit then
while h : ¬iter.IsAtEnd && iter.get!.isDigit && iter.offset < s.pos do
iter := iter.next (by simp at h; exact h.1.1)
if h : iter.IsAtEnd then return false
else if iter.get h == '.' || iter.get h == ')' then iter := iter.next h
else if iter.get h == ' ' then iter := iter.next h
else if iter.get h == '>' then iter := iter.next h
else if iter.get h == '*' then iter := iter.next h
else if iter.get h == '+' then iter := iter.next h
else if iter.get h == '-' then iter := iter.next h
else return false
true
def onlyBlockOpeners : ParserFn := fun c s =>
if atBlockStart c s then s
else s.mkErrorAt "beginning of line or sequence of nestable block openers" s.pos
def nl := satisfyFn (· == '\n') "newline"
/--
Reads the end of a line, which is a newline or the end of the input.
-/
def lineEnd : ParserFn := fun c s =>
if c.atEnd s.pos then s
else expectFn (· == '\n') "newline" c s
def pushMissing : ParserFn := fun _c s =>
s.pushSyntax .missing
def strFn (str : String) : ParserFn := asTokenFn <| fun c s =>
let rec go (iter : str.Pos) (s : ParserState) :=
if h : iter.IsAtEnd then s
else
let ch := iter.get h
go (iter.next h) <| satisfyFn (· == ch) ch.toString c s
termination_by iter
let iniPos := s.pos
let iniSz := s.stxStack.size
let s := go str.startPos s
if s.hasError then s.mkErrorAt s!"'{str}'" iniPos (some iniSz) else s
/--
Ordered lists may have two styles of marker, with trailing dots or parentheses.
-/
public inductive OrderedListType where
/-- Items like `1.` -/
| numDot
/-- Items like `1)` -/
| parenAfter
deriving Repr, BEq, DecidableEq
public instance : Ord OrderedListType where
compare
| .numDot, .numDot => .eq
| .numDot, .parenAfter => .lt
| .parenAfter, .numDot => .gt
| .parenAfter, .parenAfter => .eq
def OrderedListType.all : List OrderedListType :=
[.numDot, .parenAfter]
theorem OrderedListType.all_complete : ∀ x : OrderedListType, x ∈ all := by
unfold all; intro x; cases x <;> repeat constructor!
/--
Unordered lists may have three markers: asterisks, dashes, or pluses.
-/
public inductive UnorderedListType where
/-- Items like `*` -/
| asterisk
/-- Items like `-` -/
| dash
/-- Items like `+` -/
| plus
deriving Repr, BEq, DecidableEq
public instance : Ord UnorderedListType where
compare
| .asterisk, .asterisk => .eq
| .asterisk, _ => .lt
| .dash, .asterisk => .gt
| .dash, .dash => .eq
| .dash, .plus => .lt
| .plus, .plus => .eq
| .plus, _ => .gt
def UnorderedListType.all : List UnorderedListType :=
[.asterisk, .dash, .plus]
theorem UnorderedListType.all_complete : ∀ x : UnorderedListType, x ∈ all := by
unfold all; intro x; cases x <;> repeat constructor!
def unorderedListMarker (type : UnorderedListType) (trailing : ParserFn := skipFn) :
ParserFn :=
nodeFn ``listMarker <| asTokenFn
(match type with
| .asterisk => chFn '*'
| .dash => chFn '-'
| .plus => chFn '+')
trailing
def orderedListMarker (type : OrderedListType) (trailing : ParserFn := skipFn) : ParserFn :=
nodeFn ``listMarker <| asTokenFn
(takeWhile1Fn (·.isDigit) "digits" >>
match type with
| .numDot => chFn '.'
| .parenAfter => chFn ')')
trailing
def unorderedMarkersFn := atomicFn' (go UnorderedListType.all)
where
go
| [] => fun _ s => s.mkError "no list type"
| [x] => atomicFn' (unorderedListMarker x)
| x :: xs => atomicFn' (unorderedListMarker x) <|> go xs
def numberingFn := atomicFn' (go OrderedListType.all)
where
go
| [] => fun _ s => s.mkError "no list type"
| [x] => atomicFn' (orderedListMarker x)
| x :: xs => atomicFn' (orderedListMarker x) <|> go xs
/--
Parses a character that's allowed as part of inline text. This resolves escaped characters and
performs limited lookahead for characters that only begin a different inline as part of a sequence.
-/
public def inlineTextCharFn : ParserFn := fun c s =>
let i := s.pos
if h : c.atEnd i then s.mkEOIError
else
let curr := c.get' i h
match curr with
| '\\' =>
let s := s.next' c i h
let i := s.pos
if h : c.atEnd i then s.mkEOIError
else s.next' c i h
| '\n' => s.mkUnexpectedErrorAt "unexpected newline" i
| '*' | '_' | '[' | ']' | '{' | '}' | '`' =>
(s.setPos i).mkUnexpectedError s!"unexpected '{curr}' (use '\\{curr}' to escape)" ["text"]
| '!' =>
let s := s.next' c i h
let i' := s.pos
if h : c.atEnd i' then s
else if c.get' i' h == '['
then s.mkUnexpectedErrorAt "![" i
else s
| '$' =>
let s := s.next' c i h
let i' := s.pos
if h : c.atEnd i' then
s
else if c.get' i' h == '`' then
s.mkUnexpectedErrorAt "$`" i
else if c.get' i' h == '$' then
let s := s.next' c i' h
let i' := s.pos
if h : c.atEnd i' then
s
else if c.get' i' h == '`' then
s.mkUnexpectedErrorAt "$$`" i
else s
else s
| _ => s.next' c i h
/--
Parses block opener prefixes. If this parser succeeds at the beginning of a line, then a special
block is beginning.
-/
public def blockOpenerFn := atomicFn' <|
eatSpaces >>
(atomicFn' ((unorderedMarkersFn >> chFn ' ')) <|> -- Unordered list
atomicFn' ((numberingFn >> chFn ' ')) <|> -- Ordered list
atomicFn' (strFn ": ") <|> -- Description list item
atomicFn' (atLeastFn 3 (chFn ':')) <|> -- Directive
atomicFn' (atLeastFn 3 (chFn '`')) <|> -- Code block
atomicFn' (strFn "%%%") <|> -- Metadata
atomicFn' (chFn '>')) -- Block quote
/--
Whether the line after the newline at the current position continues a paragraph. It continues when
the line begins with content other than a block opener.
If the current position is not a newline, returns `false`.
-/
def lineContinues (c : ParserContext) (s : ParserState) : Bool := Id.run do
let mut s := s
let i := s.pos
if h : c.atEnd i then return false
else if c.get' i h != '\n' then return false
else s := s.next' c i h
repeat
let i := s.pos
if h : c.atEnd i then return true
else if c.get' i h == ' ' then s := s.next' c i h
else if c.get' i h == '\n' then return false
else break
return (blockOpenerFn c s).hasError
/--
The trailing whitespace of a token that may end a block's inline content, which belongs to a
paragraph, a header, a description list's term, or a footnote definition.
At a newline, the content carries on to the next line when `allowNewlines` is set and that line
begins content other than a block opener. This parser then consumes nothing, and a line break inline
token consumes the newline. Otherwise, the content ends here, and `blockTailWs` consumes the newline
and the blank lines after it, up to and including the last newline. If the current position is not a
newline, it does nothing.
-/
def blockTailWs (allowNewlines : Bool) : ParserFn := fun c s =>
if h : c.atEnd s.pos then s
else if c.get' s.pos h != '\n' then s
else if allowNewlines && lineContinues c s then s
else lineTailWsFn c s
/--
Parses an argument value, which may be a string, an identifier, or a numeral. The value's token
records the input consumed by `trailing` as its trailing whitespace.
-/
public def valFn (trailing : ParserFn := skipFn) : ParserFn := fun c s =>
if h : c.atEnd s.pos then
s.mkEOIError
else
let ch := c.get' s.pos h
let i := s.stackSize
let finish (s : ParserState) : ParserState :=
if s.hasError then s else withTrailing trailing c s
if ch == '\"' then
let startPos := s.pos
let s := strLitFnAux startPos false c (s.next' c startPos h)
let s := finish (if s.hasError then s else onOneLine startPos c s)
s.mkNode ``ArgVal.str i
else if isIdFirst ch || isIdBeginEscape ch then
let s := finish (rawIdentFn (includeWhitespace := false) c s)
s.mkNode ``ArgVal.ident i
else if ch.isDigit then
let s := finish (numberFnAux false c s)
s.mkNode ``ArgVal.num i
else
s.mkError "identifier, string, or number"
where
onOneLine (startPos : String.Pos.Raw) : ParserFn := fun c s => Id.run do
let mut i := startPos
while i < s.pos do
if c.get i == '\n' then
-- The position stays after the literal, so the argument list resumes behind it.
return s.mkUnexpectedTokenError "a string argument on one line" (iniPos := s.pos)
i := c.next i
return s
def withCurrentStackSize (p : Nat → ParserFn) : ParserFn := fun c s =>
p s.stxStack.size c s
/-- Matches the character indicated, pushing nothing to the stack in case of success -/
def skipChFn (c : Char) : ParserFn :=
expectFn (· == c) s!"'{c}'"
def skipToNewline : ParserFn :=
takeUntilFn (· == '\n')
def skipRestOfLine : ParserFn :=
skipToNewline >> (eoiFn <|> nl)
def skipBlock : ParserFn :=
skipToNewline >> manyFn nonEmptyLine >> takeWhileFn (· == '\n')
where
nonEmptyLine : ParserFn :=
atomicFn' <|
chFn '\n' >>
takeWhileFn (fun c => c.isWhitespace && c != '\n') >>
satisfyFn (!·.isWhitespace) "non-whitespace" >> skipToNewline
/--
If the state has an error, consumes input with `recover` until parsing may continue, and records the
error at the position where it occurred.
-/
def recoverPendingError (recover : ParserFn) : ParserFn := fun c s =>
if let some msg := s.errorMsg then
let errPos := s.pos
let s' := recover c { s with errorMsg := none }
if s'.hasError then s
else { s with
pos := s'.pos,
errorMsg := none,
stxStack := s'.stxStack,
recoveredErrors := s.recoveredErrors.push (errPos, s'.stxStack, msg)
}
else s
/--
Runs `p`, and on failure consumes input with `recover` until parsing may continue. The error is
recorded where `p` stopped, so that it marks the input that could not be read rather than the input
that recovery passed over.
-/
def recoverAtErrPos (p recover : ParserFn) : ParserFn := fun c s =>
recoverPendingError recover c (p c s)
@[inherit_doc recoverAtErrPos]
public def recoverBlockAtErrPos (p : ParserFn) : ParserFn := recoverAtErrPos p (ignoreFn skipBlock)
@[inherit_doc recoverAtErrPos]
def recoverWsAtErrPos (p : ParserFn) : ParserFn :=
recoverAtErrPos p (ignoreFn <| takeUntilFn (fun c => c == ' ' || c == '\n'))
/-- Runs `p`, and on failure skips the spaces there and discards everything `p` pushed. -/
def recoverNonSpaceAtErrPos (p : ParserFn) : ParserFn :=
withCurrentStackSize fun iniSz =>
recoverAtErrPos p (ignoreFn (takeUntilFn (fun c => c != ' ')) >>
show ParserFn from fun _ s => s.shrinkStack iniSz)
def recoverEol (p : ParserFn) : ParserFn :=
recoverFn p fun _ => ignoreFn <| skipToNewline
@[inherit_doc recoverAtErrPos]
def recoverEolAtErrPos (p : ParserFn) : ParserFn := recoverAtErrPos p (ignoreFn skipToNewline)
@[inherit_doc recoverAtErrPos]
def recoverLineAtErrPos (p : ParserFn) : ParserFn := recoverAtErrPos p (ignoreFn skipRestOfLine)
/--
Closes a name with `closer`. If name parsing stopped at a non-name character, the error message is
informative. A name ends at `]` and may not span a line, so those expect `closer` instead.
-/
def closeRefNameWith (closer : String) : ParserFn := fun c s =>
let cutShort :=
if h : c.atEnd s.pos then false
else
let ch := c.get' s.pos h
!(ch == ']' || ch == '\n')
-- The characters listed are those `isRefNameChar` rejects, less the two that end a name.
if cutShort then s.mkError "a character other than '^', '[', '\\', or a tab"
else strFn closer c s
@[inherit_doc closeRefNameWith]
def closeRefName : ParserFn := closeRefNameWith "]"
/--
Recovery inside an argument list reads to the next whitespace, or to a character that closes the
list or the argument, so that the closer is still there for the parser that waits for it.
-/
def recoverArgAtErrPos (closes : Char → Bool) (p : ParserFn) : ParserFn :=
recoverAtErrPos p (ignoreFn <| takeUntilFn (fun c => c == ' ' || c == '\n' || closes c))
/--
Runs `p`. On failure, skips to the end of the line and replaces everything `p` pushed with `stxs`,
so that an enclosing node of fixed arity still receives children. The error is recorded where `p`
stopped.
-/
def recoverEolWithAtErrPos (stxs : Array Syntax) (p : ParserFn) : ParserFn := fun c s =>
let iniSz := s.stxStack.size
let s := p c s
if let some msg := s.errorMsg then
let errPos := s.pos
let s' := (ignoreFn skipToNewline) c {s with errorMsg := none}
if s'.hasError then s
else
let s' := stxs.foldl (init := s'.shrinkStack iniSz) (·.pushSyntax ·)
{ s' with recoveredErrors := s.recoveredErrors.push (errPos, s'.stxStack, msg) }
else s
def recoverSkip (p : ParserFn) : ParserFn :=
recoverFn p fun _ => skipFn
/-- Recovers from an error by pushing the provided syntax items, without adjusting the position. -/
def recoverHereWith (stxs : Array Syntax) (p : ParserFn) : ParserFn :=
recoverFn p fun rctx =>
show ParserFn from
fun _ s => stxs.foldl (init := s.restore rctx.initialSize rctx.initialPos) (·.pushSyntax ·)
/--
The whitespace after an argument, recorded on the argument's last token as its trailing whitespace:
the spaces that follow it.
-/
public def argEndWs : ParserFn := eatSpaces
/--
Parses an argument to a role, directive, command, or code block, which may be named or positional or
a flag. The argument's final token records the input `tail` consumes as its trailing whitespace.
Error recovery stops at characters that satisfy `closes`, which should delimit the current argument
list (e.g. via a role's `}`).
-/
public def argFn (tail : ParserFn := skipFn) (closes : Char → Bool := fun _ => false) :
ParserFn :=
withCurrentStackSize fun iniSz =>
flag <|> withParens iniSz <|> potentiallyNamed iniSz <|> (valFn tail >> mkAnon iniSz)
where
mkNamed (iniSz : Nat) : ParserFn := fun _ s => s.mkNode ``Arg.named iniSz
mkNamedNoParen (iniSz : Nat) : ParserFn := fun _ s => s.mkNode ``Arg.named_no_paren iniSz
mkAnon (iniSz : Nat) : ParserFn := fun _ s => s.mkNode ``Arg.anon iniSz
mkIdent (iniSz : Nat) : ParserFn := fun _ s => s.mkNode ``ArgVal.ident iniSz
flag : ParserFn :=
(nodeFn ``Arg.flag_on
(asTokenFn (strFn "+") >> recoverNonSpaceAtErrPos noSpace >>
recoverWsAtErrPos flagName) <|>
nodeFn ``Arg.flag_off
(asTokenFn (strFn "-") >> recoverNonSpaceAtErrPos noSpace >>
recoverWsAtErrPos flagName)) >>
withTrailing tail
flagName : ParserFn := expectedFn "flag name" (rawIdentFn (includeWhitespace := false))
noSpace : ParserFn := fun c s =>
if h : c.atEnd s.pos then s
else
let ch := c.get' s.pos h
if ch == ' ' then
s.mkError "no space before"
else s
potentiallyNamed iniSz :=
atomicFn' (rawIdentFn (includeWhitespace := false)) >> withTrailing eatSpaces >>
((atomicFn' (asTokenFn (strFn ":=") eatSpaces) >>
valFn (eatSpaces >> tail) >> mkNamedNoParen iniSz) <|>
(mkIdent iniSz >> mkAnon iniSz >> withTrailing tail))
-- A recoverable token step consumes and records its whitespace even when the token is lost.
-- `wsFallback` runs after the recovery too, and records the whitespace on the most recent
-- surviving token, past the recovery's stub.
recovering (closes : Char → Bool) (iniSz : Nat) (p : ParserFn) : ParserFn :=
recoverArgAtErrPos closes p >> wsFallback eatSpaces iniSz
withParens iniSz :=
-- Inside the parentheses, the `)` closes the argument as well.
let inParens := fun c => c == ')' || closes c
atomicFn' (asTokenFn (strFn "(") eatSpaces) >>
recovering inParens iniSz
(expectedFn "argument name" (rawIdentFn (includeWhitespace := false))) >>
recovering inParens iniSz (asTokenFn (strFn ":=")) >>
recovering inParens iniSz valFn >>
recoverEolAtErrPos (asTokenFn (strFn ")")) >> wsFallback (eatSpaces >> tail) iniSz >>
mkNamed iniSz
/--
Fails when the line the argument list is on has already ended.
-/
def guardSameLine : ParserFn := fun c s =>
if atNewLine c s then s.mkError "argument on the same line" else s
/--
Skips the spaces between a name and its arguments. The tokens themselves consume this whitespace as
they are parsed, so on ordinary parses this parser consumes nothing. Call sites wrap it in
`wsFallback`, so that it still records input consumed during error recovery.
-/
def nameArgWhitespace : ParserFn := fun c s =>
-- After a line end, the construct that resumes on the next line records its indentation, not
-- this argument list.
if atNewLine c s then s else eatSpaces c s
/--
Parses zero or more arguments to a role, directive, command, or code block. Each argument's final
token records the input `tail` consumes as its trailing whitespace. The argument list must be on
the line the parser starts on. A starting position at the beginning of a line means the list has
already ended, so the list is empty.
Error recovery stops at characters that satisfy `closes`, which should delimit the current argument
list (e.g. via a role's `}`).
-/
public def argsFn (tail : ParserFn := argEndWs) (closes : Char → Bool := fun _ => false) :
ParserFn := fun c s =>
let base := s.stxStack.size
sepByFn true (guardSameLine >> argFn tail closes) (guardSameLine >> wsFallback eatSpaces base) c s
/--
Reads the name of a footnote or link reference, which is a run of the characters that a name may
contain. `description` names what was expected where the name begins.
-/
def refNameFn (description : String) : ParserFn :=
expectedFn description (many1Fn (satisfyFn isRefNameChar description))
/--
Parses a name and zero or more arguments to a role, directive, command, or code block. The final
token of the name and of each argument records the input that `tail` consumes as its trailing
whitespace. The name and arguments must be on the line the parser starts on. A starting position
at the beginning of a line fails to parse a name.
The caller's own tokens record the whitespace before the name. If a caller lets this parser consume
that whitespace instead, no token records it.
Error recovery stops at characters that satisfy `closes`, which should delimit the current argument
list (e.g. via a role's `}`).
-/
public def nameAndArgsFn (tail : ParserFn := argEndWs) (closes : Char → Bool := fun _ => false) :
ParserFn := fun c s =>
let base := s.stxStack.size
(nameArgWhitespace >> guardSameLine >>
expectedFn "identifier" (rawIdentFn (includeWhitespace := false)) >>
withTrailing tail >> wsFallback nameArgWhitespace base >>
argsFn (tail := tail) (closes := closes)) c s
/--
The context within which a newline element is parsed.
-/
public structure InlineCtxt where
/-- Are newlines allowed here? -/
allowNewlines := true
/--
How many asterisks introduced the current level of boldness? `none` means no bold here.
-/
boldDepth : Option Nat := none
/--
How many underscores introduced the current level of emphasis? `none` means no emphasis here.
-/
emphDepth : Option Nat := none
/-- Are we in a link? -/
inLink : Bool := false
/--
The trailing whitespace parser for the final token of a top-level inline element.
-/
tail : ParserFn := skipFn
/--
The docstring's base column, which allows inline code and math to be parsed relative
to the docstring's indentation.
-/
baseColumn : Nat := 0
deriving Inhabited
/- Parsing inlines:
* An inline's tokens record the whitespace between them, and the enclosing block's `tail` parser
decides what the last token of a top-level inline records after it.
-/
/--
A linebreak that isn't a block break (that is, there's non-space content on the next line)
-/
def linebreakFn (ctxt : InlineCtxt) : ParserFn :=
if ctxt.allowNewlines then
nodeFn ``Inline.linebreak <| asTokenFn (trailing := eatSpaces) fun c s =>
if lineContinues c s then skipChFn '\n' c s
else s.mkError "newline"
else
errorFn "Newlines not allowed here"
partial def notInLink (ctxt : InlineCtxt) : ParserFn := fun _ s =>
if ctxt.inLink then s.mkUnexpectedError "a link inside a link" else s
/-- The context for inline elements nested inside another inline element. -/
def InlineCtxt.inner (ctxt : InlineCtxt) : InlineCtxt := { ctxt with tail := skipFn }
-- Like `satisfyFn (· == '\n')` but with a better error message that mentions what was expected.
def newlineOrUnexpected (msg : String) : ParserFn := fun c s =>
let i := s.pos
if h : c.atEnd i then s.mkEOIError
else if c.get' i h == '\n' then s.next' c i h
else s.mkUnexpectedError s!"unexpected {describeChar (c.get' i h)}" [msg]
mutual
partial def emphLike
(name delimKind : SyntaxNodeKind) (char : Char) (what plural noun : String)
(getter : InlineCtxt → Option Nat) (setter : InlineCtxt → Option Nat → InlineCtxt)
(ctxt : InlineCtxt) : ParserFn :=
nodeFn name <|
withStartPos fun openPos =>
atomicFn' (nodeFn delimKind <| asTokenFn (asTokenFn (opener ctxt) >> notFollowedByFn (chFn ' ' false <|> chFn '\n' false) "space or newline after opener")) >>
(recoverSkip <| fun c s =>
let openStop := s.pos
let count := openPos.byteDistance openStop
let delimiter := c.extract openPos openStop
(manyFn (inlineFn ((setter ctxt (some count)).inner)) >>
unterminatedAtEnd openPos openStop noun >>
nodeFn delimKind (asTokenFn (atomicFn' (noSpaceBefore >>
repFn count (expectFn (· == char) s!"'{delimiter}' to close {noun}"))) ctxt.tail)) c s)
where
opener (ctxt : InlineCtxt) : ParserFn :=
match getter ctxt with
| none => many1Fn (expectFn (· == char) s!"{plural} to open {noun}")
| some 1 | some 0 => fun _ s => s.mkUnexpectedError s!"no room left to {what} here"
-- A nested opener is a run of at least one delimiter that is shorter than the delimiter it is
-- nested in, so that the close of the outer element remains unambiguous.
| some d =>
expectFn (· == char) s!"'{char}' to open nested {noun}" >>
atMostFn (d - 2) (satisfyFn (· == char) s!"{char}") s!"at most {d - 1} {plural}"
noSpaceBefore : ParserFn := fun c s =>
if s.pos == 0 then s
else
let prior := c.get (c.prev s.pos)
if prior.isWhitespace then
s.mkUnexpectedError s!"unexpected space before the closing '{char}'"
else s
/--
Parses emphasis: a matched pair of one or more `_`.
-/
public partial def emphFn :=
emphLike ``Inline.emph ``emphDelimiter '_' "emphasize" "underscores" "emphasis" (·.emphDepth) ({· with emphDepth := ·})
/--
Parses bold: a matched pair of one or more `*`.
-/
public partial def boldFn :=
emphLike ``Inline.bold ``boldDelimiter '*' "bold" "asterisks" "bold text" (·.boldDepth) ({· with boldDepth := ·})
/--
Parses inline code. The content is one `versoCodeLine` token per source line. Each line's token
records the docstring's indentation on the line after it as trailing whitespace, while indentation
past that column is part of the code. A space written at each end of the content to escape a
backtick or a space becomes whitespace on the token before it.
-/
public partial def codeFn (ctxt : InlineCtxt := {}) : ParserFn :=
nodeFn ``Inline.code <|
withStartPos fun openPos =>
atomicFn' opener >>
( atomicFn' <|
withStartPos fun openStop => contentAndCloser openPos openStop)
where
opener : ParserFn :=
nodeFn ``codeDelimiter <|
asTokenFn (many1Fn (expectFn (· == '`') "backticks to open inline code"))
/-- Exactly `count` backticks, which close inline code opened with `count` backticks. -/
closingBackticks (count : Nat) : ParserFn :=
atomicFn' (repFn count <| satisfyFn (· == '`') "backtick") >>
notFollowedByFn (satisfyFn (· == '`') "`") "backtick"
/--
Runs `p`, which parses the content of inline code or the backticks that close it. When `p`
fails, the code has no closer, and the error is `unclosedCodeError`.
-/
orUnclosedAt (openPos openStop : String.Pos.Raw) (count : Nat) (p : ParserFn) : ParserFn :=
fun c s =>
let s := p c s
if s.hasError then { s with errorMsg := some (unclosedCodeError c openPos openStop count) }
else s
/--
Parses the content and closer of inline code whose opener extends from `openPos` to `openStop`.
When they fail to parse, the content is the rest of the opener's line, and parsing resumes at
the newline. The characters after it are then read as they would be after any other inline: a
further line continues the paragraph, and a blank line ends it.
If the line contains a sequence of backticks that is too long, then the error is reported there;
otherwise, it is reported as unterminated inline code at the opener.
-/
contentAndCloser (openPos openStop : String.Pos.Raw) : ParserFn :=
-- Backticks are one byte each, so this is OK
let count := openPos.byteDistance openStop
let orUnclosed := orUnclosedAt openPos openStop count
recoverFn (recover := restOfLine) <|
orUnclosed (nodeFn versoCodeKind (many1Fn (codeLine (count - 1)))) >>
codeBoundarySpacesToWs >>
nodeFn ``codeDelimiter (asTokenFn (orUnclosed (closingBackticks count)) ctxt.tail)
/-- The rest of the line as the content of inline code that has no closer. -/
restOfLine (rctx : RecoveryContext) : ParserFn :=
(show ParserFn from fun _ s => s.restore rctx.initialSize rctx.initialPos) >>
nodeFn versoCodeKind (nodeFn nullKind (nodeFn versoCodeLineKind
(asTokenFn (takeWhileFn (· ≠ '\n'))))) >>
codeBoundarySpacesToWs >> pushMissing
codeLine (maxCount : Nat) : ParserFn :=
atomicFn' <|
nodeFn versoCodeLineKind <|
asTokenFn (trailing := codeLineTailWs ctxt.baseColumn) fun c s =>
let startPos := s.pos
let s := manyFn (codeContentsFn maxCount) c s
if s.hasError then s
else
let i := s.pos
if h : c.atEnd i then
if i == startPos then codeContentsFn maxCount c s else s
else if c.get' i h == '\n' then s.next' c i h
else if i == startPos then codeContentsFn maxCount c s
else s
codeContentsFn (maxCount : Nat) : ParserFn :=
atomicFn' (asTokenFn (satisfyFn (maxCount > 0 && · == '`') >> atMostFn (maxCount - 1) (chFn '`') s!"at most {maxCount} backticks")) <|>
expectFn (fun ch => ch != '`' && ch != '\n') "a character other than a backtick"
/--
Parses mathematics. The content is inline code, with its lines and indentation read the same way.
-/
public partial def mathFn (ctxt : InlineCtxt := {}) : ParserFn :=
atomicFn' (nodeFn ``Inline.display_math <|
nodeFn ``displayMathMarker (strFn "$$") >> codeFn ctxt) <|>
atomicFn' (nodeFn ``Inline.inline_math <|
nodeFn ``inlineMathMarker (strFn "$") >> codeFn ctxt)
/-- Reads a prefix of a line of text, stopping at a text-mode special character. -/
public partial def textFn (ctxt : InlineCtxt := {}) : ParserFn :=
nodeFn ``Inline.text <|
nodeFn versoTextKind <|
asTokenFn (many1Fn inlineTextCharFn) ctxt.tail
/-- Parses a link. -/
public partial def linkFn (ctxt : InlineCtxt) :=
nodeFn ``Inline.link <|
(atomicFn' (notInLink ctxt >> strFn "[" >> notFollowedByFn (chFn '^') "'^'" )) >>
(recoverEol <|
many1Fn (inlineFn {ctxt.inner with inLink := true}) >>
strFn "]" >> linkTargetFn ctxt.tail)
/-- Parses a footnote. -/
public partial def footnoteFn (ctxt : InlineCtxt) :=
nodeFn ``Inline.footnote <|
(atomicFn' (notInLink ctxt >> strFn "[^" )) >>
(recoverLineAtErrPos <|
nodeFn versoRefKind (asTokenFn (refNameFn "a footnote name")) >>
closeRefName >> withTrailing ctxt.tail)
partial def linkTargetFn (tail : ParserFn := skipFn) : ParserFn := fun c s =>
let s := (ref <|> url) c s
if s.hasError then
match s.errorMsg with
| some e => s.setError { e with
expected := ["link target '(url)' or '[ref]' (use '\\[' for a literal '[')"] }
| none => s
else s
where
notUrlEnd := expectedFn "URL" (satisfyEscFn (· ∉ ")\n".toList)) >> takeUntilEscFn (· ∈ ")\n".toList)
notRefEnd := refNameFn "a reference name"
ref : ParserFn :=
nodeFn ``LinkTarget.ref <|
(atomicFn' <| strFn "[") >>
recoverEolAtErrPos (nodeFn versoRefKind (asTokenFn notRefEnd) >> closeRefName >>
withTrailing tail)
url : ParserFn :=
nodeFn ``LinkTarget.url <|
(atomicFn' <| strFn "(") >>
recoverEolAtErrPos
(nodeFn versoLinkUrlKind (asTokenFn notUrlEnd) >> strFn ")" >> withTrailing tail)
/-- Parses an image. -/
public partial def imageFn (tail : ParserFn := skipFn) : ParserFn :=
nodeFn ``Inline.image <|
atomicFn' (strFn "![") >>
(recoverSkip <|
nodeFn versoImageAltKind (asTokenFn (takeUntilEscFn (· ∈ "]\n".toList))) >>
strFn "]" >>
linkTargetFn tail)
/--
Parses a role.
-/
public partial def roleFn (ctxt : InlineCtxt) : ParserFn :=
nodeFn ``Inline.role <| withCurrentStackSize fun base =>
atomicFn' (chFn '{') >> nameAndArgsThenContent base
where
-- An error in the name and arguments is recovered by reading to the closing brace on the same
-- line, after which the content is parsed as usual. If the line has no closing brace, the role
-- ends at the end of the line and has no content.
nameAndArgsThenContent (base : Nat) : ParserFn := fun c s =>
let s := recoverAtErrPos (nameAndArgs base) closingBraceOnLine c s
if s.hasError then recoverPendingError (ignoreFn skipToNewline >> noContent) c s
else (bracketed <|> atomicFn' nonBracketed) c s
-- The brace is the role's closing delimiter, so recovery pushes it.
closingBraceOnLine : ParserFn :=
atomicFn' (ignoreFn (takeUntilFn (fun c => c == '}' || c == '\n')) >> chFn '}')
nameAndArgs (base : Nat) :=
withTrailing eatSpaces >>
nameAndArgsFn (tail := argEndWs) (closes := (· == '}')) >>
wsFallback eatSpaces base >>
rawFn (fun c s =>
let i := s.pos
if h : c.atEnd i then s.mkEOIError [closeMsg]
else if c.get' i h == '}' then s.next' c i h
else (s.setPos i).mkUnexpectedError s!"unexpected {describeChar (c.get' i h)}" [closeMsg])
false
closeMsg := "positional argument, named argument, flag, or '}' (use '\\{' for a literal '{')"
bracketed :=
atomicFn' (nodeFn nullKind (expectChFn '[')) >>
recoverEolAtErrPos (manyFn (inlineFn ctxt.inner) >>
nodeFn nullKind (expectChFn ']' >> withTrailing ctxt.tail))
nonBracketed : ParserFn := fun c s =>
let s := s.pushSyntax (mkNullNode #[])
let s := nodeFn nullKind (delimitedInlineFn ctxt) c s
s.pushSyntax (mkNullNode #[])
noContent : ParserFn := fun _ s =>
s.pushSyntax (mkNullNode #[]) |>.pushSyntax (mkNullNode #[]) |>.pushSyntax (mkNullNode #[])
/--
Parses an inline that is self-delimiting (that is, with well-defined start and stop characters).
-/
public partial def delimitedInlineFn (ctxt : InlineCtxt) : ParserFn :=
expectedFn "'_'" (emphFn ctxt) <|> expectedFn "'*'" (boldFn ctxt) <|>
expectedFn "'`'" (codeFn ctxt) <|> mathFn ctxt <|>
expectedFn "'{'" (roleFn ctxt) <|> imageFn ctxt.tail <|>
linkFn ctxt <|> footnoteFn ctxt
/--
Parses any inline element.
-/
public partial def inlineFn (ctxt : InlineCtxt) : ParserFn :=
textFn ctxt <|> expectedFn "newline" (linebreakFn ctxt) <|> delimitedInlineFn ctxt
end
/--
Parses a line of text (that is, one or more inline elements). When `recordTrailing` is set, each
top-level inline's final token consumes the whitespace that follows it as its trailing whitespace.
-/
public def textLineFn (ctxt : InlineCtxt := {}) (recordTrailing := false) : ParserFn :=
if !recordTrailing then
many1Fn (inlineFn ctxt)
else fun c s => Id.run do
let ctxt := { ctxt with tail := blockTailWs ctxt.allowNewlines }
let iniSz := s.stxStack.size
let mut s := s
let mut first := true
repeat
let itSz := s.stxStack.size
let itPos := s.pos
let itRec := s.recoveredErrors.size
s := inlineFn ctxt c s
if s.hasError then
if s.pos == itPos && !first then
s := s.restore itSz itPos
break
if s.pos == itPos then
s := s.mkUnexpectedError "invalid 'many' parser combinator application, parser did not consume anything"
break
first := false
-- After an inline that parsed without error recovery, its final token's trailing parser made
-- the line-end decision. Stopping at a newline means the next line continues. A token that
-- consumed its line's end as trailing whitespace ends the text. After error recovery, the
-- text ends if the inline's final token consumed its line's end; otherwise, the next
-- round parses a linebreak by lookahead as usual.
if s.recoveredErrors.size == itRec then
if h : c.atEnd s.pos then
break
else if c.get' s.pos h == '\n' then
if ctxt.allowNewlines then
s := nodeFn ``Inline.linebreak (asTokenFn (skipChFn '\n') eatSpaces) c s
else
break
else if consumedLineEnd s then
break
else if consumedLineEnd s then
break
return s.mkNode nullKind iniSz
open Lean.Parser.Term in
/-- The fields of a metadata block, which are the fields of a structure instance. -/
@[run_builtin_parser_attribute_hooks]
public def metadataContents : Parser :=
structInstFields (sepByIndent structInstField ", " (allowTrailingSep := true))
def withPercents : ParserFn → ParserFn := fun p =>
adaptUncacheableContextFn (fun c => {c with tokens := c.tokens.insert "%%%" "%%%"}) p
/--
The style of a list, shared by all of its items.
-/
public inductive ListStyle where
/-- A list whose items are numbered. -/
| ordered (type : OrderedListType)
/-- A list whose items are bulleted. -/
| unordered (type : UnorderedListType)
/--
The context within which a block should be valid.
-/
public structure BlockCtxt where
/--
Whether blocks in this context are at the document's top level, rather than the contents of
another block.
-/
topLevel : Bool := true
/--
Whether each block's final token records the whitespace that follows the block as its trailing
whitespace.
-/
recordTrailing : Bool := false
/--
The block's maximal directive size (that is, the greatest number of allowed colons).
-/
maxDirective : Option Nat := none
/--
The position at which the document content starts, used to allow headers on the first line of a
docstring (e.g. `/-! # Header -/`). With the default value `⟨1, 0⟩`, the beginning-of-line check
is unaffected for normal documents.
-/
docStartPosition : Position := ⟨1, 0⟩
/--
The base column of the docstring, which is the least indentation of any non-empty line in it,
including the opening and closing delimiters. Items that may not nest in blocks, such as headers,
start at this column, and a code element's lines treat the indentation up to it as whitespace.
-/
baseColumn : Nat := 0
deriving Inhabited, Repr
/--
The trailing whitespace of a block's final token. When `recordTrailing` is set, the token takes the
rest of its line and any blank lines that follow. Otherwise it takes nothing.
-/
def blockTrailingWs (ctxt : BlockCtxt) : ParserFn :=
if ctxt.recordTrailing then lineTailWsFn else skipFn
/--
The separator between blocks in a sequence.
-/
def blockSepFallback (base : Nat) : ParserFn := fun c s =>
if s.recoveredErrors.isEmpty then s
else wsFallback lineTailWsFn base c s
/--
Finds the minimum column of the first non-whitespace character on each non-empty line between
`startPos` and `endPos`, excluding the line that contains `startPos`.
The result is `none` if no such line exists.
-/
public def minContentIndent? (text : FileMap) (startPos endPos : String.Pos.Raw) :
Option Nat := Id.run do
let mut result := none
let mut thisLineCol := 0
if h : endPos ≤ text.source.rawEndPos then
let endPos := text.source.posGE endPos h
if h : startPos ≤ text.source.rawEndPos then
let mut i := text.source.posGE startPos h
let mut afterNewline := false
while h : i ≠ text.source.endPos do
let c := i.get h
i := i.next h
if i > endPos then break
if c == '\n' then
afterNewline := true
thisLineCol := 0
else if afterNewline && c != ' ' then
result := some <| if let some col := result then min col thisLineCol else thisLineCol
afterNewline := false
else thisLineCol:= thisLineCol + 1
return result
/--
Computes the base column of a docstring whose opening delimiter is at `openPos` and whose content
begins at `startPos`. `endPos` is the position of the `-` before the `/` in the closing delimiter.
The base column is the minimum column among the opening delimiter, the closing delimiter, and the
least-indented non-empty content line.
-/
public def docStringBaseColumn (text : FileMap) (openPos : String.Pos.Raw)
(startPos : String.Pos.Raw) (endPos : String.Pos.Raw) : Nat :=
let openCol := (text.toPosition openPos).column
let closeCol := (text.toPosition endPos).column
let delimCol := min openCol closeCol
if let some contentCol := minContentIndent? text startPos endPos then
min delimCol contentCol
else
delimCol
/--
Computes the `BlockCtxt` for parsing a docstring whose opening delimiter is at `openPos` and whose
content begins at `startPos`, on the line where that content starts and separated from it by spaces
alone. `endPos` is the position of the `-` before the `/` in the closing delimiter.
When the docstring content starts mid-line (e.g. `/-! # Header -/`), the `docStartPosition` is set
to the position after any leading spaces so that headers on the first line are recognized. For
indented docstrings, `baseColumn` is `docStringBaseColumn`.
-/
public def BlockCtxt.forDocString (text : FileMap) (openPos : String.Pos.Raw)
(startPos : String.Pos.Raw) (endPos : String.Pos.Raw) : BlockCtxt :=
let baseColumn := docStringBaseColumn text openPos startPos endPos
let position := text.toPosition startPos
if position.column ≤ baseColumn then { baseColumn }
else
-- Skip leading spaces to find where content actually starts
let pos := Id.run do
if h : startPos ≤ text.source.rawEndPos then
let mut pos := text.source.posGE startPos h
while h : pos ≠ text.source.endPos do
if pos.get h == ' ' then
pos := pos.next h
else
break
return pos.offset
else text.source.rawEndPos
{ docStartPosition := text.toPosition pos, baseColumn }
/--
Whether `pos` is on the docstring's first line, at or before where its content starts. The opening
delimiter precedes the content there, so it counts as the beginning of a line.
-/
def atDocStart (ctxt : BlockCtxt) (c : ParserContext) (pos : String.Pos.Raw) : Bool :=
let position := c.fileMap.toPosition pos
position.line == ctxt.docStartPosition.line && position.column ≤ ctxt.docStartPosition.column
/--
Whether a block may open at `pos`: only spaces precede it on its line, or it is on the docstring's
first line.
-/
def atBolAt (ctxt : BlockCtxt) (c : ParserContext) (pos : String.Pos.Raw) : Bool :=
let lineStart := c.fileMap.lineStart (c.fileMap.toPosition pos).line
(Substring.Raw.mk c.inputString lineStart pos).all (· == ' ') || atDocStart ctxt c pos
@[inherit_doc atBolAt]
def atBol (ctxt : BlockCtxt) (c : ParserContext) (s : ParserState) : Bool :=
atBolAt ctxt c s.pos
/--
Whether a block that belongs to the document rather than to the block it is written in may open at
`pos`: at the document's base column, or on the docstring's first line.
-/
def atBaseColumn (ctxt : BlockCtxt) (c : ParserContext) (pos : String.Pos.Raw) : Bool :=
(c.fileMap.toPosition pos).column ≤ ctxt.baseColumn || atDocStart ctxt c pos
def bol (ctxt : BlockCtxt) : ParserFn := fun c s =>
if atBol ctxt c s then s
else s.mkErrorAt s!"beginning of line at {c.fileMap.toPosition s.pos}" s.pos
def bolThen (ctxt : BlockCtxt) (p : ParserFn) (description : String) : ParserFn := fun c s =>
if atBol ctxt c s then
let s := p c s
if s.hasError then
s.mkErrorAt description s.pos
else s
else s.mkErrorAt description s.pos
open Lean.Parser.Term in
/--
Parses a metadata block, which contains the contents of a Lean structure initialization but is
surrounded by `%%%` on each side.
-/
public def metadataBlockFn (ctxt : BlockCtxt := {}) : ParserFn :=
nodeFn ``Block.metadata_block <|
atLineStart >>
opener >>
withPercents metadataContents.fn >>
closer
where
-- In a position where the block can't open, reading the opening delimiter commits the parse to a
-- message that names the construct. The unexpected token that's used includes the source range
-- for the message.
atLineStart : ParserFn := fun c s =>
if atBol ctxt c s then s
else
let s' := atomicFn' (eatSpaces >> strFn "%%%") c s
if s'.hasError then s'
else s'.setError { unexpectedTk := s'.stxStack.back, unexpected := misplacedMsg }
misplacedMsg := "unexpected metadata block opener '%%%' (must be at start of line)"
opener :=
atomicFn' (bolThen ctxt (eatSpaces >> strFn "%%%") "%%% (at line beginning)") >>
-- The opener records the indentation before the contents, so the Lean parser that reads them
-- starts at their column and aligns the fields there. That column must be the base column.
withTrailing (eatSpaces >> ignoreFn (chFn '\n') >> eatSpaces) >> atBaseColumn
atBaseColumn : ParserFn := fun c s =>
if c.currentColumn s == ctxt.baseColumn then s
else s.mkErrorAt s!"metadata contents at column {ctxt.baseColumn}" s.pos
closer :=
bolThen ctxt (strFn "%%%") "%%% (at line beginning)" >>
withTrailing (eatSpaces >> ignoreFn (chFn '\n' <|> eoiFn) >>
blockTrailingWs ctxt)
/--
Succeeds when the parser is looking at an ordered list marker.
-/
public def lookaheadOrderedListMarker (p : OrderedListType → Int → ParserFn) :
ParserFn := fun c s =>
let iniPos := s.pos
let iniSz := s.stxStack.size
let s := (onlyBlockOpeners >> checkIndentGe "list marker") c s
if s.hasError then s.setPos iniPos |>.shrinkStack iniSz
else
let numPos := s.pos
let s := ignoreFn (takeWhile1Fn (·.isDigit) "digits") c s
if s.hasError then {s with pos := iniPos}.shrinkStack iniSz else
let digits := c.extract numPos s.pos
match digits.toNat? with
| none => {s.mkError s!"digits, got '{digits}'" with pos := iniPos}
| some n =>
let i := s.pos
if h : c.atEnd i then {s.mkEOIError with pos := iniPos}
else
let (s, next, type) := match c.get' i h with
| '.' => (s.next' c i h, (chFn ' ' <|> chFn '\n'), OrderedListType.numDot)
| ')' => (s.next' c i h, (chFn ' ' <|> chFn '\n'), OrderedListType.parenAfter)
| other =>
(s.setError { unexpected := s!"unexpected '{other}'", expected := ["'.'", "')'"] },
skipFn,
.numDot)
if s.hasError then {s with pos := iniPos}
else
let s := next c s
if s.hasError then {s with pos := iniPos}
else
p type n c (s.shrinkStack iniSz |>.setPos numPos)
/--
Succeeds when the parser is looking at an unordered list marker.
-/
public def lookaheadUnorderedListMarker (p : UnorderedListType → ParserFn) :
ParserFn := fun c s =>
let iniPos := s.pos
let iniSz := s.stxStack.size
let s := (onlyBlockOpeners >> checkIndentGe "list marker") c s
let markerPos := s.pos
if s.hasError then s.setPos iniPos |>.shrinkStack iniSz
else if h : c.atEnd s.pos then s.mkEOIError.setPos iniPos |>.shrinkStack iniSz
else let (s, type) : (_ × UnorderedListType) := match c.get' s.pos h with
| '*' => (s.next' c s.pos h, .asterisk)
| '-' => (s.next' c s.pos h, .dash)
| '+' => (s.next' c s.pos h, .plus)
| other => (s.setError {expected := ["*", "-", "+"], unexpected := s!"'{other}'"}, .plus)
if s.hasError then s.setPos iniPos
else
let s := (chFn ' ' <|> chFn '\n') c s
if s.hasError then s.setPos iniPos
else p type c (s.shrinkStack iniSz |>.setPos markerPos)
def skipUntilDedent (indent : Nat) : ParserFn :=
skipRestOfLine >>
manyFn (chFn ' ' >> takeWhileFn (· == ' ') >> checkIndent indent >> skipRestOfLine)
@[inherit_doc recoverAtErrPos]
def recoverUnindent (indent : Nat) (p : ParserFn) (finish : ParserFn := skipFn) :
ParserFn :=
recoverAtErrPos p (ignoreFn (skipUntilDedent indent) >> finish)
/--
Sets the leading whitespace of the first token in `stx` to run from `startPos`. Syntax without
tokens stays unchanged.
Every token that a parse produces has empty leading whitespace, because the token before it records
the whitespace between them. The first token of an input records the whitespace that the input
starts with, and this applies it.
-/
public def setStartLeading (startPos : String.Pos.Raw) (stx : Syntax) : Syntax :=
/-
A parse puts source info on atoms and identifiers only, never on nodes, so the head info this
reads and the head info it writes belong to the same leaf.
-/
match stx.getHeadInfo? with
| some (.original leading pos trailing endPos) =>
stx.setHeadInfo (.original { leading with startPos } pos trailing endPos)
| _ => stx
mutual
/--
Parses an item of a list in the given style. The saved position is the first item marker's, which
every marker lines up with.
-/
public partial def listItemFn (ctxt : BlockCtxt) (style : ListStyle) : ParserFn :=
nodeFn ``ListItem.item <|
-- The marker takes the padding between it and the item's contents, plus any blank lines that
-- follow, as with a keyword's trailing space. An item with no contents is empty, and its
-- marker has recorded the end of its line, so the block after the list can start.
afterMarker marker lineTailWsFn (blocksFn ctxt)
where
marker : ParserFn :=
checkIndentEq "list item marker" >>
(match style with
| .ordered type => orderedListMarker type
| .unordered type => unorderedListMarker type) >>
ignoreFn (lookaheadFn (chFn ' ' <|> chFn '\n'))
/--
Parses an item from a description list. The saved position is the first item's colon, which every
item's colon lines up with.
-/
public partial def descItemFn (ctxt : BlockCtxt) : ParserFn :=
nodeFn ``DescItem.item <| withCurrentStackSize fun base =>
-- The colon takes the space between it and the term, as after a keyword.
afterMarker colon eatSpaces (
textLineFn { baseColumn := ctxt.baseColumn } (recordTrailing := true) >>
wsFallback lineTailWsFn base >>
recoverSkip (checkIndentGe "description body" >> blocks1Fn ctxt) >>
wsFallback lineTailWsFn base)
where
colon : ParserFn :=
checkIndentEq "description list item" >>
asTokenFn (chFn ':' false) >> ignoreFn (lookaheadFn (chFn ' '))
/--
Parses a block quote.
-/
public partial def blockquoteFn (ctxt : BlockCtxt) : ParserFn :=
nodeFn ``Block.blockquote <| withCurrentStackSize fun base =>
afterMarker (chFn '>') eatSpaces (blocksFn { ctxt with topLevel := false }) >>
-- A blockquote with no contents has no token of its own to record the end of its line, so
-- the marker records it and the block after the blockquote can start.
wsFallback (unlessLineEndConsumed (eatSpaces >> optNlWs (blockTrailingWs ctxt))) base
/-- Parses an unordered list. -/
public partial def unorderedListFn (ctxt : BlockCtxt) : ParserFn :=
nodeFn ``Block.ul <|
lookaheadUnorderedListMarker fun type =>
withPositionHere (many1Fn (listItemFn { ctxt with topLevel := false } (.unordered type)))
/-- Parses an ordered list. -/
public partial def orderedListFn (ctxt : BlockCtxt) : ParserFn :=
nodeFn ``Block.ol <|
lookaheadOrderedListMarker fun type _start => -- TODO? Validate list numbering?
withPositionHere (many1Fn (listItemFn { ctxt with topLevel := false } (.ordered type)))
/-- Parses a definition list. -/
public partial def definitionListFn (ctxt : BlockCtxt) : ParserFn :=
nodeFn ``Block.dl <|
atomicFn' (onlyBlockOpeners >> ignoreFn (lookaheadFn (chFn ':' >> chFn ' '))) >>
withPositionHere (many1Fn (descItemFn { ctxt with topLevel := false }))
/--
Parses a paragraph (that is, a sequence of otherwise-undecorated inlines). A paragraph contains
something other than whitespace, so a run of empty lines is not a paragraph.
-/
public partial def paraFn (ctxt : BlockCtxt) : ParserFn := fun c s =>
let base := s.stxStack.size
let startPos := s.pos
-- Non-paragraph blocks take precedence wherever one may open.
let notBlockOpener :=
if atBlockStart c s then notFollowedByFn blockOpenerFn "block opener" else skipFn
(nodeFn ``Block.para <|
notBlockOpener >>
textLineFn { baseColumn := ctxt.baseColumn } (recordTrailing := ctxt.recordTrailing) >>
guardContent base startPos) c s
where
guardContent (base : Nat) (startPos : String.Pos.Raw) : ParserFn := fun _ s => Id.run do
let mut i := base
while i < s.stxStack.size do
-- A text line wraps its inlines in one node, so its children are the elements to
-- inspect.
let element := s.stxStack.get! i
let inlines := if element.getKind == nullKind then element.getArgs else #[element]
for inline in inlines do
unless blankInline inline do return s
i := i + 1
return (s.restore base startPos).mkError "paragraph content"
blankInline (stx : Syntax) : Bool :=
match stx.getSubstring? (withLeading := false) (withTrailing := false) with
| some text => text.all Char.isWhitespace
| none => false
/-- Parses a header. -/
public partial def headerFn (ctxt : BlockCtxt) : ParserFn :=
nodeFn ``Block.header <|
-- A header can't nest in a block, so it must be at the base column.
inUnindentedBlock >>
withStartPos fun markerPos =>
-- A '#' at the start of a line opens a header.
bol ctxt >>
nodeFn ``headerMarker
(asTokenFn (many1Fn (skipChFn '#')) (skipChFn ' ' >> takeWhileFn (· == ' '))) >>
-- Reading the marker commits the parse, so that a '#' indented past the base column is
-- an error.
checkNonIndented markerPos >>
lookaheadFn (expectFn (· != '\n') "header text") >>
textLineFn { allowNewlines := false, baseColumn := ctxt.baseColumn }
(recordTrailing := ctxt.recordTrailing)
where
indentMsg := s!"'#' (header) to start at column {ctxt.baseColumn}"
inUnindentedBlock : ParserFn := fun c s =>
match c.savedPos? with
| none => s
| some pos =>
if (c.fileMap.toPosition pos).column ≤ ctxt.baseColumn then s
else s.mkErrorAt indentMsg s.pos
checkNonIndented (markerPos : String.Pos.Raw) : ParserFn := fun c s =>
if atBaseColumn ctxt c markerPos then s
else
let marker := s.stxStack.back
s.popSyntax.setError { unexpectedTk := marker.getArg 0, expected := [indentMsg] }
/--
Parses a code block. Each line's token records the fence's indentation on the line after it as
trailing whitespace, so the code the block denotes is what the lines' tokens contain.
-/
public partial def codeBlockFn (ctxt : BlockCtxt) : ParserFn :=
nodeFn ``Block.codeblock <| withCurrentStackSize fun base =>
-- Opener - leaves the open token on the stack. The fence consumes the spaces that follow it
-- and, when nothing else is on its line, the line's newline and the next line's indentation.
withCurrentColumn fun col =>
-- The opening fence governs the lines up to the closing fence.
withPositionHere <|
atomicFn' (nodeFn ``codeBlockFence
(asTokenFn (atLeastFn 3 (skipChFn '`'))
(takeWhileFn (· == ' ') >> optNlWs (eatIndent col)))) >>
withFence fun openPos fence =>
let fenceWidth := fence.length
recoverUnindent col <|
optionalFn (nameAndArgsFn (tail := argEndWs >> optNlWs (eatIndent col))) >>
unlessLineEndConsumed (wsFallback (ignoreFn (recoverEolAtErrPos
(newlineOrUnexpected "positional argument, named argument, flag, or newline")) >>
eatIndent col) base) >>
nodeFn versoCodeBlockKind (manyFn (blankCodeLine col <|> codeFrom col fenceWidth)) >>
unterminatedDelimiterAtEnd openPos fence "code block" >>
closeFence openPos fence
where
/-- Runs `p` with the position and the text of the fence on top of the syntax stack. -/
withFence (p : String.Pos.Raw → String → ParserFn) : ParserFn := fun c s =>
match s.stxStack.back with
| .node _ _ #[.atom (.original (pos := openPos) ..) fence] => p openPos fence c s
| _ => s.mkError "internal error: expected a code block fence"
blankCodeLine (col : Nat) : ParserFn :=
atomicFn' <|
nodeFn versoCodeLineKind (asTokenFn (takeWhileFn (· == ' ') >> nl) (eatIndent col))
codeFrom (col width : Nat) :=
atomicFn' (bol ctxt >>
lookaheadFn (ignoreFn (takeWhileFn (· == ' ') >> checkIndentGe "code block contents" >>
notFollowedByFn (atLeastFn width (skipChFn '`')) "ending fence"))) >>
nodeFn versoCodeLineKind
(asTokenFn (manyFn (satisfyFn (· != '\n') "non-newline") >> satisfyFn (· == '\n') "newline")
(eatIndent col))
closeFence (openPos : String.Pos.Raw) (fence : String) : ParserFn := fun c s =>
let line := (c.fileMap.toPosition openPos).line
(bol ctxt >>
-- An over-indented fence is past the saved column, so the check reports it. A fence at the
-- saved column has no spaces before it left to skip.
takeWhileFn (· == ' ') >>
checkIndentEq s!"closing '{fence}' for the code block opened on line {line}" >>
atomicFn' (nodeFn ``codeBlockFence (asTokenFn (repFn fence.length (skipChFn '`')))) >>
notFollowedByFn (skipChFn '`') "extra `" >>
withTrailing (takeWhileFn (· == ' ') >> ignoreFn lineEnd >>
blockTrailingWs ctxt)) c s
/-- Parses a directive. -/
public partial def directiveFn (ctxt : BlockCtxt) : ParserFn :=
nodeFn ``Block.directive <| withCurrentStackSize fun base =>
-- Opener - leaves the open token, the name and the arguments on the stack
atomicFn'
(nodeFn ``directiveDelimiter (asTokenFn (atLeastFn 3 (skipChFn ':'))) >>
guardOpenerSize >>
withTrailing eatSpaces >>
recoverEolWithAtErrPos #[.missing, .node .none nullKind #[]]
(nameAndArgsFn (tail := argEndWs >> optNlWs lineTailWsFn) >>
unlessLineEndConsumed (wsFallback
(ignoreFn (newlineOrUnexpected "positional argument, named argument, flag, or newline"))
base))) >>
wsFallback lineTailWsFn base >>
-- The opening delimiter's position governs the contents and the closing delimiter.
(withDelimiterAt 2 fun delimiterPos delimiter =>
withPositionAt delimiterPos <|
blocksFn {ctxt with topLevel := false, maxDirective := delimiter.lengthAssumingAscii} >>
recoverHereWith #[.missing]
(unterminatedDelimiterAtEnd delimiterPos delimiter "directive" >>
closeDelimiter delimiterPos delimiter))
where
withDelimiter (atDepth : Nat) (p : SourceInfo → String → ParserFn) : ParserFn := fun c s =>
match delimiterAtom (s.stxStack.get! (s.stxStack.size - (atDepth + 1))) with
| .atom info str =>
if str.all (· == ':') then
p info str c s
else
s.mkError s!"Internal error - index {atDepth} wasn't the directive delimiter - it was the atom {str}"
| .missing => s.pushSyntax .missing
| stx =>
s.mkError s!"Internal error - index {atDepth} wasn't the directive delimiter - it was {stx} in {s.stxStack.back}, {s.stxStack.pop.back}, {s.stxStack.pop.pop.back}, {s.stxStack.pop.pop.pop.back}"
delimiterAtom : Syntax → Syntax
| .node _ _ #[a] => a
| stx => stx
withDelimiterSize (atDepth : Nat) (p : Nat → ParserFn) : ParserFn :=
withDelimiter atDepth fun _ str => p str.lengthAssumingAscii -- `str` is made up of all `':'`
/-- Runs `p` with the position and the text of the delimiter at `atDepth`. -/
withDelimiterAt (atDepth : Nat) (p : String.Pos.Raw → String → ParserFn) : ParserFn :=
withDelimiter atDepth fun info str c s =>
match info.getPos? with
| some pos => p pos str c s
| none => s.mkError "internal error: expected a positioned directive delimiter"
guardOpenerSize : ParserFn := withDelimiterSize 0 fun x =>
if let some m := ctxt.maxDirective then
if x < m then skipFn else fun _ s => s.mkError "Too many ':'s here"
else skipFn
closeDelimiter (delimiterPos : String.Pos.Raw) (str : String) : ParserFn := fun c s =>
let ⟨line, col⟩ := c.fileMap.toPosition delimiterPos
(bolThen ctxt
(description := s!"closing '{str}' for the directive opened on line {line} at column {col}")
(checkIndentEq str >>
nodeFn ``directiveDelimiter (asTokenFn (strFn str)) >> notFollowedByFn (chFn ':') "':'" >>
withTrailing (eatSpaces >>
ignoreFn lineEnd >>
blockTrailingWs ctxt))) c s
/--
Parses a block command.
-/
-- This low-level definition is to get exactly the right amount of lookahead
-- together with column tracking
public partial def blockCommandFn (ctxt : BlockCtxt) : ParserFn := fun c s =>
let iniPos := s.pos
let iniSz := s.stxStack.size
let iniErrs := s.recoveredErrors
let restorePosOnErr : ParserState → ParserState
| ⟨stack, lhsPrec, _, cache, some msg, _⟩ => ⟨stack, lhsPrec, iniPos, cache, some msg, iniErrs⟩
| other => other
let s := intro c s
if s.hasError then restorePosOnErr s
else
s.mkNode ``Block.command iniSz
where
intro :=
atomicFn' (chFn '{') >> withTrailing eatSpaces >>
nameAndArgsFn (tail := argEndWs) (closes := (· == '}')) >>
nameArgWhitespace >> chFn '}' >>
withTrailing (eatSpaces >> ignoreFn lineEnd >>
blockTrailingWs ctxt)
/--
Parses a link reference target.
-/
public partial def linkRefFn (c : BlockCtxt) : ParserFn :=
nodeFn ``Block.link_ref <|
atLineStart >>
atomicFn' (ignoreFn definitionShape) >>
chFn '[' >>
(recoverLineAtErrPos <|
nodeFn versoRefKind (asTokenFn (refNameFn "a reference name")) >> closeRefNameWith "]:" >>
withTrailing eatSpaces >>
nodeFn versoLinkRefUrlKind (asTokenFn urlFn
(eatSpaces >> ignoreFn lineEnd >>
blockTrailingWs c)))
where
-- Commit earlier for better errors when unambiguous
atLineStart : ParserFn := fun ctx s =>
if atBol c ctx s then s
else if (bracketedName ctx (eatSpaces ctx s)).hasError then
s.mkErrorAt "link reference definition" s.pos
else
let s' := (eatSpaces >> chFn '[') ctx s
s'.setError { unexpectedTk := s'.stxStack.back, unexpected := misplacedMsg }
misplacedMsg := "unexpected link reference definition (must be at start of line)"
/--
Checks whether the current position begins something that looks sufficiently like a link ref
definition to be a syntax error if it is not. Commits if so, fails if not.
This allows fallback in the case of paragraphs that start with text like `[txt][ref]:` or in
case of footnote defs while still providing good errors on bogus defs like `[ref]: example.com`.
-/
definitionShape : ParserFn :=
bol c >> bracketedName
/--
Succeeds where a bracketed name is followed by a colon. A `^` after the bracket opens a footnote
definition instead. The characters between the brackets are read once this succeeds, so one that
a name may not contain is reported at its own position.
-/
bracketedName : ParserFn := fun c s =>
let ok : Bool := Id.run do
let mut i := s.pos
if h : c.atEnd i then return false
else if c.get' i h != '[' then return false
else i := c.next i
if h : c.atEnd i then return false
else if c.get' i h == '^' then return false
repeat
if h : c.atEnd i then return false
else
let ch := c.get' i h
if ch == '\n' then return false
else if ch == ']' then
let j := c.next i
return !c.atEnd j && c.get j == ':'
else i := c.next i
return false
if ok then s else s.mkErrorAt "link reference definition" s.pos
/--
Reads the URL, which ends at the last character on the line that is not a space. The spaces
after it are the token's trailing whitespace.
-/
urlFn : ParserFn := fun c s => Id.run do
let mut stop := s.pos
let mut i := s.pos
repeat
if h : c.atEnd i then break
else
let ch := c.get' i h
if ch == '\n' then break
else
i := c.next i
if ch != ' ' then stop := i
return s.setPos stop
/--
Parses a footnote reference target.
-/
public partial def footnoteRefFn (c : BlockCtxt) : ParserFn :=
nodeFn ``Block.footnote_ref <|
atomicFn' (ignoreFn (bol c) >> strFn "[^" >>
nodeFn versoRefKind (asTokenFn (refNameFn "a footnote name")) >>
strFn "]:") >>
withTrailing eatSpaces >>
notFollowedByFn blockOpenerFn "block opener" >> checkIndentGe "footnote text" >>
textLineFn { baseColumn := c.baseColumn } (recordTrailing := c.recordTrailing)
/--
Parses a block.
When `recordTrailing` is set, the block's final token consumes the whitespace that follows the
block (including the indentation of the next line), even when error recovery lost the token that
would have consumed it.
-/
public partial def blockFn (c : BlockCtxt) : ParserFn :=
noTabs >>
-- The whitespace before a block, this line's indentation included, belongs to the token before
-- it, so a block begins at its own first token, at or past the column the enclosing block
-- saved.
checkIndentGe indentMsg >>
withTrailingFallback
(expectedFn "block opener (at line start: '#', '>', ':', '*', '-', '+', '1.', '```', '%%%', '{…}')" (
blockCommandFn c <|> unorderedListFn c <|> orderedListFn c <|> definitionListFn c <|>
headerFn c <|> codeBlockFn c <|> directiveFn c <|> blockquoteFn c <|>
linkRefFn c <|> footnoteRefFn c <|> metadataBlockFn c) <|>
paraFn c)
where
indentMsg := "block"
withTrailingFallback (block : ParserFn) : ParserFn := fun ctx s =>
if !c.recordTrailing then block ctx s
else
let base := s.stxStack.size
let s := block ctx s
if s.hasError then s else blockSepFallback base ctx s
/--
Reports a tab where a block would begin. This check records the error at the tab, consuming it
so that the message points at the character it is about and the blocks after it still parse.
-/
noTabs : ParserFn := fun c s =>
let s' := eatSpaces c s
if h : c.atEnd s'.pos then s
else if c.get' s'.pos h == '\t' then
let err : Error :=
{ unexpected := "tabs are not allowed; please configure your editor to expand them" }
-- `skipBlank` skips the tab and the rest of a line with nothing else on it, so that a line
-- indented with a tab still parses and a line of only whitespace ends the block before it.
let s'' := skipBlank c (s'.next' c s'.pos h)
{ s'' with recoveredErrors := s''.recoveredErrors.push (s'.pos, s''.stxStack, err) }
else s
/--
Skips a run of whitespace through its last newline, the way a blank line between blocks is
skipped.
-/
skipBlank (c : ParserContext) (s : ParserState) : ParserState := Id.run do
let mut s := s
let mut keep := s.pos
repeat
let i := s.pos
if h : c.atEnd i then break
else
let ch := c.get' i h
if ch == ' ' || ch == '\t' then s := s.next' c i h
else if ch == '\n' then
s := s.next' c i h
keep := s.pos
else break
return s.setPos keep
/--
Parses zero or more blocks. Each block's final token consumes the whitespace that follows it, the
next line's indentation included, and `blockSepFallback` parses the whitespace after a block whose
final token was lost to error recovery.
-/
public partial def blocksFn (c : BlockCtxt) : ParserFn := fun ctx s =>
let base := s.stxStack.size
sepByFn true (blockFn { c with recordTrailing := true }) (blockSepFallback base) ctx s
/--
Parses one or more blocks.
-/
public partial def blocks1Fn (c : BlockCtxt) : ParserFn := fun ctx s =>
let base := s.stxStack.size
sepBy1Fn true (blockFn { c with recordTrailing := true }) (blockSepFallback base) ctx s
/--
Sets the error that ended a sequence of blocks before the end of the input, unless a recovered
error already reports the position it would be set at.
`ctxt` is the context that the blocks were parsed with.
-/
partial def earlyStopErrorFn (ctxt : BlockCtxt) : ParserFn := fun c s =>
if c.atEnd s.pos then s
else
let s' := blockFn ctxt c (mkParserState c.inputString |>.setPos s.pos)
let (errPos, e) : String.Pos.Raw × Error :=
match s'.errorMsg with
| some e => (s'.pos, e)
| none =>
-- A block sequence stops only where a block fails, so this case is a fallback that names
-- the character.
(s.pos, { unexpected := s!"unexpected '{c.get s.pos}'" })
-- A recovered error at the position where the blocks stopped is the error that stopped them.
if s.recoveredErrors.any (·.1 == errPos) then s
else { s with errorMsg := some e, pos := errPos }
/--
Parses a document: the whitespace it starts with, then zero or more blocks.
The document's first token records the whitespace between the position where this parser starts
and that token. A caller that starts it part-way into a larger input is responsible for the
whitespace before that position, which a doc comment's opening delimiter records as its trailing
whitespace.
-/
public partial def documentFn (blockContext : BlockCtxt := {}) : ParserFn := fun c s =>
let startPos := s.pos
let base := s.stxStack.size
-- A doc comment can sit inside Lean syntax that saved a position, such as the field list of a
-- `structure`. Clearing it lets the document's top-level blocks start at any column.
let s := (withNoPosition <| ignoreFn lineTailWsFn >>
blocksFn blockContext >> wsFallback docEndWs base >> earlyStopErrorFn blockContext) c s
if s.hasError then s
else
let s := s.mkNode ``Parser.document base
s.popSyntax.pushSyntax (setStartLeading startPos s.stxStack.back)
end
/--
Whether a Verso element may repeat `ch` to make one delimiter, so that a sequence of them is a
single delimiter rather than several.
-/
def isGrowableVersoDelimiter (ch : Char) : Bool :=
ch == '*' || ch == '_' || ch == '`' || ch == ':' || ch == '#'
/--
Returns the start and end of the range over which a Verso parse error at `pos` is reported, along
with the error to report. An end position of `none` indicates that the error has no range, but is to
be reported at a single point.
An error that includes an unexpected token is reported at that token's range. When such an error has
no message of its own, as with errors from Lean's own parsers, a message is derived from the token.
Otherwise, the range starts at `pos` and covers the character there, or the whole run of it when a
Verso element repeats that character to make a single delimiter (e.g. `***` for bold text). At the
end of the input, and at a newline, the end is `none`.
Whitespace before the error stays outside the range.
-/
public def locateError (ictx : InputContext) (pos : String.Pos.Raw) (e : Error) :
String.Pos.Raw × Option String.Pos.Raw × Error :=
if let some ⟨start, stop⟩ := e.unexpectedTk.getRange? then
-- A parser that names what it found says it better than the token does, so its message stands.
let unexpected :=
if !e.unexpected.isEmpty then
e.unexpected
else
match e.unexpectedTk with
| .ident .. => "unexpected identifier"
| .atom _ val => s!"unexpected token '{val}'"
| _ => "unexpected token"
(start, some stop, { e with unexpected })
else if ictx.atEnd pos || ictx.get pos == '\n' then
(pos, none, e)
else
-- A delimiter is a run of its character, so the whole run is one delimiter to mark.
let ch := ictx.get pos
let stop := if isGrowableVersoDelimiter ch then findCharSeqEnd ictx ch pos else ictx.next pos
(pos, some stop, e)
/--
The syntax kinds of the nodes that `documentFn` produces.
-/
public def documentKinds : List SyntaxNodeKind := [
-- the document and its literal content
``document, versoTextKind, versoRefKind, versoLinkUrlKind, versoLinkRefUrlKind, versoImageAltKind,
versoCodeKind, versoCodeLineKind, versoCodeBlockKind,
-- delimiters and markers
``headerMarker, ``listMarker, ``emphDelimiter, ``boldDelimiter, ``codeDelimiter, ``codeBlockFence,
``directiveDelimiter, ``inlineMathMarker, ``displayMathMarker,
-- argument values and arguments
``ArgVal.str, ``ArgVal.ident, ``ArgVal.num, ``Arg.anon, ``Arg.named, ``Arg.named_no_paren,
``Arg.flag_on, ``Arg.flag_off,
-- link targets
``LinkTarget.url, ``LinkTarget.ref,
-- inline elements
``Inline.text, ``Inline.emph, ``Inline.bold, ``Inline.code, ``Inline.inline_math,
``Inline.display_math, ``Inline.link, ``Inline.image, ``Inline.footnote, ``Inline.linebreak,
``Inline.role,
-- list items
``ListItem.item, ``DescItem.item,
-- block elements
``Block.para, ``Block.ul, ``Block.ol, ``Block.dl, ``Block.blockquote, ``Block.codeblock,
``Block.directive, ``Block.header, ``Block.link_ref, ``Block.footnote_ref, ``Block.metadata_block,
``Block.command
]
/--
Parser information for Verso markup.
Listing the syntax kinds that `documentFn` produces ensures that we don't get spurious module
dependencies.
-/
public def documentInfo : ParserInfo where
-- `docComment` is part of every builtin parser with `declModifiers`. The check prior to the
-- insert saves significant initialization time.
collectKinds s :=
documentKinds.foldl (init := s) fun s k =>
if s.contains k then s else s.insert k
section
open Lean.PrettyPrinter
/--
Parses as `ifVerso` if the option `doc.verso` is `true`, or as `ifNotVerso` otherwise.
-/
public def ifVersoFn (ifVerso ifNotVerso : ParserFn) : ParserFn := fun c s =>
if c.options.getBool `doc.verso then ifVerso c s
else ifNotVerso c s
@[inherit_doc ifVersoFn]
public def ifVerso (ifVerso ifNotVerso : Parser) : Parser where
info := orelseInfo ifVerso.info ifNotVerso.info
fn := ifVersoFn ifVerso.fn ifNotVerso.fn
/--
Formatter for `ifVerso`—formats according to the underlying formatters.
-/
@[combinator_formatter ifVerso, expose]
public def ifVerso.formatter (f1 f2 : Formatter) : Formatter := f1 <|> f2
/--
Parenthesizer for `ifVerso`—parenthesizes according to the underlying parenthesizers.
-/
@[combinator_parenthesizer ifVerso, expose]
public def ifVerso.parenthesizer (p1 p2 : Parenthesizer) : Parenthesizer := p1 <|> p2
/--
Parses as `ifVerso` if module docs should use Verso syntax, or as `ifNotVerso` otherwise.
Checks `doc.verso.module` if explicitly set, otherwise falls back to `doc.verso`.
-/
public def ifVersoModuleDocsFn (ifVerso ifNotVerso : ParserFn) : ParserFn := fun c s =>
let useVerso :=
if c.options.contains `doc.verso.module then
c.options.getBool `doc.verso.module
else
c.options.getBool `doc.verso
if useVerso then ifVerso c s
else ifNotVerso c s
@[inherit_doc ifVersoModuleDocsFn]
public def ifVersoModuleDocs (ifVerso ifNotVerso : Parser) : Parser where
info := orelseInfo ifVerso.info ifNotVerso.info
fn := ifVersoModuleDocsFn ifVerso.fn ifNotVerso.fn
/--
Formatter for `ifVersoModuleDocs`—formats according to the underlying formatters.
-/
@[combinator_formatter ifVersoModuleDocs, expose]
public def ifVersoModuleDocs.formatter (f1 f2 : Formatter) : Formatter := f1 <|> f2
/--
Parenthesizer for `ifVersoModuleDocs`—parenthesizes according to the underlying parenthesizers.
-/
@[combinator_parenthesizer ifVersoModuleDocs, expose]
public def ifVersoModuleDocs.parenthesizer (p1 p2 : Parenthesizer) : Parenthesizer := p1 <|> p2
/--
Disables the option `doc.verso` while running a parser.
-/
public def withoutVersoSyntax (p : Parser) : Parser where
fn :=
adaptUncacheableContextFn
(fun c => { c with options := c.options.set `doc.verso false })
p.fn
info := p.info
/--
Formatter for `withoutVersoSyntax`—formats according to the underlying formatter.
-/
@[combinator_formatter withoutVersoSyntax, expose]
public def withoutVersoSyntax.formatter (p : Formatter) : Formatter := p
/--
Parenthesizer for `withoutVersoSyntax`—parenthesizes according to the underlying parenthesizer.
-/
@[combinator_parenthesizer withoutVersoSyntax, expose]
public def withoutVersoSyntax.parenthesizer (p : Parenthesizer) : Parenthesizer := p
end
builtin_initialize
register_parser_alias withoutVersoSyntax