Skip to content

Commit

Permalink
Added line number as a parameter to lineInfo in ErrorBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 committed Apr 5, 2024
1 parent 48d55fc commit b2fcfe3
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Text/Gigaparsec/Errors/DefaultErrorBuilder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ indentAndUnlines lines indent = fromString pre <> intercalate (fromString ('\n'
where pre = replicate indent ' '

{-# INLINABLE lineInfoDefault #-}
lineInfoDefault :: String -> [String] -> [String] -> Word -> Word -> [StringBuilder]
lineInfoDefault curLine beforeLines afterLines pointsAt width =
lineInfoDefault :: String -> [String] -> [String] -> Word -> Word -> Word -> [StringBuilder]
lineInfoDefault curLine beforeLines afterLines _line pointsAt width =
concat [map inputLine beforeLines, [inputLine curLine, caretLine], map inputLine afterLines]
where inputLine :: String -> StringBuilder
inputLine = fromString . ('>' :)
Expand Down
3 changes: 2 additions & 1 deletion src/Text/Gigaparsec/Errors/ErrorBuilder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ This class describes how to format an error message generated by a parser into
a form the parser writer desires.
-}
type ErrorBuilder :: * -> Constraint
class (Ord (Item err)) => ErrorBuilder err where
class Ord (Item err) => ErrorBuilder err where
{-|
This is the top level function, which finally compiles all the formatted
sub-parts into a finished value of type @err@.
Expand Down Expand Up @@ -245,6 +245,7 @@ class (Ord (Item err)) => ErrorBuilder err where
lineInfo :: String -- ^ the full line of input that produced this error message.
-> [String] -- ^ the lines of input from just before the one that produced this message (up to 'numLinesBefore').
-> [String] -- ^ the lines of input from just after the one that produced this message (up to 'numLinesAfter').
-> Word -- ^ the line number of the error message
-> Word -- ^ the offset into the line that the error points at.
-> Word -- ^ how wide the caret in the message should be.
-> LineInfo err
Expand Down
4 changes: 2 additions & 2 deletions src/Text/Gigaparsec/Internal/Errors/ParseError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ fromParseError srcFile input err =
(Builder.unexpected @err (either (const Nothing) (Just . fst) unexpectedTok))
(Builder.expected @err (Builder.combineExpectedItems @err (Set.map expectItem expecteds)))
(Builder.combineMessages @err (Set.foldr (\r -> (Builder.reason @err r :)) [] reasons))
(Builder.lineInfo @err curLine linesBefore linesAfter caret (trimToLine caretSize))
(Builder.lineInfo @err curLine linesBefore linesAfter line caret (trimToLine caretSize))
where unexpectedTok = unexpectItem lexicalError <$> unexpected
caretSize = either id snd unexpectedTok

formatErr SpecialisedError{..} =
Builder.specialisedError @err
(Builder.combineMessages @err (map (Builder.message @err) msgs))
(Builder.lineInfo @err curLine linesBefore linesAfter caret (trimToLine caretWidth))
(Builder.lineInfo @err curLine linesBefore linesAfter line caret (trimToLine caretWidth))

expectItem :: ExpectItem -> Builder.Item err
expectItem (ExpectRaw t) = Builder.raw @err t
Expand Down
2 changes: 1 addition & 1 deletion test/Text/Gigaparsec/Internal/TestError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ instance ErrorBuilder TestError where
message = id

type LineInfo TestError = Word
lineInfo _ _ _ _ width = width
lineInfo _ _ _ _ _ width = width

numLinesBefore = 2
numLinesAfter = 2
Expand Down

0 comments on commit b2fcfe3

Please sign in to comment.