Skip to content

Commit

Permalink
Feat!: add line number information to lineInfo (#35)
Browse files Browse the repository at this point in the history
* chore: version bump

* Added line number as a parameter to `lineInfo` in `ErrorBuilder`
  • Loading branch information
j-mie6 authored Apr 5, 2024
1 parent cc2fcc3 commit 813d839
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Revision history for gigaparsec

## 0.2.6.0 -- TBD
## 0.3.0.0 -- TBD
* Generalised `deriveLiftedConstructors`/`deriveDeferredConstructors` functionality to also work
with pattern synonyms and `forall`s in more places.
* Added line numbering for the line information in the `ErrorBuilder`.

## 0.2.5.1 -- 2024-02-07
* Fixed bug where hints can be revived by the `hide` combinator.
Expand Down
2 changes: 1 addition & 1 deletion gigaparsec.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ name: gigaparsec
-- PVP summary: +-+------- breaking API changes
-- | | +----- non-breaking API additions
-- | | | +--- code changes with no API change
version: 0.2.6.0
version: 0.3.0.0

-- A short (one-line) description of the package.
synopsis:
Expand Down
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 813d839

Please sign in to comment.