Add DTC HTML5 writing draft.
authorJulien Moutinho <julm+tct@autogeree.net>
Thu, 7 Dec 2017 21:51:48 +0000 (22:51 +0100)
committerJulien Moutinho <julm+tct@autogeree.net>
Thu, 7 Dec 2017 21:51:48 +0000 (22:51 +0100)
Language/DTC/Document.hs
Language/DTC/Read/TCT.hs
Language/DTC/Sym.hs
Language/DTC/Write/HTML5.hs
Language/DTC/Write/XML.hs
Language/RNC/Sym.hs
Language/RNC/Write.hs
Language/TCT/Cell.hs
Language/TCT/Read/Cell.hs
Language/TCT/Read/Token.hs
Language/TCT/Write/XML.hs

index 1d8f8605541bbfcc28e823de8c573eb80939b779..f498de03fb02c15e81f6835443b5711f8509b8ea 100644 (file)
@@ -16,6 +16,7 @@ import Data.String (IsString(..))
 import Data.Text (Text)
 import Text.Show (Show)
 import qualified Data.Sequence as Seq
+import Language.TCT.Write.XML (XmlPos(..))
 
 -- * Class 'Default'
 class Default a where
@@ -116,6 +117,7 @@ data Body
            , title   :: Title
            , aliases :: [Alias]
            , body    :: [Body]
+           , pos     :: XmlPos
            }
  | Verticals [Vertical]
  deriving (Eq,Show)
@@ -124,31 +126,40 @@ data Body
 data Vertical
  = Para    { attrs :: CommonAttrs
            , horis :: Horizontals
+           , pos   :: XmlPos
            }
  | OL      { attrs :: CommonAttrs
            , items :: [Verticals]
+           , pos   :: XmlPos
            }
  | UL      { attrs :: CommonAttrs
            , items :: [Verticals]
+           , pos   :: XmlPos
            }
  | RL      { attrs :: CommonAttrs
            , refs  :: [Reference]
+           , pos   :: XmlPos
            }
  | ToC     { attrs :: CommonAttrs
            , depth :: Maybe Int
+           , pos   :: XmlPos
            }
  | ToF     { attrs :: CommonAttrs
            , depth :: Maybe Int
+           , pos   :: XmlPos
            }
  | Index   { attrs :: CommonAttrs
+           , pos   :: XmlPos
            }
  | Figure  { type_ :: Text
            , attrs :: CommonAttrs
            , title :: Title
            , verts :: Verticals
+           , pos   :: XmlPos
            }
  | Artwork { attrs :: CommonAttrs
            , art   :: Artwork
+           , pos   :: XmlPos
            }
  | Comment Text
  deriving (Eq,Show)
@@ -160,6 +171,12 @@ data CommonAttrs
  ,   classes :: [Text]
  } deriving (Eq,Show)
 
+-- * Type 'Auto'
+data Auto
+ =   Auto
+ {   auto_id :: Ident
+ } deriving (Eq,Show)
+
 -- * Type 'Verticals'
 type Verticals = [Vertical]
 
index 24f1ff71262073940371821b2cfdbe446c6763bb..efb485a046ca26051d8b6440eb5af3b1be13cb90 100644 (file)
@@ -10,6 +10,8 @@
 -- | Read DTC from TCT.
 module Language.DTC.Read.TCT where
 
+-- import Control.Monad.Trans.Class (MonadTrans(..))
+-- import qualified Control.Monad.Trans.Reader as R
 import Control.Applicative (Applicative(..))
 import Control.Monad (Monad(..))
 import Data.Bool
@@ -20,7 +22,7 @@ import Data.Function (($), (.), const, id)
 import Data.Functor ((<$>), (<$))
 import Data.Int (Int)
 import Data.List.NonEmpty (NonEmpty(..))
-import Data.Maybe (Maybe(..), fromMaybe)
+import Data.Maybe (Maybe(..), fromMaybe, maybe)
 import Data.Monoid (Monoid(..))
 import Data.Ord (Ord(..))
 import Data.Proxy (Proxy(..))
@@ -30,25 +32,29 @@ import Data.String (String)
 import Data.Text (Text)
 import Data.Tuple (snd)
 import GHC.Exts (toList)
+import Prelude (Num(..))
 import Text.Read (readMaybe)
 import Text.Show (Show(..))
+import qualified Control.Monad.Trans.State as S
 import qualified Data.List as List
+import qualified Data.Map.Strict as Map
 import qualified Data.Sequence as Seq
 import qualified Data.Set as Set
 import qualified Data.Text as Text
 import qualified Text.Megaparsec as P
 import qualified Text.Megaparsec.Perm as P
 
-import Language.TCT hiding (Parser)
-import Language.TCT.Write.XML (XML,XMLs,XmlLeaf(..))
 import Language.DTC.Document (Nat(..), Nat1(..))
+import Language.TCT hiding (Parser)
+import Language.TCT.Write.XML (XML,XMLs,XmlLeaf(..),XmlPos(..))
 import qualified Language.DTC.Document as DTC
 import qualified Language.DTC.Sym as DTC
 import qualified Language.RNC.Sym as RNC
 import qualified Language.TCT.Write.XML as XML
 
 -- * Type 'Parser'
-type Parser = P.Parsec Error XMLs
+-- type Parser = P.Parsec Error XMLs
+type Parser = S.StateT XmlPos (P.Parsec Error XMLs)
 
 instance RNC.Sym_Rule Parser where
        -- rule n p = P.dbg s p {-(p P.<?> s)-} where s = Text.unpack n
@@ -56,7 +62,7 @@ instance RNC.Sym_Rule Parser where
 instance RNC.Sym_RNC Parser where
        none = P.label "none" $ P.eof
        any  = P.label "any" $ p_satisfyMaybe $ const $ Just ()
-       anyElem p = P.dbg "anyElem" $ P.label "anyElem" $ do
+       anyElem p = P.label "anyElem" $ do
                (n,ts) <- P.token check $ Just expected
                parserXMLs (p n) ts
                where
@@ -65,9 +71,21 @@ instance RNC.Sym_RNC Parser where
                check t = Left
                 ( Just $ P.Tokens $ pure t
                 , Set.singleton $ P.Tokens $ pure expected )
+       position p = do
+               st <- S.get
+               ($ st) <$> p
        element n p = do
                ts <- P.token check $ Just expected
-               parserXMLs p ts
+               xp <- S.get
+               S.put xp
+                { xmlPosAncestors          = (n,maybe 1 (+1) $ Map.lookup n $ xmlPosPrecedingsSiblings xp):xmlPosAncestors xp
+                , xmlPosPrecedingsSiblings = mempty
+                }
+               parserXMLs p ts <* S.put xp
+                { xmlPosPrecedingsSiblings =
+                       Map.insertWith (\_new old -> old + 1) n 1 $
+                               xmlPosPrecedingsSiblings xp
+                }
                where
                expected = TreeN (cell0 n) mempty
                check (TreeN (unCell -> e) ts) | e == n = Right ts
@@ -142,14 +160,22 @@ readDTC ::
  DTC.Sym_DTC Parser =>
  XMLs ->
  Either (P.ParseError (P.Token XMLs) Error) DTC.Document
-readDTC = parseXMLs (P.initialPos "") DTC.document
+readDTC =
+       parseXMLs
+        XmlPos { xmlPosAncestors = []
+               , xmlPosPrecedingsSiblings = mempty
+               }
+        (P.initialPos "")
+        DTC.document
 
 parseXMLs ::
  DTC.Sym_DTC Parser =>
+ XmlPos ->
  P.SourcePos -> Parser a -> XMLs ->
  Either (P.ParseError (P.Token XMLs) Error) a
-parseXMLs pos p i =
-       snd $ P.runParser' (p <* RNC.none)
+parseXMLs xp pos p i =
+       snd $
+       P.runParser' ((`S.evalStateT` xp) $ p <* RNC.none)
         P.State
                 { P.stateInput = i
                 , P.statePos = pure $
@@ -161,13 +187,14 @@ parseXMLs pos p i =
                 , P.stateTokensProcessed = 0
                 }
 
--- | @parserXMLs pos p xs@ returns a 'Parser' parsing @xs@ with @p@.
+-- | @parserXMLs xp pos p xs@ returns a 'Parser' parsing @xs@ with @p@ from position @xp@.
 parserXMLs ::
  DTC.Sym_DTC Parser =>
  Parser a -> XMLs -> Parser a
 parserXMLs p xs = do
        pos <- P.getPosition
-       case parseXMLs pos p xs of
+       xp <- S.get
+       case parseXMLs xp pos p xs of
         Left (P.TrivialError (posErr:|_) un ex) -> do
                P.setPosition posErr
                P.failure un ex
@@ -204,7 +231,6 @@ sourcePos :: Pos -> Maybe P.SourcePos
 sourcePos (Pos l c) | l>0 && c>0 = Just $ P.SourcePos "" (P.mkPos l) (P.mkPos c)
 sourcePos _ = Nothing
 
--- ** Type 'XMLs'
 instance P.Stream XMLs where
        type Token  XMLs = XML
        type Tokens XMLs = XMLs
index 918b52102ea5cd46c2c0bd4f3763fcf6da65a365..343c90013bf8da9a90030c2d6df3a34e1e570a41 100644 (file)
@@ -71,6 +71,7 @@ class RNC.Sym_RNC repr => Sym_DTC repr where
                        choice
                         [ rule "section" $
                                element "section" $
+                               position $
                                DTC.Section
                                 <$> commonAttrs
                                 <*> title
@@ -101,25 +102,39 @@ class RNC.Sym_RNC repr => Sym_DTC repr where
        vertical = rule "vertical" $
                choice
                 [ DTC.Comment <$> comment
-                , element "para" $ DTC.Para
+                , element "para" $
+                       position $
+                       DTC.Para
                         <$> commonAttrs
                         <*> horizontals
-                , element "ol" $ DTC.OL
+                , element "ol" $
+                       position $
+                       DTC.OL
                         <$> commonAttrs
                         <*> many (element "li" $ many vertical)
-                , element "ul" $ DTC.UL
+                , element "ul" $
+                       position $
+                       DTC.UL
                         <$> commonAttrs
                         <*> many (element "li" $ many vertical)
-                , element "rl" $ DTC.RL
+                , element "rl" $
+                       position $
+                       DTC.RL
                         <$> commonAttrs
                         <*> many reference
-                , element "toc" $ DTC.ToC
+                , element "toc" $
+                       position $
+                       DTC.ToC
                         <$> commonAttrs
                         <*> optional (attribute "depth" int)
-                , element "tof" $ DTC.ToF
+                , element "tof" $
+                       position $
+                       DTC.ToF
                         <$> commonAttrs
                         <*> optional (attribute "depth" int)
-                , element "index" $ DTC.Index
+                , element "index" $
+                       position $
+                       DTC.Index
                         <$> commonAttrs
                         <*  any
                 , figure
@@ -132,6 +147,7 @@ class RNC.Sym_RNC repr => Sym_DTC repr where
        figure =
                rule "figure" $
                element "figure" $
+               position $
                DTC.Figure
                 <$> attribute "type" text
                 <*> commonAttrs
index 3557049f7b648b819816f41250800cef39828ae3..09b862021c3bdd11093b881d3636a4b442c4e740 100644 (file)
@@ -1,23 +1,43 @@
+{-# LANGUAGE DeriveFunctor #-}
 {-# LANGUAGE DisambiguateRecordFields #-}
 {-# LANGUAGE DuplicateRecordFields #-}
+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
 {-# LANGUAGE OverloadedStrings #-}
 {-# LANGUAGE RecordWildCards #-}
+{-# LANGUAGE TypeApplications #-}
+{-# LANGUAGE FlexibleInstances #-}
+{-# LANGUAGE ScopedTypeVariables #-}
 {-# LANGUAGE ViewPatterns #-}
 {-# OPTIONS_GHC -fno-warn-orphans #-}
 -- | Render a DTC source file in HTML5.
 module Language.DTC.Write.HTML5 where
 
-import Control.Monad (forM_, mapM_)
+-- import Control.Monad.Trans.Class (MonadTrans(..))
 -- import Data.Bool
 -- import Data.Eq (Eq(..))
+-- import Data.String (IsString(..))
+-- import Prelude (Num(..), undefined)
+import Control.Applicative (Applicative(..))
+import Control.Monad (Monad(..), forM_, mapM_)
+import Data.Eq (Eq(..))
 import Data.Foldable (Foldable(..))
-import Data.Function (($), (.))
-import Data.Functor ((<$>))
-import Data.Maybe (Maybe(..))
+import Data.Function (($), (.), const)
+import Data.Functor (Functor(..), (<$>), ($>))
+import Data.Functor.Compose (Compose(..))
+import Data.Functor.Identity (Identity(..))
+import Data.Int (Int)
+import Data.Map.Strict (Map)
+import Data.Maybe (Maybe(..), fromMaybe)
+import Data.Monoid (Monoid(..))
 import Data.Semigroup (Semigroup(..))
+import Data.Text (Text)
+import Data.Tuple (snd)
 import Text.Blaze ((!))
 import Text.Blaze.Html (Html)
-import qualified Data.List as L
+import Text.Show (Show(..))
+import qualified Control.Monad.Trans.State as S
+import qualified Data.List as List
+import qualified Data.Map.Strict as Map
 import qualified Data.Text as Text
 import qualified Data.Text.Lazy as TL
 import qualified Text.Blaze.Html5 as H
@@ -28,10 +48,63 @@ import Text.Blaze.Utils
 
 import Language.DTC.Document (Document)
 import Language.DTC.Write.XML ()
+import Language.TCT.Write.XML (XmlName(..), XmlPos(..))
 import qualified Language.DTC.Document as DTC
 
+-- import Debug.Trace (trace)
+
 instance H.ToMarkup DTC.Ident where
        toMarkup (DTC.Ident i) = H.toMarkup i
+instance AttrValue XmlPos where
+       attrValue = attrValue . textXmlPosAncestors . xmlPosAncestors
+
+-- * Type 'InhHtml5'
+data InhHtml5
+ =   InhHtml5
+inhHtml5 :: InhHtml5
+inhHtml5 = InhHtml5
+
+{- NOTE: composing state and markups
+type HtmlM st = Compose (S.State st) H.MarkupM
+instance Monad (HtmlM st) where
+       return = pure
+       Compose sma >>= a2csmb =
+               Compose $ sma >>= \ma ->
+                       case ma >>= H.Empty . a2csmb of
+                        H.Append _ma (H.Empty csmb) ->
+                               H.Append ma <$> getCompose csmb
+                        _ -> undefined
+
+($$) :: (Html -> Html) -> HTML -> HTML
+($$) f m = Compose $ f <$> getCompose m
+infixr 0 $$
+-}
+
+unMarkupValue :: H.MarkupM a -> b -> H.MarkupM b
+unMarkupValue = \case
+ H.Parent x0 x1 x2 m          -> H.Parent x0 x1 x2 . unMarkupValue m
+ H.CustomParent x0 m          -> H.CustomParent x0 . unMarkupValue m
+ H.Leaf x0 x1 x2 _            -> H.Leaf x0 x1 x2
+ H.CustomLeaf x0 x1 _         -> H.CustomLeaf x0 x1
+ H.Content x0 _               -> H.Content x0
+ H.Comment x0 _               -> H.Comment x0
+ H.Append x0 m                -> H.Append x0 . unMarkupValue m
+ H.AddAttribute x0 x1 x2 m    -> H.AddAttribute x0 x1 x2 . unMarkupValue m
+ H.AddCustomAttribute x0 x1 m -> H.AddCustomAttribute x0 x1 . unMarkupValue m
+ H.Empty _                    -> H.Empty
+
+markupValue :: H.MarkupM a -> a
+markupValue m0 = case m0 of
+ H.Parent _ _ _ m1           -> markupValue m1
+ H.CustomParent _ m1         -> markupValue m1
+ H.Leaf _ _ _ x              -> x
+ H.CustomLeaf _ _ x          -> x
+ H.Content _ x               -> x
+ H.Comment _ x               -> x
+ H.Append _ m1               -> markupValue m1
+ H.AddAttribute _ _ _ m1     -> markupValue m1
+ H.AddCustomAttribute _ _ m1 -> markupValue m1
+ H.Empty x                   -> x
 
 html5Document :: Document -> Html
 html5Document DTC.Document{..} = do
@@ -41,7 +114,7 @@ html5Document DTC.Document{..} = do
                        H.meta ! HA.httpEquiv "Content-Type"
                               ! HA.content "text/html; charset=UTF-8"
                        whenSome (DTC.titles $ DTC.about (head :: DTC.Head)) $ \ts ->
-                               let t = textHorizontals $ L.head $ (DTC.unTitle <$> ts) <> [[DTC.Plain ""]] in
+                               let t = textHorizontals $ List.head $ (DTC.unTitle <$> ts) <> [[DTC.Plain ""]] in
                                H.title $ H.toMarkup t
                        -- link ! rel "Chapter" ! title "SomeTitle">
                        H.link ! HA.rel "stylesheet"
@@ -49,24 +122,50 @@ html5Document DTC.Document{..} = do
                               ! HA.href "style/dtc-html5.css"
                H.body $
                        forM_ body html5Body
-
 html5Body :: DTC.Body -> Html
 html5Body = \case
         DTC.Section{..} ->
-               html5CommonAttrs attrs $
-               H.section $ do
-                       H.table ! HA.class_ "section-header" $
-                               H.tbody $
-                                       H.tr $ do
-                                               H.td ! HA.class_ "section-number" $
-                                                       "N.N.N"
-                                               H.td ! HA.class_ "section-title" $
-                                                       html5Horizontals $ DTC.unTitle title
+               H.section
+                ! HA.class_ "section"
+                ! HA.id (attrValue pos) $ do
+                       html5CommonAttrs attrs $
+                               H.table ! HA.class_ "section-header" $
+                                       H.tbody $
+                                               H.tr $ do
+                                                       H.td ! HA.class_ "section-number" $ do
+                                                               html5SectionNumber $ xmlPosAncestors pos
+                                                       H.td ! HA.class_ "section-title" $ do
+                                                               html5Horizontals $ DTC.unTitle title
                        forM_ body html5Body
                {- aliases :: [Alias]
                -}
         DTC.Verticals vs -> html5Verticals vs
 
+textXmlPosAncestors :: [(XmlName,Int)] -> Text
+textXmlPosAncestors =
+       snd . foldr (\(n,c) (nParent,acc) ->
+               (n,
+                       (if Text.null acc
+                               then acc
+                               else acc <> ".") <>
+                       Text.pack
+                        (if n == nParent
+                               then show c
+                               else show n<>show c)
+               )
+        ) ("","")
+
+html5SectionNumber :: [(XmlName,Int)] -> Html
+html5SectionNumber = go [] . List.reverse
+       where
+       go :: [(XmlName,Int)] -> [(XmlName,Int)] -> Html
+       go _rs [] = mempty
+       go rs (a@(_n,cnt):as) = do
+               H.a ! HA.href (attrValue $ "#"<>textXmlPosAncestors (a:rs)) $
+                       H.toMarkup $ show cnt
+               H.toMarkup '.'
+               go (a:rs) as
+
 html5Verticals :: [DTC.Vertical] -> Html
 html5Verticals = foldMap html5Vertical
 
@@ -74,28 +173,33 @@ html5Vertical :: DTC.Vertical -> Html
 html5Vertical = \case
         DTC.Para{..} ->
                html5CommonAttrs attrs $
-               H.div ! HA.class_ "para" $
+               H.div ! HA.class_ "para"
+                     ! HA.id (attrValue pos) $ do
                        html5Horizontals horis
         DTC.OL{..} ->
                html5CommonAttrs attrs $
-               H.ol ! HA.class_ "ol" $
+               H.ol ! HA.class_ "ol"
+                    ! HA.id (attrValue pos) $ do
                        forM_ items $ \item ->
                                H.li $ html5Verticals item
         DTC.UL{..} ->
                html5CommonAttrs attrs $
-               H.ul ! HA.class_ "ul" $
+               H.ul ! HA.class_ "ul"
+                    ! HA.id (attrValue pos) $ do
                        forM_ items $ \item ->
                                H.li $ html5Verticals item
         DTC.RL{..} ->
                html5CommonAttrs attrs $
-               H.div ! HA.class_ "rl" $
+               H.div ! HA.class_ "rl"
+                     ! HA.id (attrValue pos) $ do
                        H.table $
                                forM_ refs html5Reference
         DTC.Comment t ->
                H.Comment (H.Text t) ()
         DTC.Figure{..} ->
                html5CommonAttrs attrs $
-               H.div ! HA.class_ (attrValue $ "figure-"<>type_) $ do
+               H.div ! HA.class_ (attrValue $ "figure-"<>type_)
+                     ! HA.id (attrValue pos) $ do
                        H.table ! HA.class_ "figure-caption" $
                                H.tbody $
                                        H.tr $ do
@@ -107,9 +211,13 @@ html5Vertical = \case
                        H.div ! HA.class_ "figure-content" $ do
                                html5Verticals verts
         DTC.ToC{..} ->
-               H.nav ! HA.class_ "toc" $ ""
+               H.nav ! HA.class_ "toc"
+                     ! HA.id (attrValue pos) $
+               ""
         DTC.ToF{..} -> 
-               H.nav ! HA.class_ "tof" $ ""
+               H.nav ! HA.class_ "tof"
+                     ! HA.id (attrValue pos) $
+               ""
        {-
         Index{..} -> 
         Artwork{..} -> 
@@ -163,7 +271,12 @@ html5Horizontal = \case
         DTC.U    hs  -> H.span ! HA.class_ "underline" $ html5Horizontals hs
         DTC.Eref{..} -> H.a ! HA.class_ "eref" ! HA.href (attrValue href) $ html5Horizontals text
         DTC.Iref{..} -> H.a ! HA.class_ "iref" ! HA.href (attrValue to)   $ html5Horizontals text
-        DTC.Ref{..}  -> H.a ! HA.class_ "ref"  ! HA.href (attrValue to)   $ html5Horizontals text
+        DTC.Ref{..}  ->
+               H.a ! HA.class_ "ref"
+                   ! HA.href ("#"<>attrValue to) $
+               if null text
+               then H.toMarkup to
+               else html5Horizontals text
         DTC.Rref{..} -> H.a ! HA.class_ "rref" ! HA.href (attrValue to)   $ html5Horizontals text
         DTC.Plain t  -> H.toMarkup t
 
index fb9194d178b1d660bba88a60e465e4f337128a95..8a6403705d186182c940deffb7f38d5f784138c3 100644 (file)
@@ -157,26 +157,26 @@ xmlCommonAttrs DTC.CommonAttrs{id=ident, ..} =
 
 xmlVertical :: DTC.Vertical -> XML
 xmlVertical = \case
-        DTC.Para as hs ->
-               xmlCommonAttrs as $
-               XML.para $ xmlHorizontals hs
-        DTC.OL as vs ->
-               xmlCommonAttrs as $
-               XML.ol $ forM_ vs $ XML.li . xmlVerticals
-        DTC.UL as vs ->
-               xmlCommonAttrs as $
-               XML.ul $ forM_ vs $ XML.li . xmlVerticals
-        DTC.ToC as d ->
-               xmlCommonAttrs as $
+        DTC.Para{..} ->
+               xmlCommonAttrs attrs $
+               XML.para $ xmlHorizontals horis
+        DTC.OL{..} ->
+               xmlCommonAttrs attrs $
+               XML.ol $ forM_ items $ XML.li . xmlVerticals
+        DTC.UL{..} ->
+               xmlCommonAttrs attrs $
+               XML.ul $ forM_ items $ XML.li . xmlVerticals
+        DTC.ToC{..} ->
+               xmlCommonAttrs attrs $
                XML.toc
-                !?? mayAttr XA.depth d
-        DTC.ToF as d ->
-               xmlCommonAttrs as $
+                !?? mayAttr XA.depth depth
+        DTC.ToF{..} ->
+               xmlCommonAttrs attrs $
                XML.tof
-                !?? mayAttr XA.depth d
-        DTC.RL as rs ->
-               xmlCommonAttrs as $
-               XML.rl $ forM_ rs $ xmlReference
+                !?? mayAttr XA.depth depth
+        DTC.RL{..} ->
+               xmlCommonAttrs attrs $
+               XML.rl $ forM_ refs $ xmlReference
         -- DTC.Index -> XML.index
         DTC.Figure{..} ->
                xmlCommonAttrs attrs $
@@ -186,8 +186,8 @@ xmlVertical = \case
                        xmlVerticals verts
         DTC.Comment c ->
                XML.comment c
-        DTC.Artwork as _art ->
-               xmlCommonAttrs as $
+        DTC.Artwork{..} ->
+               xmlCommonAttrs attrs $
                XML.artwork mempty
 
 xmlHorizontals :: DTC.Horizontals -> XML
index 18f5f81bcd6005110d6f9ab4b5d3a6bb4b7a9e41..e525cfe8f5a93ce26b7352dcbf81007aa9bcf804 100644 (file)
@@ -7,17 +7,16 @@
 {-# LANGUAGE TypeFamilyDependencies #-}
 module Language.RNC.Sym where
 
-import Control.Applicative (Applicative(..), (<$>), (<$))
+import Control.Applicative (Applicative(..), (<$>))
 import Data.Foldable (Foldable,foldl',foldr)
 import Data.Function (($),(.),id,flip)
 import Data.Int (Int)
-import Data.Maybe (Maybe(..), maybe)
+import Data.Maybe (Maybe(..))
 import Data.Text (Text)
 import Text.Show (Show)
-import qualified Data.Text as Text
 
-import Language.DTC.Document (Default(..), MayText(..))
-import Language.TCT.Write.XML (XmlName(..))
+import Language.DTC.Document (Default(..))
+import Language.TCT.Write.XML (XmlName(..), XmlPos)
 import qualified Language.DTC.Document as DTC
 
 foldlApp :: (DTC.Default a, Foldable t) => t (a -> a) -> a
@@ -59,6 +58,7 @@ class
  , Sym_Rule repr
  , Sym_Interleaved repr
  ) => Sym_RNC repr where
+       position  :: repr (XmlPos -> a) -> repr a
        element   :: XmlName -> repr a -> repr a
        attribute :: XmlName -> repr a -> repr a
        comment   :: repr Text
index 78c66834c788abc937d421e2e90d382024f87a9f..4dd921a44cfe330ba0a4f5aa269a0f7e4607c6b5 100644 (file)
@@ -54,6 +54,7 @@ instance Sym_Interleaved Writer where
                Compose (Writer . unWriter <$> ws <>
                [Writer $ unWriter $ many $ Writer w])
 instance Sym_RNC Writer where
+       position (Writer w) = Writer w
        element n (Writer w) = Writer $ \rm po pp ->
                pairInfix pp po op $
                "element \""<>Text.pack (show n)<>"\" "<>w rm (op,SideR) PairBrace
index 0399cb3d9a3dd99f26e4913d85df074a1b9289aa..de599a4bbc8430fa25e254631834eac5bab127d5 100644 (file)
@@ -6,7 +6,6 @@ import Data.Eq (Eq(..))
 import Data.Function (($), (.))
 import Data.Functor (Functor)
 import Data.Maybe (Maybe(..))
-import Data.Monoid (Monoid(..))
 import Data.Ord (Ord(..))
 import Data.Semigroup (Semigroup(..))
 import Data.Sequence (Seq, ViewL(..), ViewR(..))
index 76a600ee38a4c2dde78310d22d17e0ccf6d799e8..d5f7d77cf0396d05dd66ea9bd3e82beb3f11ef68 100644 (file)
@@ -26,6 +26,7 @@ type Parser e s a =
  , P.Token s ~ Char
  , Ord e
  , IsString (P.Tokens s)
+ , P.ShowErrorComponent e
  ) => P.Parsec e s a
 
 p_satisfyMaybe :: P.MonadParsec e s m => (P.Token s -> Maybe a) -> m a
@@ -51,11 +52,7 @@ p_ColNum :: Parser e s Column
 p_ColNum = intOfPos . P.sourceColumn <$> P.getPosition
 
 -- * Debug
-pdbg :: ( Show a
-        , P.Token s ~ Char
-        , P.ShowToken (P.Token s)
-        , P.Stream s
-        ) => String -> P.ParsecT e s m a -> P.ParsecT e s m a
+pdbg :: Show a => String -> Parser e s a -> Parser e s a
 -- pdbg m p = P.dbg m p
 pdbg _m p = p
 {-# INLINE pdbg #-}
index bde0daebc89acd86dc35c4c2b1e4bc7d12f0a1aa..7ee57f2a25de37de610f7ea71efd02b4b8f1ebf1 100644 (file)
@@ -35,12 +35,6 @@ import Language.TCT.Elem
 import Language.TCT.Read.Elem
 import Language.TCT.Read.Cell
 
-{-
-import Debug.Trace (trace)
-dbg m x = trace (m <> ": " <> show x) x
-pdbg m p = P.dbg m p
--}
-
 textOf :: Buildable a => a -> Text
 textOf = TL.toStrict . Builder.toLazyText . build
 
@@ -126,7 +120,7 @@ appendLexeme lex acc =
         LexemeWhite (unCell -> "") -> acc
         LexemeWhite     cs -> appendToken  acc $ TokenPlain <$> cs
         LexemeAlphaNum  cs -> appendToken  acc $ TokenPlain . Text.pack <$> cs
-        LexemeChar       c -> appendToken  acc $ TokenPlain . Text.singleton <$> c
+        LexemeAny       cs -> appendToken  acc $ TokenPlain . Text.pack <$> cs
         LexemeToken     ts -> appendTokens acc ts
 
 -- * Type 'Lexeme'
@@ -139,7 +133,7 @@ data Lexeme
  |   LexemeLink      !(Cell Text)
  |   LexemeWhite     !(Cell White)
  |   LexemeAlphaNum  !(Cell [Char])
- |   LexemeChar      !(Cell Char)
+ |   LexemeAny       !(Cell [Char])
  |   LexemeToken     !Tokens
  deriving (Eq, Show)
 
@@ -158,19 +152,29 @@ p_Tokens = pdbg "Tokens" $
                (p_Lexeme >>= \next -> go $ mangleLexemes $ next:acc)
        
        mangleLexemes = \case
-        w@LexemeWhite{}  :p@LexemePairAny{}:acc -> w:any2close p:acc
-        p@LexemePairAny{}:w@LexemeWhite{}  :acc -> any2open p:w:acc
-        
-        l@LexemeAlphaNum{}:c@LexemeChar{}   :p@LexemePairAny{}:acc -> l:c:any2close p:acc
-        l@LexemeAlphaNum{}:p@LexemePairAny{}:c@LexemeChar{}:acc -> l:any2open p:c:acc
-        
-        acc -> acc
+        LexemeAny (Cell _bx ex x):LexemeAny (Cell by _ey y):acc -> LexemeAny (Cell by ex (x<>y)):acc
+       
+        -- "   
+        w@LexemeWhite{}:LexemePairAny p:acc -> w:LexemePairClose p:acc
+        --    "
+        LexemePairAny p:w@LexemeWhite{}:acc -> LexemePairOpen p:w:acc
        
-       any2close,any2open :: Lexeme -> Lexeme
-       any2close (LexemePairAny ps) = LexemePairClose ps
-       any2close c = c
-       any2open (LexemePairAny ps) = LexemePairOpen ps
-       any2open c = c
+        --    ,,,"
+        LexemePairAny p:a@LexemeAny{}:w@LexemeWhite{}:acc -> LexemePairOpen p:a:w:acc
+        -- ",,,   
+        w@LexemeWhite{}:a@LexemeAny{}:LexemePairAny p:acc -> w:a:LexemePairClose p:acc
+       
+        -- ",,,AAA
+        an@LexemeAlphaNum{}:a@LexemeAny{}:LexemePairAny p:acc -> an:a:LexemePairClose p:acc
+        -- ,,,"AAA
+        an@LexemeAlphaNum{}:LexemePairAny p:a@LexemeAny{}:acc -> an:LexemePairOpen p:a:acc
+       
+        -- ")
+        c@LexemePairClose{}:LexemePairAny p:acc -> c:LexemePairClose p:acc
+        -- ("
+        LexemePairAny p:o@LexemePairOpen{}:acc -> LexemePairOpen p:o:acc
+       
+        acc -> acc
 
 pairAny :: Char -> Maybe Pair
 pairAny = \case
@@ -218,7 +222,7 @@ p_Lexeme = pdbg "Lexeme" $
         , P.try $ LexemeEscape      <$> p_Cell p_Escape
         , P.try $ LexemeLink        <$> p_Cell p_Link
         , P.try $ LexemeAlphaNum    <$> p_Cell (P.some p_AlphaNum)
-        ,         LexemeChar        <$> p_Cell P.anyChar
+        ,         LexemeAny         <$> p_Cell (pure <$> P.anyChar)
         ]
 
 p_AlphaNum :: Parser e s Char
index 2e203c93ef7e98752672b4dd4431efa0b538c958..df5a6889abe398941a7b8dca13668b39f1ea9914 100644 (file)
@@ -14,6 +14,8 @@ import Data.Eq (Eq(..))
 import Data.Foldable (null, foldl', any)
 import Data.Function (($), (.), id)
 import Data.Functor (Functor(..), (<$>))
+import Data.Int (Int)
+import Data.Map.Strict (Map)
 import Data.Maybe (Maybe(..), maybe)
 import Data.Monoid (Monoid(..))
 import Data.Ord (Ord(..))
@@ -81,6 +83,14 @@ data XmlLeaf
  |   XmlText    Text
  deriving (Eq,Ord,Show)
 
+-- ** Type 'XmlPos'
+data XmlPos
+ =   XmlPos
+ {   xmlPosAncestors          :: [(XmlName,Count)]
+ ,   xmlPosPrecedingsSiblings :: Map XmlName Count
+ } deriving (Eq,Show)
+type Count = Int
+
 -- * Type 'InhXml'
 data InhXml
  =   InhXml
@@ -325,10 +335,10 @@ xmlTokens tok = goTokens tok
                                goTokens $
                                        rs |> Cell br er (TokenPlain (Text.dropAround Char.isSpace r))
                         _ -> goTokens toks
-                TokenPair PairHash (toList -> [unCell -> TokenPlain t]) ->
+                TokenPair PairHash to ->
                        Seq.singleton $
                        TreeN (cell "ref") $
-                               xmlAttrs [cell ("to",t)]
+                               xmlAttrs [cell ("to",TL.toStrict $ Write.textTokens to)]
                 TokenPair (PairElem name attrs) ts ->
                        Seq.singleton $
                        TreeN (cell $ xmlLocalName name) $