Revise parser APIs a bit

This commit is contained in:
Richard Feldman 2021-04-29 20:26:18 -04:00
parent 2181558e69
commit 391a4f13db
3 changed files with 82 additions and 54 deletions

View file

@ -380,29 +380,29 @@ walkBackwardsUsv : Str, { start: state, step: (state, U32 -> state) } -> state
## Return the first [Unicode Scalar Value](http://www.unicode.org/glossary/#unicode_scalar_value) ## Return the first [Unicode Scalar Value](http://www.unicode.org/glossary/#unicode_scalar_value)
## in the string, along with the rest of the string after that USV. ## in the string, along with the rest of the string after that USV.
parseUsv : Str -> Result { usv : U32, rest : Str } [ StrWasEmpty ]* parseUsv : Str -> Result { answer : U32, rest : Str } [ StrWasEmpty ]*
## Return the first [extended grapheme cluster](http://www.unicode.org/glossary/#extended_grapheme_cluster) ## Return the first [extended grapheme cluster](http://www.unicode.org/glossary/#extended_grapheme_cluster)
## in the string, along with the rest of the string after that grapheme. ## in the string, along with the rest of the string after that grapheme.
parseGrapheme : Str -> Result { grapheme : Str, rest : Str } [ StrWasEmpty ]* parseGrapheme : Str -> Result { answer : Str, rest : Str } [ Expected [ Grapheme ]* Str ]*
## If the first string begins with the second, return whatever comes ## If the first string begins with the second, return whatever comes
## after the second. ## after the second.
chompStr : Str, Str -> Result Str [ Expected [ ExactStr Str ]* Bytes ]* chompStr : Str, Str -> Result Str [ Expected [ ExactStr Str ]* Bytes ]*
chompUsv, U32 -> Result Str [ Expected [ ExactUsv U32 ]* Bytes ]* chompUsv, U32 -> Result Str [ Expected [ Usv U32 ]* Bytes ]*
## If the string begins with digits which can represent a valid #U8, return ## If the string begins with digits which can represent a valid #U8, return
## that number along with the rest of the string after the digits. ## that number along with the rest of the string after the digits.
parseU8 : Str -> Result { u8 : U8, rest : Str } [ Expected [ NumU8 ]* Str ]* parseU8 : Str -> Result { answer : U8, rest : Str } [ Expected [ NumU8 ]* Str ]*
parseI8 : Str -> Result { i8 : I8, rest : Str } [ Expected [ NumI8 ]* Str ]* parseI8 : Str -> Result { answer : I8, rest : Str } [ Expected [ NumI8 ]* Str ]*
parseU16 : Str -> Result { u16 : U16, rest : Str } [ Expected [ NumU16 ]* Str ]* parseU16 : Str -> Result { answer : U16, rest : Str } [ Expected [ NumU16 ]* Str ]*
parseI16 : Str -> Result { i16 : I16, rest : Str } [ Expected [ NumI16 ]* Str ]* parseI16 : Str -> Result { answer : I16, rest : Str } [ Expected [ NumI16 ]* Str ]*
parseU32 : Str -> Result { u32 : U32, rest : Str } [ Expected [ NumU32 ]* Str ]* parseU32 : Str -> Result { answer : U32, rest : Str } [ Expected [ NumU32 ]* Str ]*
parseI32 : Str -> Result { i32 : I32, rest : Str } [ Expected [ NumI32 ]* Str ]* parseI32 : Str -> Result { answer : I32, rest : Str } [ Expected [ NumI32 ]* Str ]*
parseU64 : Str -> Result { u64 : U64, rest : Str } [ Expected [ NumU64 ]* Str ]* parseU64 : Str -> Result { answer : U64, rest : Str } [ Expected [ NumU64 ]* Str ]*
parseI64 : Str -> Result { i64 : I64, rest : Str } [ Expected [ NumI64 ]* Str ]* parseI64 : Str -> Result { answer : I64, rest : Str } [ Expected [ NumI64 ]* Str ]*
parseU128 : Str -> Result { u128 : U128, rest : Str } [ Expected [ NumU128 ]* Str ]* parseU128 : Str -> Result { answer : U128, rest : Str } [ Expected [ NumU128 ]* Str ]*
parseI128 : Str -> Result { i128 : I128, rest : Str } [ Expected [ NumI128 ]* Str ]* parseI128 : Str -> Result { answer : I128, rest : Str } [ Expected [ NumI128 ]* Str ]*
parseF64 : Str -> Result { u128 : U128, rest : Str } [ Expected [ NumF64 ]* Str ]* parseF64 : Str -> Result { answer : U128, rest : Str } [ Expected [ NumF64 ]* Str ]*
parseF32 : Str -> Result { i128 : I128, rest : Str } [ Expected [ NumF32 ]* Str ]* parseF32 : Str -> Result { answer : I128, rest : Str } [ Expected [ NumF32 ]* Str ]*

View file

@ -1,39 +0,0 @@
##
##
## Parse an [IPv4](https://en.wikipedia.org/wiki/IPv4) address:
##
## parser : Parser Ip4
## parser =
## a <- keep u8
## _ <- skip (usv '.')
## b <- keep u8
## _ <- skip (usv '.')
## c <- keep u8
## _ <- skip (usv '.')
## d <- keep u8
##
## Parser.succeed (Ip4.fromOctets a b c d)
interface Parser
exposes [ Parser ]
imports []
Parser a :
[
@Parser (Bytes -> Result { answer : a, rest : Bytes } RawProblem)
]
RawProblem : [ ... ]
keep : Parser a, (a -> Parser b) -> Parser b
skip : Parser *, ({} -> Parser b) -> Parser b
str : Parser Str
u8 : Parser U8
u8 = @Parser Str.parseU8 # TODO doesn't work for bytes!
# TODO do we need a separate parser for Str vs Bytes because of encoding? e.g. Parser.Bytes.utf8
i8 : Parser I8

View file

@ -0,0 +1,67 @@
##
##
## Parse an [IPv4](https://en.wikipedia.org/wiki/IPv4) address:
##
## parser : Parser Ip4
## parser =
## a <- keep u8
## _ <- skip (usv '.')
## b <- keep u8
## _ <- skip (usv '.')
## c <- keep u8
## _ <- skip (usv '.')
## d <- keep u8
##
## Parser.succeed (Ip4.fromOctets a b c d)
interface Parser
exposes [ Parser ]
imports []
Parser a :
[
@Parser (Bytes -> Result { answer : a, rest : Bytes } Problem)
]
Problem :
[
Expected
[
NumU8,
NumI8,
NumU16,
NumI16,
NumU32,
NumI32,
NumU64,
NumI64,
NumU128,
NumI128,
NumF64,
NumF32,
Usv U32,
Utf8 Str,
Utf16Le Str,
Utf16Be Str,
GraphemeUtf8,
GraphemeUtf16Le,
GraphemeUtf16Be,
End,
]
Str
]
keep : Parser a, (a -> Parser b) -> Parser b
skip : Parser *, ({} -> Parser b) -> Parser b
utf8 : Parser Str
utf16 : Parser Str
graphemeUtf8 : Parser Str
graphemeUtf16Le : Parser Str
graphemeUtf16Be : Parser Str
usv : Parser U32
u8 : Parser U8
i8 : Parser I8