This library provides facilities for parsing structured text data into concrete Elm values.
The input stream over which Parsers operate.
data is the initial input provided by the userinput is the remainder after running a parseposition is the starting position of input in data after a parseA record representing the current parse location in an InputStream.
source the current line of source codeline the current line number (starting at 1)column the current column (starting at 1)A tuple representing the current parser state, the remaining input
stream and the parse result. Don't worry about this type unless
you're writing your own primitive parsers.
Running a Parser results in one of two states:
Ok res when the parser has successfully parsed the inputErr messages when the parser has failed with a list of error messages.A tuple representing a failed parse. It contains the state after running the parser, the remaining input stream and a list of error messages.
A tuple representing a successful parse. It contains the state after running the parser, the remaining input stream and the result.
Parse a string. See runParser if your parser needs to manage
some internal state.
import Combine.Num exposing (int)
import String
parseAnInteger : String -> Result String Int
parseAnInteger input =
case parse int input of
Ok (_, stream, result) ->
Ok result
Err (_, stream, errors) ->
Err (String.join " or " errors)
parseAnInteger "123"
-- Ok 123
parseAnInteger "abc"
-- Err "expected an integer"Parse a string while maintaining some internal state.
import Combine.Num exposing (int)
import String
type alias Output =
{ count : Int
, integers : List Int
}
statefulInt : Parse Int Int
statefulInt =
-- Parse an int, then increment the state and return the parsed
-- int. It's important that we try to parse the int _first_
-- since modifying the state will always succeed.
int |> ignore (modifyState ((+) 1))
ints : Parse Int (List Int)
ints =
sepBy (string " ") statefulInt
parseIntegers : String -> Result String Output
parseIntegers input =
case runParser ints 0 input of
Ok (state, stream, ints) ->
Ok { count = state, integers = ints }
Err (state, stream, errors) ->
Err (String.join " or " errors)
parseIntegers ""
-- Ok { count = 0, integers = [] }
parseIntegers "1 2 3 45"
-- Ok { count = 4, integers = [1, 2, 3, 45] }
parseIntegers "1 a 2"
-- Ok { count = 1, integers = [1] }Construct a new primitive Parser.
If you find yourself reaching for this function often consider opening a Github issue with the library to have your custom Parsers included in the standard distribution.
Unwrap a parser so it can be applied to a state and an input
stream. This function is useful if you want to construct your own
parsers via primitive. If you're using this outside of the context
of primitive then you might be doing something wrong so try asking
for help on the mailing list.
Here's how you would implement a greedy version of manyTill using
primitive and app:
manyTill : Parser s a -> Parser s x -> Parser s (List a)
manyTill p end =
let
accumulate acc state stream =
case app end state stream of
( rstate, rstream, Ok _ ) ->
( rstate, rstream, Ok (List.reverse acc) )
_ ->
case app p state stream of
( rstate, rstream, Ok res ) ->
accumulate (res :: acc) rstate rstream
( estate, estream, Err ms ) ->
( estate, estream, Err ms )
in
primitive <| accumulate []Defer running a parser until it's actually required. Use this function to avoid "bad-recursion" errors.
type Expression
= ETerm String
| EList (List E)
name : Parser s String
name = whitespace |> keep (regex "[a-zA-Z]+") |> ignore whitespace
term : Parser s Expression
term = andMap name ETerm
list : Parser s Expression
list =
let
-- helper is itself a function so we avoid the case where the
-- value `list` tries to apply itself in its definition.
helper () =
between (string "(") (string ")") (many (or term list))
|> andMap EList
in
-- lazy defers calling helper until it's actually needed.
lazy helper
parse list ""
-- Err ["expected \"(\""]
parse list "()"
-- Ok (EList [])
parse list "(a (b c))"
-- Ok (EList [ETerm "a", EList [ETerm "b", ETerm "c"]])Fail without consuming any input.
parse (fail "some error") "hello"
-- Err ["some error"]Parse an exact string match.
parse (string "hello") "hello world"
-- Ok "hello"
parse (string "hello") "goodbye"
-- Err ["expected \"hello\""]Parse a Regex match.
Regular expressions must match from the beginning of the input and their
subgroups are ignored. A ^ is added implicitly to the beginning of
every pattern unless one already exists.
parse (regex "a+") "aaaaab"
-- Ok "aaaaa"Fail when the input is not empty.
parse end ""
-- Ok ()
parse end "a"
-- Err ["expected end of input"]Parse zero or more whitespace characters.
parse (whitespace |> keep (string "hello")) "hello"
-- Ok "hello"
parse (whitespace |> keep (string "hello")) " hello"
-- Ok "hello"Parse one or more whitespace characters.
parse (whitespace1 |> keep (string "hello")) "hello"
-- Err ["whitespace"]
parse (whitespace1 |> keep (string "hello")) " hello"
-- Ok "hello"Transform the result of a parser.
let
parser =
string "a"
|> map String.toUpper
in
parse parser "a"
-- Ok "A"Run a parser and return the value on the right on success.
parse (string "true" |> onsuccess True) "true"
-- Ok True
parse (string "true" |> onsuccess True) "false"
-- Err ["expected \"true\""]Transform the error of a parser.
let
parser =
string "a"
|> mapError (always ["bad input"])
in
parse parser b
-- Err ["bad input"]Variant of mapError that replaces the Parser's error with a List
of a single string.
parse (string "a" |> onerror "gimme an 'a'") "b"
-- Err ["gimme an 'a'"]Sequence two parsers, passing the result of the first parser to a function that returns the second parser. The value of the second parser is returned on success.
import Combine.Num exposing (int)
choosy : Parser s String
choosy =
let
createParser n =
if n % 2 == 0 then
string " is even"
else
string " is odd"
in
int
|> andThen createParser
parse choosy "1 is odd"
-- Ok " is odd"
parse choosy "2 is even"
-- Ok " is even"
parse choosy "1 is even"
-- Err ["expected \" is odd\""]Sequence two parsers.
import Combine.Num exposing (int)
plus : Parser s String
plus = string "+"
sum : Parser s Int
sum =
int
|> map (+)
|> andMap (plus |> keep int)
parse sum "1+2"
-- Ok 3Run a list of parsers in sequence, accumulating the results. The main use case for this parser is when you want to combine a list of parsers into a single, top-level, parser. For most use cases, you'll want to use one of the other combinators instead.
parse (sequence [string "a", string "b"]) "ab"
-- Ok ["a", "b"]
parse (sequence [string "a", string "b"]) "ac"
-- Err ["expected \"b\""]Consume input while the predicate matches.
parse (while ((/=) ' ')) "test 123"
-- Ok "test"Choose between two parsers.
parse (or (string "a") (string "b")) "a"
-- Ok "a"
parse (or (string "a") (string "b")) "b"
-- Ok "b"
parse (or (string "a") (string "b")) "c"
-- Err ["expected \"a\"", "expected \"b\""]Choose between a list of parsers.
parse (choice [string "a", string "b"]) "a"
-- Ok "a"
parse (choice [string "a", string "b"]) "b"
-- Ok "b"Return a default value when the given parser fails.
letterA : Parser s String
letterA = optional "a" (string "a")
parse letterA "a"
-- Ok "a"
parse letterA "b"
-- Ok "a"Wrap the return value into a Maybe. Returns Nothing on failure.
parse (maybe (string "a")) "a"
-- Ok (Just "a")
parse (maybe (string "a")) "b"
-- Ok NothingApply a parser zero or more times and return a list of the results.
parse (many (string "a")) "aaab"
-- Ok ["a", "a", "a"]
parse (many (string "a")) "bbbb"
-- Ok []
parse (many (string "a")) ""
-- Ok []Parse at least one result.
parse (many1 (string "a")) "a"
-- Ok ["a"]
parse (many1 (string "a")) ""
-- Err ["expected \"a\""]Apply the first parser zero or more times until second parser succeeds. On success, the list of the first parser's results is returned.
string "<!--" |> keep (manyTill anyChar (string "-->"))Parser zero or more occurences of one parser separated by another.
parse (sepBy (string ",") (string "a")) "b"
-- Ok []
parse (sepBy (string ",") (string "a")) "a,a,a"
-- Ok ["a", "a", "a"]
parse (sepBy (string ",") (string "a")) "a,a,b"
-- Ok ["a", "a"]Parse one or more occurences of one parser separated by another.
Parse zero or more occurences of one parser separated and optionally ended by another.
parse (sepEndBy (string ",") (string "a")) "a,a,a,"
-- Ok ["a", "a", "a"]Parse one or more occurences of one parser separated and optionally ended by another.
parse (sepEndBy1 (string ",") (string "a")) ""
-- Err ["expected \"a\""]
parse (sepEndBy1 (string ",") (string "a")) "a"
-- Ok ["a"]
parse (sepEndBy1 (string ",") (string "a")) "a,"
-- Ok ["a"]Parse one or more occurences of p separated by op, recursively
apply all functions returned by op to the values returned by p. See
the examples/Calc.elm file for an example.
Similar to chainl but functions of op are applied in
right-associative order to the values of p. See the
examples/Python.elm file for a usage example.
Parse something between two other parsers.
The parser
between (string "(") (string ")") (string "a")is equivalent to the parser
string "(" |> keep (string "a") |> ignore (string ")")Join two parsers, ignoring the result of the one on the right.
unsuffix : Parser s String
unsuffix =
regex "[a-z]"
|> keep (regex "[!?]")
parse unsuffix "a!"
-- Ok "a"Join two parsers, ignoring the result of the one on the left.
unprefix : Parser s String
unprefix =
string ">"
|> ignore (while ((==) ' '))
|> ignore (while ((/=) ' '))
parse unprefix "> a"
-- Ok "a"Modify the parser's state.
Get the current position in the input stream and pipe it into a parser.
Get the current column and pipe it into a parser.
Get the current (line, column) in the input stream.
Get the current source line in the input stream.
Get the current line in the input stream.
Get the current column in the input stream.
Modify the parser's InputStream.
The Parser type.
At their core,
Parsers wrap functions from somestateand anInputStreamto a tuple representing the newstate, the remainingInputStreamand aParseResult res.