Skip to content

Instantly share code, notes, and snippets.

@stepheneb

stepheneb/myChompUntil.elm

Last active May 27, 2019
Embed
What would you like to do?
module ChompUntilAfter exposing (myChompUntil)
import Parser.Advanced as A exposing ((|.), (|=))
type alias Parser a =
MyParser Never Problem a
type Problem
= BadIndent
| BadKeyword String
type MyParser context problem value
= MyParser (State context -> PStep context problem value)
type Context
= Definition String
| List
| Record
type MyProblem
= Expecting String
nameParser : Parser String
nameParser =
A.succeed identity
|. myChompUntil "\nName :"
|= getName
getName : Parser String
getName =
A.getChompedString <| myChompUntil " Summary"
--- copied and modified from Parser.Advanced
-- ChompUntil : String -> Parser ()
-- ChompUntil str =
-- A.chompUntil (toToken str)
myChompUntil : String -> Parser ()
myChompUntil str =
myChompUntilA (toToken str)
toToken : String -> A.Token MyProblem
toToken str =
A.Token str (Expecting str)
myChompUntilA : A.Token x -> MyParser c x ()
myChompUntilA (A.Token str expecting) =
MyParser <|
\s ->
let
( newOffset, newRow, newCol ) =
Elm.Kernel.Parser.findSubString str s.offset s.row s.col s.src
in
if newOffset == -1 then
Bad False (fromInfo newRow newCol expecting s.context)
else
Good (s.offset < newOffset)
()
{ src = s.src
, offset = newOffset
, indent = s.indent
, context = s.context
, row = newRow
, col = newCol
}
type PStep context problem value
= Good Bool value (State context)
| Bad Bool (Bag context problem)
type alias DeadEnd context problem =
{ row : Int
, col : Int
, problem : problem
, contextStack : List { row : Int, col : Int, context : context }
}
type Bag c x
= Empty
| AddRight (Bag c x) (DeadEnd c x)
| Append (Bag c x) (Bag c x)
fromState : State c -> x -> Bag c x
fromState s x =
AddRight Empty (DeadEnd s.row s.col x s.context)
fromInfo : Int -> Int -> x -> List (Located c) -> Bag c x
fromInfo row col x context =
AddRight Empty (DeadEnd row col x context)
type alias State context =
{ src : String
, offset : Int
, indent : Int
, context : List (Located context)
, row : Int
, col : Int
}
type alias Located context =
{ row : Int
, col : Int
, context : context
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.