Debian Codegraph @

importers/ding2tei/src/Language/Ding/Token.hs

raw symbols SCIP
1 {-
2 - Language/Ding/Token.hs - token structures as produced by the lexer
3 -
4 - Copyright 2020-2021 Einhard Leichtfuß
5 -
6 - This file is part of ding2tei-haskell.
7 -
8 - ding2tei-haskell is free software: you can redistribute it and/or modify
9 - it under the terms of the GNU Affero General Public License as published
10 - by the Free Software Foundation, either version 3 of the License, or
11 - (at your option) any later version.
12 -
13 - ding2tei-haskell is distributed in the hope that it will be useful,
14 - but WITHOUT ANY WARRANTY; without even the implied warranty of
15 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 - GNU Affero General Public License for more details.
17 -
18 - You should have received a copy of the GNU Affero General Public License
19 - along with ding2tei-haskell. If not, see <https://www.gnu.org/licenses/>.
20 -}
21
22
23 {-|
24 - Token types, as produced by the Alex generated lexer and consumed by the
25 - happy generated parser (and the header parser).
26 -}
27 module Language.Ding.Token
28 ( Token(..)
29 , Position(..)
30 , Atom(..)
31 , tokenToString
32 , tokenToPosition
33 , tokenToLine
34 , tokenToColumn
35 ) where
36
37 import Data.NatLang.Grammar (GramLexCategory)
38 import Language.Ding.Show.Grammar (showGLC)
39
40
41
42 -- | Token, as produced by the lexer. Annotated with any directly preceding
43 -- whitespace and the position in the input.
44 data Token
45 = Token
46 String -- ^ Preceding whitespace.
47 Position
48 Atom
49 | EmptyToken -- ^ Neutral element in the monoid.
50
51
52 -- Note:
53 -- * This instance's show function is not injective. Information on position
54 -- and preceding whitespace is dropped.
55 instance Show Token where
56 show (Token _ _ atom) = show atom
57 show EmptyToken = "EmptyToken"
58
59
60 -- Note: One cannot simply use AlexPosn here, since this would introduce a
61 -- dependency cycle (in the current setup).
62
63 -- | Position of a token in the input, line and column.
64 data Position = Position
65 { positionToLine :: Int
66 , positionToColumn :: Int
67 }
68 deriving Show
69
70
71 -- TODO: Consider to add an explicit constructor for each possible separator.
72
73 -- | The essential part of a `Token'.
74 data Atom = NL
75 | LangSep -- ^ "::"
76 | Vert
77 | Semi
78 | Comma
79 | Tilde
80 | Plus
81 | Wordswitch -- ^ "<>"
82 | StrongSlash -- ^ see `doc/ding.slashes'
83 | WeakSlash -- ^ see `doc/ding.slashes'
84 | DoubleSlash
85
86 | OBrace
87 | CBrace
88 | OBracket
89 | CBracket
90 | OParen
91 | CParen
92 | OAngle
93 | CAngle
94 | OSlash -- ^ see `doc/ding.slashes'
95 | CSlash -- ^ see `doc/ding.slashes'
96
97 | SlashSpecial String
98 | Abbrev String
99 | AbbrevPlural String
100 | GramKW GramLexCategory
101 | IntPronKW String
102 | Text String
103
104 | KW_to
105
106 | HeaderLine String
107 deriving Show
108
109
110 -- | Convert a token back to the string it represents excluding potential
111 -- delimiters and dropping the annotated preceding whitespace. Uses
112 -- `atomToString'.
113 tokenToString :: Token -> String
114 tokenToString (Token _ _ atom) = atomToString atom
115 tokenToString EmptyToken = ""
116
117
118 -- | Convert an atom back to the string that it represents, excluding any
119 -- delimiters (</>).
120 -- This function is not injective, in particular the distinction of different
121 -- kinds of slashes is lost.
122 atomToString :: Atom -> String
123 atomToString NL = "\n"
124 atomToString LangSep = "::"
125 atomToString Vert = "|"
126 atomToString Semi = ";"
127 atomToString Comma = ","
128 atomToString Tilde = "~"
129 atomToString Plus = "+"
130 atomToString Wordswitch = "<>"
131 atomToString StrongSlash = "/"
132 atomToString WeakSlash = "/"
133 atomToString DoubleSlash = "//"
134
135 atomToString OBrace = "{"
136 atomToString CBrace = "}"
137 atomToString OBracket = "["
138 atomToString CBracket = "]"
139 atomToString OParen = "("
140 atomToString CParen = ")"
141 atomToString OAngle = "<"
142 atomToString CAngle = ">"
143 atomToString OSlash = "/"
144 atomToString CSlash = "/"
145
146 atomToString (SlashSpecial s) = s -- pretty: "/ " ++ s ++ " /"
147 atomToString (Abbrev s) = s -- pretty: "/ " ++ s ++ " /"
148 atomToString (AbbrevPlural s) = s -- pretty: "/" ++ s ++ "/s"
149
150 -- showGLC always gives a list of length one in this context.
151 atomToString (GramKW gram) = head $ showGLC gram
152 atomToString (IntPronKW pron) = pron
153 atomToString (Text t) = t
154
155 atomToString KW_to = "to"
156
157 atomToString (HeaderLine l) = l
158
159
160 tokenToPosition :: Token -> Position
161 tokenToPosition EmptyToken = error "Tried to get position of empty token."
162 tokenToPosition (Token _ pos _) = pos
163
164 tokenToLine :: Token -> Int
165 tokenToLine = positionToLine . tokenToPosition
166
167 tokenToColumn :: Token -> Int
168 tokenToColumn = positionToColumn . tokenToPosition
169
170
171 -- All tokens have a string representation, which, together with the preceding
172 -- whitespace, identifies their value. Two tokens may hence be combined in a
173 -- natural way, into a canonical `Text' token.
174
175 instance Semigroup Token where
176
177 -- Join two tokens by concatenating their string representations, with the
178 -- correct whitespace in between.
179 (Token ws1 pos1 atom1) <> (Token ws2 _ atom2) =
180 Token ws1 pos1 (Text $ atomToString atom1 ++ ws2 ++ atomToString atom2)
181
182 -- EmptyToken is supposed to be a neutral element. Note that this means that
183 -- `EmptyToken <> tok' retains the whitespace from `tok'. See also
184 -- todo/parsing.elimination.
185 EmptyToken <> tok = tok
186 tok <> EmptyToken = tok
187
188
189 instance Monoid Token where
190
191 -- The unit in the token monoid is the empty 'Text' with no preceding white-
192 -- space.
193 mempty = EmptyToken
194
195
196 -- vi: ts=2 sw=2 et