Skip to content

Commit

Permalink
redesigned layout, literate, and language parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmett committed Sep 16, 2011
1 parent 7119eea commit 116d637
Show file tree
Hide file tree
Showing 30 changed files with 719 additions and 444 deletions.
4 changes: 2 additions & 2 deletions Text/Trifecta/Diagnostic/Rendering/Caret.hs
Expand Up @@ -61,11 +61,11 @@ addCaret :: Delta -> Rendering -> Rendering
addCaret p r = drawCaret p .# r

caret :: MonadParser m => m Caret
caret = Caret <$> mark <*> line
caret = Caret <$> position <*> line

careted :: MonadParser m => m a -> m (Careted a)
careted p = do
m <- mark
m <- position
l <- line
a <- p
return $ a :^ Caret m l
Expand Down
12 changes: 6 additions & 6 deletions Text/Trifecta/Diagnostic/Rendering/Span.hs
Expand Up @@ -95,13 +95,13 @@ instance Bind Spanned where
b :~ t -> b :~ (s <> t)

instance Foldable Spanned where
foldMap f (a :~ _) = f a
foldMap f (a :~ _) = f a

instance Traversable Spanned where
traverse f (a :~ s) = (:~ s) <$> f a

instance Foldable1 Spanned where
foldMap1 f (a :~ _) = f a
foldMap1 f (a :~ _) = f a

instance Traversable1 Spanned where
traverse1 f (a :~ s) = (:~ s) <$> f a
Expand All @@ -118,8 +118,8 @@ instance Hashable Span where
instance Hashable a => Hashable (Spanned a) where
hash (a :~ s) = hash a `hashWithSalt` s

span :: MonadParser m => m a -> m Span
span p = (\s l e -> Span s e l) <$> mark <*> line <*> (p *> mark)
span :: MonadParser m => m a -> m Span
span p = (\s l e -> Span s e l) <$> position <*> line <*> (p *> position)

spanned :: MonadParser m => m a -> m (Spanned a)
spanned p = (\s l a e -> a :~ Span s e l) <$> mark <*> line <*> p <*> mark
spanned p = (\s l a e -> a :~ Span s e l) <$> position <*> line <*> p <*> position
2 changes: 2 additions & 0 deletions Text/Trifecta/Parser.hs
Expand Up @@ -15,6 +15,7 @@ module Text.Trifecta.Parser
, module Text.Trifecta.Parser.Class
, module Text.Trifecta.Parser.Char
, module Text.Trifecta.Parser.Combinators
, module Text.Trifecta.Parser.Identifier
, module Text.Trifecta.Parser.Token
, module Text.Trifecta.Parser.Result
, module Text.Trifecta.Parser.Language
Expand All @@ -37,6 +38,7 @@ import Text.Trifecta.Parser.ByteString
import Text.Trifecta.Parser.Class
import Text.Trifecta.Parser.Char
import Text.Trifecta.Parser.Combinators
import Text.Trifecta.Parser.Identifier
import Text.Trifecta.Parser.Token
import Text.Trifecta.Parser.Result
import Text.Trifecta.Parser.Language
Expand Down
30 changes: 15 additions & 15 deletions Text/Trifecta/Parser/ByteString.hs
Expand Up @@ -5,11 +5,11 @@
-- Module : Text.Trifecta.Parser.ByteString
-- Copyright : (c) Edward Kmett 2011
-- License : BSD-style (see the LICENSE file)
--
--
-- Maintainer : ekmett@gmail.com
-- Stability : experimental
-- Portability : non-portable (mptcs, fundeps)
--
--
-- Loading a file as a strict bytestring in one step.
--
-----------------------------------------------------------------------------
Expand All @@ -26,7 +26,7 @@ import Data.Semigroup
import Data.Foldable
import qualified Data.ByteString as B
import System.Console.Terminfo.PrettyPrint
import Text.Trifecta.Parser.Class
import Text.Trifecta.Parser.Mark
import Text.Trifecta.Parser.Prim
import Text.Trifecta.Parser.Step
import Text.Trifecta.Rope.Delta
Expand All @@ -48,9 +48,9 @@ import qualified Data.FingerTree as F
-- > Just a -> print $ sum a

parseFromFile :: Show a => Parser String a -> String -> IO (Maybe a)
parseFromFile p fn = do
parseFromFile p fn = do
result <- parseFromFileEx p fn
case result of
case result of
Success xs a -> Just a <$ unless (Seq.null xs) (displayLn (toList xs))
Failure xs -> Nothing <$ unless (Seq.null xs) (displayLn (toList xs))

Expand All @@ -62,19 +62,19 @@ parseFromFile p fn = do
-- > result <- parseFromFileEx (many number) "digits.txt"
-- > case result of
-- > Failure xs -> unless (Seq.null xs) $ displayLn xs
-- > Success xs a ->
-- > Success xs a ->
-- > unless (Seq.null xs) $ displayLn xs
-- > print $ sum a
-- >

parseFromFileEx :: Show a => Parser String a -> String -> IO (Result TermDoc a)
parseFromFileEx p fn = k <$> B.readFile fn where
k i = starve
parseFromFileEx p fn = k <$> B.readFile fn where
k i = starve
$ feed (rope (F.fromList [LineDirective (UTF8.fromString fn) 0, strand i]))
$ stepParser (fmap prettyTerm)
(why prettyTerm)
(release (Directed (UTF8.fromString fn) 0 0 0 0) *> p)
mempty
True
mempty
mempty
$ stepParser (fmap prettyTerm)
(why prettyTerm)
(release (Directed (UTF8.fromString fn) 0 0 0 0) *> p)
mempty
True
mempty
mempty

0 comments on commit 116d637

Please sign in to comment.