-
Notifications
You must be signed in to change notification settings - Fork 21
Improve default error #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
b538454
bc25bae
6a9f093
c225067
182c4aa
39e2b16
8ae7cbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,26 +26,25 @@ type Pos = Int | |
type PosString = { str :: String, pos :: Pos } | ||
|
||
-- | The type of parsing errors. | ||
newtype ParseError = ParseError String | ||
|
||
instance showParseError :: Show ParseError where | ||
show (ParseError msg) = msg | ||
|
||
derive instance eqParseError :: Eq ParseError | ||
|
||
derive instance ordParseError :: Ord ParseError | ||
type ParseError = { error :: String, pos :: Pos } | ||
|
||
-- | A parser is represented as a function which takes a pair of | ||
-- | continuations for failure and success. | ||
newtype Parser a = Parser (PosString -> Either { pos :: Pos, error :: ParseError } { result :: a, suffix :: PosString }) | ||
newtype Parser a = Parser (PosString -> Either ParseError { result :: a, suffix :: PosString }) | ||
|
||
-- | Run a parser by providing success and failure continuations. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These documentation comments seem a bit funky to me; they keep talking about continuations, but that's not what I interpret from this function. Am I missing something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking the same thing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think this can be removed. |
||
unParser :: forall a. Parser a -> PosString -> Either { pos :: Pos, error :: ParseError } { result :: a, suffix :: PosString } | ||
unParser :: forall a. Parser a -> PosString -> Either ParseError { result :: a, suffix :: PosString } | ||
unParser (Parser p) = p | ||
|
||
-- | Run a parser for an input string, returning either an error or a result. | ||
runParser :: forall a. Parser a -> String -> Either ParseError a | ||
runParser (Parser p) s = bimap _.error _.result (p { str: s, pos: 0 }) | ||
-- | Run a parser for an input string. If the parser succeeds, the | ||
-- | result will be returned (i.e. `Right a`). If it fails, the message and | ||
-- | the position where the parser failed will be outputted | ||
-- | as a `String` (i.e. `Left (error <> " ; pos = " <> pos)`) | ||
runParser :: forall a. Parser a -> String -> Either String a | ||
runParser (Parser p) s = bimap printError _.result (p { str: s, pos: 0 }) | ||
where | ||
printError :: ParseError -> String | ||
printError rec = rec.error <> "; pos = " <> show rec.pos | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pushed in latest commits. |
||
|
||
instance functorParser :: Functor Parser where | ||
map f (Parser p) = Parser (map (\{ result, suffix } -> { result: f result, suffix }) <<< p) | ||
|
@@ -93,7 +92,7 @@ instance lazyParser :: Lazy (Parser a) where | |
|
||
-- | Fail with the specified message. | ||
fail :: forall a. String -> Parser a | ||
fail msg = Parser \{ pos } -> Left { pos, error: ParseError msg } | ||
fail error = Parser \{ pos } -> Left { pos, error } | ||
|
||
-- | In case of error, the default behavior is to backtrack if no input was consumed. | ||
-- | | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could potentially create a type alias for this, too:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same record is used in several places, so this would just reduce a bit of duplication. It's not necessary, though.