parsix 0.1.0.2 → 0.1.0.3
raw patch · 7 files changed
+53/−27 lines, 7 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Text.Parsix.Parser.Internal: instance Data.Semigroup.Semigroup a => Data.Semigroup.Semigroup (Text.Parsix.Parser.Internal.Parser a)
- Text.Parsix.Result: instance Data.Semigroup.Semigroup Text.Parsix.Result.ErrorInfo
+ Text.Parsix.Parser.Internal: instance GHC.Base.Semigroup a => GHC.Base.Semigroup (Text.Parsix.Parser.Internal.Parser a)
+ Text.Parsix.Parser.Internal: runParser :: Parser a -> Text -> FilePath -> Position -> Result (a, Position, Highlights)
+ Text.Parsix.Position: prettySpan :: (Highlight -> AnsiStyle) -> Span -> Text -> Highlights -> Doc AnsiStyle
+ Text.Parsix.Result: instance GHC.Base.Semigroup Text.Parsix.Result.ErrorInfo
Files
- CHANGELOG.md +4/−0
- LICENSE +1/−1
- parsix.cabal +2/−2
- src/Text/Parsix/Parser/Internal.hs +16/−10
- src/Text/Parsix/Position.hs +28/−12
- tests/Empty.hs +1/−1
- tests/NotFollowedBy.hs +1/−1
CHANGELOG.md view
@@ -1,3 +1,7 @@+# 0.1.0.3++- Add `prettySpan` function+ # 0.1.0.2 - Remove unused `bytestring` dependency
LICENSE view
@@ -1,4 +1,4 @@-Copyright Olle Fredriksson (c) 2017+Copyright Olle Fredriksson (c) 2017-2019 All rights reserved.
parsix.cabal view
@@ -1,5 +1,5 @@ name: parsix-version: 0.1.0.2+version: 0.1.0.3 synopsis: Parser combinators with slicing, error recovery, and syntax highlighting description: A parser combinator library based on 'parsers' (like 'trifecta') with slicing, error recovery, and syntax highlighted diagnostics homepage: https://github.com/ollef/parsix@@ -7,7 +7,7 @@ license-file: LICENSE author: Olle Fredriksson maintainer: fredriksson.olle@gmail.com-copyright: 2017-2018 Olle Fredriksson+copyright: 2017-2019 Olle Fredriksson category: Parsing build-type: Simple extra-source-files: README.md
src/Text/Parsix/Parser/Internal.hs view
@@ -165,6 +165,21 @@ lookAhead (Parser p) = Parser $ \s0 s e0 e pos -> p s0 (\a _ _ -> s a mempty pos) e0 e pos +runParser+ :: Parser a+ -> Text+ -> FilePath+ -> Position+ -> Result (a, Position, Highlights)+runParser (Parser p) inp file start = p+ (\res _ -> Success (res, start, mempty))+ (\res _ pos hl -> Success (res, pos, hl))+ (\err -> Failure $ Error err start inp mempty file)+ (\err pos hl -> Failure $ Error err pos inp hl file)+ start+ mempty+ inp+ parseFromFile :: MonadIO m => Parser a -> FilePath -> m (Maybe a) parseFromFile p file = do result <- parseFromFileEx p file@@ -182,16 +197,7 @@ -- | @parseText p i file@ runs a parser @p@ on @i@. @file@ is only used for -- reporting errors. parseText :: Parser a -> Text -> FilePath -> Result a-parseText (Parser p) inp file = p- (\res _ -> Success res)- (\res _ _pos _hl -> Success res)- (\err -> Failure $ Error err start inp mempty file)- (\err pos hl -> Failure $ Error err pos inp hl file)- start- mempty- inp- where- start = Position 0 0 0+parseText p inp file = (\(a, _, _) -> a) <$> runParser p inp file (Position 0 0 0) -- | @parseString p i file@ runs a parser @p@ on @i@. @file@ is only used for -- reporting errors.
src/Text/Parsix/Position.hs view
@@ -44,27 +44,43 @@ -> Text -> Highlights -> Doc AnsiStyle-prettyPosition style pos inp hl- = rowStringPadding <> bar <> line- <> prettyRow <> bar <+> fmap style (positionRow pos inp hl) <> line- <> rowStringPadding <> bar <+> pretty positionPadding <> annotate (color Red) "^"+prettyPosition style pos = prettySpan style $ Span pos pos++data Span = Span+ { spanStart :: !Position+ , spanEnd :: !Position+ } deriving (Eq, Ord, Show)++prettySpan+ :: (Highlight -> AnsiStyle)+ -> Span+ -> Text+ -> Highlights+ -> Doc AnsiStyle+prettySpan style (Span startPos endPos) inp hl+ = rowNumberStringPadding <> bar <> line+ <> prettyRowNumber <> bar <+> fmap style rowString <> line+ <> rowNumberStringPadding <> bar <+> pretty positionPadding <> annotate (color Red) ("^" <> pretty (Text.replicate squiggleLength "~")) where+ rowString = positionRow startPos inp hl barHighlight = annotate (color Blue) bar = barHighlight "|"- prettyRow = barHighlight $ pretty rowString- rowString = Text.pack (show $ visualRow pos + 1) <> " "- rowStringPadding = pretty $ Text.replicate (Text.length rowString) " "+ prettyRowNumber = barHighlight $ pretty rowNumberString+ rowNumberString = Text.pack (show $ visualRow startPos + 1) <> " "+ rowNumberStringPadding = pretty $ Text.replicate (Text.length rowNumberString) " " positionPadding = Text.map go $ codeUnitSlice start end inp where start = prevNewline inp end- end = codeUnits pos+ end = codeUnits startPos go '\t' = '\t' go _ = ' ' -data Span = Span- { spanStart :: !Position- , spanEnd :: !Position- } deriving (Eq, Ord, Show)+ squiggleEnd =+ if visualRow endPos > visualRow startPos then+ nextNewline inp $ codeUnits startPos+ else+ codeUnits endPos+ squiggleLength = squiggleEnd - codeUnits startPos - 1
tests/Empty.hs view
@@ -6,6 +6,6 @@ tests :: TestTree tests = testGroup "Empty parsers"- [ testProperty "mempty succeeds" $ isSuccess . parse (mempty :: Parser ())+ [ testProperty "mempty succeeds" $ Util.isSuccess . parse (mempty :: Parser ()) , testProperty "empty fails" $ isFailure . parse empty ]
tests/NotFollowedBy.hs view
@@ -16,6 +16,6 @@ $ \x y -> isFailure $ parse (anyChar *> notFollowedBy (char y) *> char y) x , testProperty "4"- $ \x -> isSuccess+ $ \x -> Util.isSuccess $ parse (notFollowedBy (char x) <|> char x *> pure ()) [x] ]