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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
{-# LANGUAGE OverloadedStrings #-}
module Test.Unit.Token (unitTests) where
import Base
import Report.Location
import Syntax.Adapt
import Syntax.Abstract (Associativity(..))
import Syntax.Interface
import Syntax.Pragma
import Syntax.Token
import Data.Text qualified as Text
import Test.Tasty
import Test.Tasty.HUnit
import Text.Megaparsec (errorBundlePretty)
unitTests :: TestTree
unitTests = testGroup "Lexer"
[ testCase "nested math inside text returns to text" nestedMathInsideText
, testCase "nested text and math returns to the enclosing frame" deeperAlternation
, testCase "braces inside text do not close the text frame" textBraceNesting
, testCase "cases is tokenized as an environment only inside math" casesOnlyInsideMath
, testCase "imports retain source locations and POSIX spellings" locatedImports
, testCase "commented environment starts are ignored"
ignoresCommentedEnvironmentStart
, testCase "empty inputs construct empty lexical syntax"
constructsEmptyLexicalSyntax
, testCase "extracts exact source fixity pragmas"
extractsSourceFixityPragmas
, testCase "rejects malformed reserved pragma lines"
rejectsMalformedPragmas
]
nestedMathInsideText :: Assertion
nestedMathInsideText = do
tokens <- tokensInProof "$\\text{if $x \\in A$ then}$"
tokens `shouldBe`
[ BeginEnv "proof"
, BeginEnv "math"
, BeginEnv "text"
, Word "if"
, BeginEnv "math"
, Variable "x"
, Command "in"
, Variable "A"
, EndEnv "math"
, Word "then"
, EndEnv "text"
, EndEnv "math"
, EndEnv "proof"
]
deeperAlternation :: Assertion
deeperAlternation = do
tokens <- tokensInProof "$\\text{a $ \\text{b $c$ d} e$ f}$"
tokens `shouldBe`
[ BeginEnv "proof"
, BeginEnv "math"
, BeginEnv "text"
, Word "a"
, BeginEnv "math"
, BeginEnv "text"
, Word "b"
, BeginEnv "math"
, Variable "c"
, EndEnv "math"
, Word "d"
, EndEnv "text"
, Variable "e"
, EndEnv "math"
, Word "f"
, EndEnv "text"
, EndEnv "math"
, EndEnv "proof"
]
textBraceNesting :: Assertion
textBraceNesting = do
tokens <- tokensInProof "$\\text{a {b}}$"
tokens `shouldBe`
[ BeginEnv "proof"
, BeginEnv "math"
, BeginEnv "text"
, Word "a"
, InvisibleBraceL
, Word "b"
, InvisibleBraceR
, EndEnv "text"
, EndEnv "math"
, EndEnv "proof"
]
casesOnlyInsideMath :: Assertion
casesOnlyInsideMath = do
tokensInsideMath <- tokensInProof "$\\begin{cases}x\\end{cases}$"
tokensInsideMath `shouldBe`
[ BeginEnv "proof"
, BeginEnv "math"
, BeginEnv "cases"
, Variable "x"
, EndEnv "cases"
, EndEnv "math"
, EndEnv "proof"
]
tokensOutsideMath <- tokensInProof "\\begin{cases}x\\end{cases}"
assertBool
"cases should not be tokenized as an environment outside math"
(BeginEnv "cases" `notElem` tokensOutsideMath && EndEnv "cases" `notElem` tokensOutsideMath)
locatedImports :: Assertion
locatedImports = do
let raw = Text.unlines
[ "% heading"
, "\\import{set/base.tex}"
, "\\import{set\\special.tex}"
, "\\begin{axiom}"
, " $x = x$."
, "\\end{axiom}"
]
case gatherImports (FileId maxBound) "import-unit" raw of
Left err ->
assertFailure (errorBundlePretty err)
Right imports@[firstImport, secondImport] -> do
assertEqual
"import paths"
["set/base.tex", "set\\special.tex"]
(unLocated <$> imports)
assertEqual "first import line" 2 (locLine (startPos firstImport))
assertEqual "second import line" 3 (locLine (startPos secondImport))
Right imports ->
assertFailure ("expected two imports, got " <> show imports)
ignoresCommentedEnvironmentStart :: Assertion
ignoresCommentedEnvironmentStart = do
let raw = Text.unlines
[ "% \\begin{signature}"
, "ordinary text"
, "\\begin{struct}"
, " an ordered set $X$ is a onesorted structure."
, "\\end{struct}"
]
(_, chunks) <-
either
(assertFailure . errorBundlePretty)
pure
(runLexer (FileId maxBound) "commented-environment" raw)
case chunks of
[Located{unLocated = BeginEnv "struct"} : _] -> pure ()
_ -> assertFailure ("expected the real structure environment, got " <> show chunks)
constructsEmptyLexicalSyntax :: Assertion
constructsEmptyLexicalSyntax =
forM_
[ ("empty", "")
, ("comment-only", "% heading\n% body")
]
\(description, raw) -> do
let input = Text.pack raw
imports <-
either
(assertFailure . errorBundlePretty)
pure
(gatherImports
(FileId maxBound)
description
input)
assertEqual (description <> " imports") [] imports
(lexedImports, chunks) <-
either
(assertFailure . errorBundlePretty)
pure
(runLexer
(FileId maxBound)
description
input)
assertEqual (description <> " lexer imports") [] lexedImports
assertEqual (description <> " chunks") [] chunks
scanned <-
either
(assertFailure . show)
pure
(concat <$> traverse scanChunk chunks)
assertEqual (description <> " scanned declarations") [] scanned
delta <-
either
(assertFailure . show)
pure
(canonicalSyntaxDelta [])
assertBool
(description <> " syntax declarations")
(null (canonicalSyntaxDeltaEntries delta))
extractsSourceFixityPragmas :: Assertion
extractsSourceFixityPragmas = do
let input =
" %! infixl 0\n"
<> "\t%! infixr 07\r\n"
<> "%! infix 3"
ordinaryComments =
Text.unlines
[ "% ! infixl 1"
, "text %! infixr 2"
, "% ordinary"
]
pragmas <-
either
(assertFailure . Text.unpack . renderSyntaxPragmaError)
pure
(extractSyntaxPragmas
(FileId maxBound)
"pragma-unit"
input)
assertEqual
"normalized pragmas"
[ (LeftAssoc, 0, 1, 3)
, (RightAssoc, 7, 2, 2)
, (NonAssoc, 3, 3, 1)
]
[ ( syntaxPragmaAssociativity pragma
, sourceMixfixLevelValue (syntaxPragmaLevel pragma)
, locLine (syntaxPragmaLocation pragma)
, locColumn (syntaxPragmaLocation pragma)
)
| pragma <- pragmas
]
assertEqual
"ordinary comments"
(Right [])
(extractSyntaxPragmas
(FileId maxBound)
"pragma-unit"
ordinaryComments)
rejectsMalformedPragmas :: Assertion
rejectsMalformedPragmas =
forM_
[ ("%!infixl 1\n", SyntaxPragmaMissingSpaceAfterPrefix)
, ("%!\n", SyntaxPragmaMissingKeyword)
, ("%! Infixl 1\n", SyntaxPragmaUnknownKeyword "Infixl")
, ("%! infixl\n", SyntaxPragmaMissingLevel)
, ("%! infixl -1\n", SyntaxPragmaInvalidLevel)
, ("%! infixl ١\n", SyntaxPragmaInvalidLevel)
, ("%! infixl 8\n", SyntaxPragmaLevelOutOfRange)
, ("%! infixl 1 extra\n", SyntaxPragmaTrailingContent)
, ("%! infixl 1\r", SyntaxPragmaLoneCarriageReturn)
]
\(input, expectedProblem) ->
case extractSyntaxPragmas
(FileId maxBound)
"pragma-unit"
input of
Left (InvalidSyntaxPragma location actualProblem) -> do
assertEqual
("problem for " <> show input)
expectedProblem
actualProblem
assertEqual "error line" 1 (locLine location)
assertEqual "error column" 1 (locColumn location)
Left err ->
assertFailure
("unexpected pragma error: "
<> Text.unpack (renderSyntaxPragmaError err))
Right pragmas ->
assertFailure
("expected malformed pragma rejection, got "
<> show pragmas)
tokensInProof :: Text -> IO [Token]
tokensInProof raw =
case runLexer (FileId maxBound) "lexer-unit" wrapped of
Left err ->
assertFailure (errorBundlePretty err)
Right (_imports, chunks) ->
pure (concatMap (map unLocated) chunks)
where
wrapped = Text.unlines
[ "\\begin{proof}"
, raw
, "\\end{proof}"
]
shouldBe :: (Eq a, Show a, HasCallStack) => a -> a -> Assertion
shouldBe = flip (assertEqual "")
|