-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Begin the ability to give grammars of syntactic forms using antiquotations
- Loading branch information
1 parent
b95a1a5
commit 1649b8d
Showing
4 changed files
with
309 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Lean.Data.Position | ||
import Lean.Parser | ||
|
||
open Lean | ||
|
||
namespace Manual | ||
|
||
def parserInputString [Monad m] [MonadFileMap m] (str : TSyntax `str) : m String := do | ||
let preString := (← getFileMap).source.extract 0 (str.raw.getPos?.getD 0) | ||
let mut code := "" | ||
let mut iter := preString.iter | ||
while !iter.atEnd do | ||
if iter.curr == '\n' then code := code.push '\n' | ||
else | ||
for _ in [0:iter.curr.utf8Size] do | ||
code := code.push ' ' | ||
iter := iter.next | ||
code := code ++ str.getString | ||
return code | ||
|
||
open Lean.Parser in | ||
def runParserCategory (env : Environment) (opts : Lean.Options) (catName : Name) (input : String) (fileName : String := "<example>") : Except (List (Position × String)) Syntax := | ||
let p := andthenFn whitespace (categoryParserFnImpl catName) | ||
let ictx := mkInputContext input fileName | ||
let s := p.run ictx { env, options := opts } (getTokenTable env) (mkParserState input) | ||
if !s.allErrors.isEmpty then | ||
Except.error (toErrorMsg ictx s) | ||
else if ictx.input.atEnd s.pos then | ||
Except.ok s.stxStack.back | ||
else | ||
Except.error (toErrorMsg ictx (s.mkError "end of input")) | ||
where | ||
toErrorMsg (ctx : InputContext) (s : ParserState) : List (Position × String) := Id.run do | ||
let mut errs := [] | ||
for (pos, _stk, err) in s.allErrors do | ||
let pos := ctx.fileMap.toPosition pos | ||
errs := (pos, toString err) :: errs | ||
errs.reverse | ||
|
||
open Lean.Parser in | ||
def runParser (env : Environment) (opts : Lean.Options) (p : Parser) (input : String) (fileName : String := "<example>") : Except (List (Position × String)) Syntax := | ||
let ictx := mkInputContext input fileName | ||
let s := p.fn.run ictx { env, options := opts } (getTokenTable env) (mkParserState input) | ||
if !s.allErrors.isEmpty then | ||
Except.error (toErrorMsg ictx s) | ||
else if ictx.input.atEnd s.pos then | ||
Except.ok s.stxStack.back | ||
else | ||
Except.error (toErrorMsg ictx (s.mkError "end of input")) | ||
where | ||
toErrorMsg (ctx : InputContext) (s : ParserState) : List (Position × String) := Id.run do | ||
let mut errs := [] | ||
for (pos, _stk, err) in s.allErrors do | ||
let pos := ctx.fileMap.toPosition pos | ||
errs := (pos, toString err) :: errs | ||
errs.reverse |
Oops, something went wrong.