Skip to content
Snippets Groups Projects
Commit d9a49db9 authored by Alice Brenon's avatar Alice Brenon
Browse files

Wire the --pristine option downto the parser

parent 7cd6a081
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ import Control.Monad.Except (runExceptT) ...@@ -6,7 +6,7 @@ import Control.Monad.Except (runExceptT)
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text.IO as Text (getContents, readFile, writeFile) import qualified Data.Text.IO as Text (getContents, readFile, writeFile)
import Text.InvisiXML (InvisiXML(..), parse) import Text.InvisiXML as Parser (InvisiXML(..), ParsingConfig(..), parse)
import Text.XML.Light.Serializer (encode) import Text.XML.Light.Serializer (encode)
import System.FilePath ((<.>), dropExtension) import System.FilePath ((<.>), dropExtension)
import System.Exit (die) import System.Exit (die)
...@@ -22,8 +22,10 @@ endPoints (Command {input = FileInput f, outputPrefix}) = ...@@ -22,8 +22,10 @@ endPoints (Command {input = FileInput f, outputPrefix}) =
run :: Command -> IO () run :: Command -> IO ()
run command = do run command = do
(source, prefix) <- endPoints command (source, prefix) <- endPoints command
source >>= runExceptT . parse >>= either (fail . show) (create prefix) source >>= runExceptT . parse parsingConfig
>>= either (fail . show) (create prefix)
where where
parsingConfig = ParsingConfig {Parser.pristine = CLI.pristine command}
create prefix (InvisiXML {text, structure}) = do create prefix (InvisiXML {text, structure}) = do
Text.writeFile (prefix <.> "txt") text Text.writeFile (prefix <.> "txt") text
writeFile (prefix <.> "ixml") $ encode structure writeFile (prefix <.> "ixml") $ encode structure
......
...@@ -2,13 +2,14 @@ ...@@ -2,13 +2,14 @@
{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleContexts #-}
module Text.InvisiXML ( module Text.InvisiXML (
InvisiXML(..) InvisiXML(..)
, ParsingConfig(..)
, Structure(..) , Structure(..)
, merge , merge
, parse , parse
) where ) where
import Control.Monad.Except (MonadError(..)) import Control.Monad.Except (MonadError(..))
import Control.Monad.State (StateT(..), execStateT, gets, modify, state) import Control.Monad.RWS (RWST(..), execRWST, asks, gets, modify, state)
import Data.Char (isSpace) import Data.Char (isSpace)
import Data.List (uncons) import Data.List (uncons)
import Data.PositionTree as PositionTree ( import Data.PositionTree as PositionTree (
...@@ -80,6 +81,10 @@ data ParsingState = ParsingState { ...@@ -80,6 +81,10 @@ data ParsingState = ParsingState {
, subStructure :: Structure , subStructure :: Structure
} }
data ParsingConfig = ParsingConfig {
pristine :: Bool
}
openStream :: [Token] -> ParsingState openStream :: [Token] -> ParsingState
openStream input = ParsingState { openStream input = ParsingState {
input input
...@@ -89,7 +94,7 @@ openStream input = ParsingState { ...@@ -89,7 +94,7 @@ openStream input = ParsingState {
, subStructure = Structure empty , subStructure = Structure empty
} }
type Parser = StateT ParsingState type Parser = RWST ParsingConfig () ParsingState
pop :: Monad m => Parser m (Maybe Token) pop :: Monad m => Parser m (Maybe Token)
pop = gets (uncons . input) >>= updateState pop = gets (uncons . input) >>= updateState
...@@ -99,9 +104,9 @@ pop = gets (uncons . input) >>= updateState ...@@ -99,9 +104,9 @@ pop = gets (uncons . input) >>= updateState
state $ \parsingState -> (Just t, parsingState {input}) state $ \parsingState -> (Just t, parsingState {input})
appendText :: Monad m => String -> Parser m () appendText :: Monad m => String -> Parser m ()
appendText s = modify append appendText s = asks (toText . pristine) >>= modify . append
where where
t = Text.pack $ unindent s toText b = Text.pack $ (if b then id else unindent) s
unindent ('\n':s') = '\n':(unindent $ dropWhile isSpace s') unindent ('\n':s') = '\n':(unindent $ dropWhile isSpace s')
{- {-
case dropWhile isSpace s' of case dropWhile isSpace s' of
...@@ -110,7 +115,7 @@ appendText s = modify append ...@@ -110,7 +115,7 @@ appendText s = modify append
-} -}
unindent (c:s') = c:(unindent s') unindent (c:s') = c:(unindent s')
unindent [] = [] unindent [] = []
append parsingState append t parsingState
| Text.null t = parsingState | Text.null t = parsingState
| otherwise = parsingState { | otherwise = parsingState {
at = offset t $ at parsingState at = offset t $ at parsingState
...@@ -143,10 +148,12 @@ checkout actual = gets context >>= compareWith actual ...@@ -143,10 +148,12 @@ checkout actual = gets context >>= compareWith actual
| tag tO0 /= tag tO1 = throwError $ Mismatch {open = tO0, close = tO1} | tag tO0 /= tag tO1 = throwError $ Mismatch {open = tO0, close = tO1}
compareWith _ _ = return () compareWith _ _ = return ()
parse :: (XmlSource s, MonadError XMLError m) => s -> m InvisiXML parse :: (XmlSource s, MonadError XMLError m) =>
parse = fmap collectState . execStateT fillStructure . openStream . tokens ParsingConfig -> s -> m InvisiXML
parse config =
fmap collectState . execRWST fillStructure config . openStream . tokens
where where
collectState (ParsingState {stack, subStructure}) = InvisiXML { collectState (ParsingState {stack, subStructure}, _) = InvisiXML {
structure = subStructure structure = subStructure
, text = Text.concat $ reverse stack , text = Text.concat $ reverse stack
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment