1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE TypeApplications #-}
3 module Language.Symantic.CLI.Test where
5 -- import Data.Monoid (Monoid(..))
6 -- import Data.Ord (Ord(..))
7 -- import Text.Show (Show(..))
8 import Control.Monad (Monad(..))
10 import Data.Either (Either(..))
11 import Data.Function (($))
12 import Data.Functor ((<$>))
13 import Data.Maybe (Maybe(..))
14 import Data.Semigroup (Semigroup(..))
15 import Data.String (String)
16 import Data.Void (Void)
17 import Text.Show (Show(..))
18 import qualified Data.Set as Set
19 import qualified Data.Text.Lazy.IO as TL
20 import qualified Language.Symantic.Document.Term as Doc
21 import qualified Language.Symantic.Document.Term.IO as DocIO
22 import qualified System.IO as IO
23 import qualified Text.Megaparsec as P
25 import Language.Symantic.CLI.Sym
26 import qualified Language.Symantic.CLI.Plain as Plain
27 import qualified Language.Symantic.CLI.Help as Help
28 import qualified Language.Symantic.CLI.Read as Read
32 { command_source_output :: [String]
33 , command_source_format :: String
34 , command_source_dump_tct :: Bool
37 { command_compile_output :: [String]
38 , command_compile_lang :: String
39 , command_compile_dump_tct :: Bool
40 , command_compile_dump_dtc :: Bool
43 { command_schema_output :: [String]
55 Plain.Plain d (Exit d) t a -> d
59 help_usage :: Plain.Doc d => Help.Help d (Exit d) t a -> d
60 help_usage = Help.help "USAGE" "COMMAND" "OPTION" Help.defReader
61 { Help.reader_command_indent = 4 }
64 help_usage :: Plain.Doc d => Help.Help d (Exit d) t a -> d
65 help_usage = Help.textHelp Help.defReader
74 , Sym_Interleaved repr
80 ) => Hdoc d repr where
81 hdoc :: repr (Exit d) ArgCommand Command
83 help_words "technical document compiler\n(disclaimer: expect hdoc's documentation to be quite… technical :)" $
84 main "hdoc" $ opts **> cmds
89 <<$? option_help (help_usage hdoc)
90 <<|?>> ((), long "version" $ exit Exit_Version)
96 help_words :: String -> repr (Exit d) t a -> repr (Exit d) t a
97 help_words d = help @d (Plain.words d)
99 option_help :: d -> ((), repr (Exit d) ArgOption ())
100 option_help d = ((), opt (OptionName 'h' "help") $ exit $ Exit_Help d)
102 command_source :: repr (Exit d) ArgCommand Command
104 help_words "Format a TCT source file." $
108 <<$? option_help (help_usage command_source)
111 <<|?>> option_dump_tct
112 command_compile :: repr (Exit d) ArgCommand Command
114 help_words "Compile a TCT source file." $
116 (\cmd os -> case cmd of
117 Command_Compile{..} -> Command_Compile{command_compile_output=command_compile_output<>os, ..}
126 <<$? option_help (help_usage command_compile)
129 <<|?>> option_dump_tct
130 <<|?>> option_dump_dtc
131 inps = many $ var "file" string
132 command_schema :: repr (Exit d) ArgCommand Command
134 help_words "Show the schema for DTC documents." $
138 <<$? option_help (help_usage command_schema)
141 end_options :: repr (Exit d) ArgOption ()
143 help_words "Force the end of options.\nUseful if <file> has the same name as an option." $
146 option_lang :: (String, repr (Exit d) ArgOption String)
150 help_words ("Use language <lang>.\nDefault <lang> is: "<>def) $
153 rule_output :: repr (Exit d) ArgOption String
157 option_output :: repr (Exit d) ArgOption String
159 help_words "Output into <file>.\nDefault <file> is: input <file> whose extension is replaced by <format> here and then." $
160 opt (OptionName 'o' "output") $
162 option_dump_tct :: (Bool, repr (Exit d) ArgOption Bool)
165 help_words "Dump TCT." $
168 option_dump_dtc :: (Bool, repr (Exit d) ArgOption Bool)
171 help_words "Dump DTC." $
174 option_format :: repr (Exit d) ArgOption String
176 help_words "Output format." $
177 opt (OptionName 'f' "format") $
178 "plain" <$$ tag "plain" <||>
179 "html5" <$$ tag "html5"
180 instance Plain.Doc d => Hdoc d (Plain.Plain d)
181 instance Plain.Doc d => Hdoc d Read.Parser
182 -- instance Plain.Doc d => Hdoc d (Help.Help d)
183 instance Plain.Doc d => Hdoc d (Help.Help d)
187 [String] -> IO.IO (Maybe (Either (Exit d) Command))
189 case Read.readArgs hdoc $ Read.Args $ Read.Arg <$> as of
190 Right a -> return $ Just $ Right a
193 P.FancyError _pos es ->
194 case Set.toList es of
195 [P.ErrorCustom (Read.ErrorRead e)] ->
196 return $ Just $ Left e
198 P.TrivialError pos e es -> do
199 IO.putStr $ P.parseErrorPretty @_ @Void $
200 P.TrivialError pos e es
203 parseIO :: [String] -> IO.IO ()
210 Just (Left err) -> do
213 DocIO.runTermIO IO.stdout $
214 Doc.withBreakable (Just 80) d <>
217 TL.putStrLn "version"