Give parse more useful output, add more useful errors
This commit is contained in:
parent
372160b040
commit
52b3b5f889
10
src/Expr.elm
10
src/Expr.elm
@ -16,7 +16,8 @@ type Expr
|
|||||||
|
|
||||||
num : Parser () Expr
|
num : Parser () Expr
|
||||||
num =
|
num =
|
||||||
Num <$> Num.float <|> Num << toFloat <$> Num.int
|
(Num <$> Num.float <|> Num << toFloat <$> Num.int)
|
||||||
|
<?> "expected a number (int or float)"
|
||||||
|
|
||||||
stringIs : String -> a -> Parser s a
|
stringIs : String -> a -> Parser s a
|
||||||
stringIs str val =
|
stringIs str val =
|
||||||
@ -33,12 +34,15 @@ op =
|
|||||||
group : Parser () Expr
|
group : Parser () Expr
|
||||||
group =
|
group =
|
||||||
between (string "(") (string ")") (sepBy1 whitespace (lazy <| \_ -> parser)) -- Avoid bad recursion issues using lazy parser evaluation
|
between (string "(") (string ")") (sepBy1 whitespace (lazy <| \_ -> parser)) -- Avoid bad recursion issues using lazy parser evaluation
|
||||||
|
<?> "expected a group (whitespace-separated expressions between brackets)"
|
||||||
|> map Group
|
|> map Group
|
||||||
|
|
||||||
parser : Parser () Expr
|
parser : Parser () Expr
|
||||||
parser =
|
parser =
|
||||||
(lazy <| \_ -> group) <|> op <|> num
|
(lazy <| \_ -> group) <|> op <|> num
|
||||||
|
|
||||||
parse : String -> Result (ParseErr ()) (ParseOk () Expr)
|
parse : String -> Result (List String) Expr
|
||||||
parse =
|
parse =
|
||||||
Combine.parse parser
|
Combine.parse parser
|
||||||
|
>> Result.mapError (\(_, _, errorList) -> errorList)
|
||||||
|
>> Result.map (\(_, _, expr) -> expr) -- Convert errors/results to nicer format
|
Loading…
Reference in New Issue
Block a user