String
Utf8
Utf8 : List U8
parse_str :
Parser Utf8 a,
Str
-> Result a
[
ParsingFailure Str,
ParsingIncomplete Str
]
color : Parser Utf8 [Red, Green, Blue] color = one_of( [ Parser.const(Red) |> Parser.skip(string("red")), Parser.const(Green) |> Parser.skip(string("green")), Parser.const(Blue) |> Parser.skip(string("blue")), ], ) expect parse_str(color, "green") == Ok(Green)
parse_str_partial : Parser Utf8 a, Str -> Parser.ParseResult Str a
Runs a parser against the start of a string, allowing the parser to consume it only partially.
- If the parser succeeds, returns the resulting value as well as the leftover input.
- If the parser fails, returns
Err (ParsingFailure msg)
at_sign : Parser Utf8 [AtSign] at_sign = Parser.const(AtSign) |> Parser.skip(codeunit('@')) expect parse_str(at_sign, "@") == Ok(AtSign) expect parse_str_partial(at_sign, "@") |> Result.map_ok(.val) == Ok(AtSign) expect parse_str_partial(at_sign, "$") |> Result.is_err
parse_utf8 :
Parser Utf8 a,
Utf8
-> Result a
[
ParsingFailure Str,
ParsingIncomplete Utf8
]
Runs a parser against a string, requiring the parser to consume it fully.
- If the parser succeeds, returns
Ok a
- If the parser fails, returns
Err (ParsingFailure Str)
- If the parser succeeds but does not consume the full string, returns
Err (ParsingIncomplete (List U8))
parse_utf8_partial : Parser Utf8 a, Utf8 -> Parser.ParseResult Utf8 a
Runs a parser against the start of a list of scalars, allowing the parser to consume it only partially.
codeunit_satisfies : (U8 -> Bool) -> Parser Utf8 U8
is_digit : U8 -> Bool is_digit = \b -> b >= '0' && b <= '9' expect parse_str(codeunit_satisfies(is_digit), "0") == Ok('0') expect parse_str(codeunit_satisfies(is_digit), "*") |> Result.is_err
codeunit : U8 -> Parser Utf8 U8
at_sign : Parser Utf8 [AtSign] at_sign = Parser.const(AtSign) |> Parser.skip(codeunit('@')) expect parse_str(at_sign, "@") == Ok(AtSign) expect Result.is_err(parse_str_partial(at_sign, "$"))
utf8 : List U8 -> Parser Utf8 (List U8)
Parse an extact sequence of utf8
string : Str -> Parser Utf8 Str
Parse the given Str
expect parse_str(string("Foo"), "Foo") == Ok("Foo") expect Result.is_err(parse_str(string("Foo"), "Bar"))
any_codeunit : Parser Utf8 U8
Matches any U8
codeunit
expect parse_str(any_codeunit, "a") == Ok('a') expect parse_str(any_codeunit, "$") == Ok('$')
any_thing : Parser Utf8 Utf8
Matches any Utf8
and consumes all the input without fail.
expect bytes = Str.to_utf8("consumes all the input") Parser.parse(any_thing, bytes, List.is_empty) == Ok(bytes)
any_string : Parser Utf8 Str
digit : Parser Utf8 U64
expect parse_str(digit, "0") == Ok(0) expect Result.is_err(parse_str(digit, "not a digit"))
digits : Parser Utf8 U64
Parse a sequence of digits into a U64
, accepting leading zeroes
expect parse_str(digits, "0123") == Ok(123) expect Result.is_err(parse_str(digits, "not a digit"))
one_of : List (Parser Utf8 a) -> Parser Utf8 a
Try a bunch of different parsers.
The first parser which is tried is the one at the front of the list, and the next one is tried until one succeeds or the end of the list was reached.
bool_parser : Parser Utf8 Bool bool_parser = one_of([string("true"), string("false")]) |> Parser.map(\x -> if x == "true" then Bool.true else Bool.false) expect parse_str(bool_parser, "true") == Ok(Bool.true) expect parse_str(bool_parser, "false") == Ok(Bool.false) expect Result.is_err(parse_str(bool_parser, "not a bool"))