1 {-# LANGUAGE DeriveFunctor #-}
2 {-# LANGUAGE RecordWildCards #-}
3 module Language.TCT.Cell where
5 import Data.Eq (Eq(..))
6 import Data.Function (($), (.))
7 import Data.Functor (Functor)
8 import Data.Maybe (Maybe(..))
9 import Data.Ord (Ord(..))
10 import Data.Semigroup (Semigroup(..))
11 import Data.Sequence (Seq, ViewL(..), ViewR(..))
12 import Data.TreeSeq.Strict (Tree(..))
14 import Text.Show (Show(..), showParen, showString, showChar)
15 import qualified Data.Sequence as Seq
18 -- | NOTE: every 'Cell' as a 'Pos',
19 -- which is useful to indicate matches/errors/warnings/whatever,
20 -- or outputing in a format somehow preserving
21 -- the original input style.
24 { posCell :: {-# UNPACK #-} !Pos
25 , posEndCell :: {-# UNPACK #-} !Pos
27 } deriving (Eq, Ord, Functor)
28 instance Show a => Show (Cell a) where
29 showsPrec p Cell{..} =
32 showChar ' ' . showsPrec 10 posCell .
33 showChar ' ' . showsPrec 10 posEndCell .
34 showChar ' ' . showsPrec 11 unCell
36 instance Semigroup a => Semigroup (Cell a) where
37 Cell bx ex x <> Cell by ey y =
38 Cell (bx`min`by) (ex`max`ey) (x<>y)
39 instance (Monoid a, Semigroup a) => Monoid (Cell a) where
40 mempty = Cell pos1 pos1 mempty
44 lineCell :: Cell a -> Line
45 lineCell = linePos . posCell
46 columnCell :: Cell a -> Column
47 columnCell = columnPos . posCell
50 cell0 = Cell pos0 pos0
52 cell1 = Cell pos1 pos1
54 posSeq :: Seq (Cell a) -> Maybe (Pos,Pos)
56 case Seq.viewl toks of
59 case Seq.viewr toks of
65 posTrees :: Seq (Trees (Cell k) a) -> Maybe (Pos,Pos)
67 case Seq.viewl trees of
71 Nothing -> posTrees ts
74 case Seq.viewr trees of
76 _ :> TreeN _ toks | iiiii->
78 TreeN (Cell bp _ep _) _ :< _ ->
79 case Seq.viewr trees of
81 _ :> TreeN _ toks | iiiii->
88 { linePos :: {-# UNPACK #-} !Line
89 , columnPos :: {-# UNPACK #-} !Column
91 instance Show Pos where
92 showsPrec _p pos = showsPrec 11 (linePos pos,columnPos pos)
93 instance Ord Pos where
94 Pos lx cx `compare` Pos ly cy =
98 posTree :: Tree (Cell k) (Cell a) -> Pos
99 posTree (TreeN c _) = posCell c
100 posTree (Tree0 c) = posCell c
102 posEndTree :: Tree (Cell k) (Cell a) -> Pos
103 posEndTree (TreeN c _) = posEndCell c
104 posEndTree (Tree0 c) = posEndCell c
112 -- | Line in the source file, counting from 1.
116 -- | Column in the source file, counting from 1.