1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
%-------------------------------= --------------------------------------------
\subsection{Pseudo-\TeX\ Commands}
%-------------------------------= --------------------------------------------
%if codeOnly || showModuleHeader
> module TeXCommands ( module TeXCommands )
> where
>
> import Data.Maybe
> import FiniteMap ( FiniteMap )
> import qualified FiniteMap as FM
> import Auxiliaries
%endif
These don't really belong into a module named TeXCommands:
> data Style = Version | Help | SearchPath | Copying | Warranty | CodeOnly | NewCode | Verb | Typewriter | Poly | Math | Pre
> deriving (Eq, Show, Enum, Bounded)
> data Lang = Haskell | Agda
> deriving (Eq, Show, Enum, Bounded)
\Todo{Better name for |Class|.}
> data Class = One Char -- ordinary text
> | Many String -- ditto
> | Inline String -- @|..|@
> | Command Command String -- @\cmd{arg}@
> | Environment Environment String -- @\begin{cmd}..arg\end{cmd}@
> | Directive Directive String -- @%cmd arg@
> | Error Exc -- parsing error
> deriving (Show)
> data Command = Hs | Eval | Perform | Vrb Bool
> deriving (Eq, Show)
>
> data Environment = Haskell_ | Code | Spec | Evaluate | Hide | Ignore | Verbatim Bool
> deriving (Eq, Show)
\NB |Hs|, |Haskell_|, |Hide|, and |Ignore| are obsolete.
ks, 16.08.2004: added EOF.
>
> data Directive = Format | Include | Let | File | Options
> | Align | Separation | Latency | Begin | End | Subst
> | If | Elif | Else | Endif | EOF
> deriving (Eq, Show)
> data Numbered a = No !LineNo a
> deriving (Show)
>
\NB The |Show| instances have been defined for debugging purposes, the
|Eq| instances are necessary for |decode|.
> conditional :: Directive -> Bool
> conditional If = True
> conditional Elif = True
> conditional Else = True
> conditional Endif = True
> conditional EOF = True
> conditional _ = False
Encoding and decoding of commands, environments, and directives.
\Todo{Better name for |Representation|.}
> class Representation a where
> representation :: [(String, a)]
> instance Representation Style where
> representation = [ ("tt", Typewriter), ("math", Math), ("poly", Poly),
> ("verb", Verb), ("code", CodeOnly), ("newcode",NewCode),
> ("pre", Pre), ("version", Version),
> ("copying", Copying), ("warranty", Warranty), ("help", Help), ("searchpath", SearchPath) ]
> instance Representation Lang where
> representation = [ ("haskell", Haskell), ("agda", Agda) ]
> instance Representation Command where
> representation = [ ("hs", Hs), ("eval", Eval),
> ("perform", Perform), ("verb*", Vrb True),
> ("verb", Vrb False) ]
> instance Representation Environment where
> representation = [ ("haskell", Haskell_), ("code", Code),
> ("spec", Spec), ("evaluate", Evaluate), ("hide", Hide),
> ("ignore", Ignore), ("verbatim*", Verbatim True),
> ("verbatim", Verbatim False) ]
> instance Representation Directive where
> representation = [ ("format", Format), ("include", Include),
> ("if", If), ("elif", Elif),
> ("else", Else), ("endif", Endif),
> ("let", Let), ("file", File),
> ("options", Options), ("align", Align),
> ("separation", Separation), ("latency", Latency),
> ("{", Begin), ("}", End), ("subst", Subst),
> ("EOF",EOF) ]
>
> encode :: (Representation a) => String -> Maybe a
> encode s = FM.lookup s (FM.fromList representation)
>
> decode :: (Eq a, Representation a) => a -> String
> decode a = fromJust (lookup a (inverse representation))
\NB We cannot use arrays for |decode|, because |Command| is neither an
enumerated nor a product type (|Vrb Bool|).
|