1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE Rank2Types #-}
5 {-# LANGUAGE TypeFamilies #-}
6 module Language.TCT.Read.Cell where
8 import Control.Applicative (Applicative(..))
9 import Data.Char (Char)
10 import Data.Either (Either(..))
12 import Data.Function (($), (.))
13 import Data.Functor ((<$>))
14 import Data.List.NonEmpty (NonEmpty(..))
15 import Data.Maybe (Maybe(..))
17 import Data.Proxy (Proxy(..))
18 import Data.Semigroup (Semigroup(..))
19 import Data.String (IsString)
20 import Data.Tuple (snd)
21 import System.FilePath (FilePath)
22 import qualified Data.Set as Set
23 import qualified Data.Text.Lazy as TL
24 import qualified Text.Megaparsec as P
26 import Language.TCT.Cell
27 import Language.TCT.Debug
30 -- | Convenient alias.
40 , IsString (P.Tokens s)
41 , P.ShowErrorComponent e
44 -- | Like 'P.satisfy' but with a predicate returning 'Maybe' instead of 'Bool'.
45 p_satisfyMaybe :: P.MonadParsec e s m => (P.Token s -> Maybe a) -> m a
46 p_satisfyMaybe f = check `P.token` Nothing
51 Nothing -> Left (Just $ P.Tokens $ c:|[], Set.empty)
53 p_SourceFile :: Parser e s FilePath
54 p_SourceFile = P.sourceName <$> P.getPosition
56 p_Position :: Parser e s Pos
57 p_Position = (<$> P.getPosition) $ \p ->
59 { pos_line = P.unPos $ P.sourceLine p
60 , pos_column = P.unPos $ P.sourceColumn p
63 p_Cell :: Parser e s a -> Parser e s (Cell a)
65 (\b a e -> Cell b e a)
70 p_LineNum :: Parser e s LineNum
71 p_LineNum = P.unPos . P.sourceLine <$> P.getPosition
73 p_ColNum :: Parser e s ColNum
74 p_ColNum = P.unPos . P.sourceColumn <$> P.getPosition
76 -- | Wrapper around |P.runParser'|
77 -- to use given 'Cell' as starting position.
79 Parsable e StreamCell a =>
81 Parser e StreamCell a ->
83 Either (P.ParseError (P.Token StreamCell) e) a
84 runParserOnCell inp p (Cell bp _ep s) =
85 snd $ P.runParser' (p <* P.eof)
87 { P.stateInput = StreamCell s
88 , P.statePos = pure $ P.SourcePos inp (P.mkPos $ pos_line bp) indent
89 , P.stateTabWidth = indent
90 , P.stateTokensProcessed = 0
92 where indent = debug0 "runParserOnCell: indent" $ P.mkPos $ pos_column bp
94 -- * Type 'StreamCell'
95 -- | Wrap 'TL.Text' to have a 'P.Stream' instance
96 -- whose 'P.advance1' method abuses the tab width state
97 -- to instead pass the line indent.
98 -- This in order to report correct 'P.SourcePos'
99 -- when parsing a 'Cell' containing newlines.
100 newtype StreamCell = StreamCell { unStreamCell :: TL.Text }
101 deriving (IsString,Eq,Ord)
102 instance P.Stream StreamCell where
103 type Token StreamCell = Char
104 type Tokens StreamCell = TL.Text
105 take1_ (StreamCell t) = (StreamCell <$>) <$> P.take1_ t
106 takeN_ n (StreamCell t) = (StreamCell <$>) <$> P.takeN_ n t
107 takeWhile_ f (StreamCell t) = StreamCell <$> P.takeWhile_ f t
108 tokensToChunk _s = P.tokensToChunk (Proxy::Proxy TL.Text)
109 chunkToTokens _s = P.chunkToTokens (Proxy::Proxy TL.Text)
110 chunkLength _s = P.chunkLength (Proxy::Proxy TL.Text)
111 advance1 _s indent (P.SourcePos n line col) c =
113 '\n' -> P.SourcePos n (line <> P.pos1) indent
114 _ -> P.SourcePos n line (col <> P.pos1)
115 advanceN s indent = TL.foldl' (P.advance1 s indent)