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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
|
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE NoImplicitPrelude #-}
-- |
-- This module defines the lexer and its associated data types.
-- The lexer takes `Text` as input and produces a stream of tokens
-- annotated with positional information. This information is bundled
-- together with the original raw input for producing error messages.
--
-- The lexer perfoms some normalizations to make describing the grammar easier.
-- Words outside of math environments are case-folded. Some commands are analysed
-- as variable tokens and are equivalent to their respective unicode variants
-- (α, β, γ, ..., 𝔸, 𝔹, ℂ, ...). Similarly, @\\begin{...}@ and @\\end{...}@ commands
-- are each parsed as single tokens.
--
module Syntax.Token
( Token(..)
, VariableDisplay(..)
, VariableSuffix(..)
, displayVariable
, renderVariableText
, tokToString
, tokToText
, TokStream(..)
, Located(..)
, runLexer
, gatherImports
) where
import Base hiding (many)
import Report.Location
import Control.DeepSeq (NFData)
import Control.Monad.Combinators
import Control.Monad.State.Strict
import Data.List.NonEmpty qualified as NonEmpty
import Data.Text qualified as Text
import Prettyprinter (Pretty(..))
import Text.Megaparsec hiding (Token, Label, label)
import Text.Megaparsec.Char qualified as Char
import Text.Megaparsec.Char.Lexer qualified as Lexer
import Tptp.UnsortedFirstOrder (isAsciiLetter, isAsciiAlphaNumOrUnderscore)
runLexer :: FileId -> String -> Text -> Either (ParseErrorBundle Text Void) ([FilePath], [[Located Token]])
runLexer fileId file raw = runParser (evalStateT document (initLexerState fileId)) file raw
type Lexer = StateT LexerState (Parsec Void Text)
data LexerState = LexerState
{ frames :: !(NonEmpty Frame)
, currentFileId :: !FileId
} deriving (Show, Eq)
data Frame
= TopText
| MathFrame !Text
| TextFrame !Int
deriving (Show, Eq)
initLexerState :: FileId -> LexerState
initLexerState fileId = LexerState (TopText :| []) fileId
topFrameOf :: LexerState -> Frame
topFrameOf LexerState{frames = frame :| _} = frame
topFrame :: Lexer Frame
topFrame = gets topFrameOf
pushFrame :: Frame -> LexerState -> LexerState
pushFrame frame st@LexerState{frames = top :| rest} =
st{frames = frame :| (top : rest)}
popFrame :: LexerState -> LexerState
popFrame st@LexerState{frames = _ :| []} = st
popFrame st@LexerState{frames = _ :| (top : rest)} =
st{frames = top :| rest}
modifyTopFrame :: (Frame -> Frame) -> LexerState -> LexerState
modifyTopFrame f st@LexerState{frames = top :| rest} =
st{frames = f top :| rest}
-- Token recognizers only emit tokens; this is the single place that changes
-- lexical context.
advance :: Token -> LexerState -> LexerState
advance tok st =
case (topFrameOf st, tok) of
(TopText, BeginEnv "math") ->
pushFrame (MathFrame "math") st
(TextFrame{}, BeginEnv "math") ->
pushFrame (MathFrame "math") st
(TopText, BeginEnv "align*") ->
pushFrame (MathFrame "align*") st
(TextFrame{}, BeginEnv "align*") ->
pushFrame (MathFrame "align*") st
(MathFrame env, EndEnv env')
| env == env' ->
popFrame st
(MathFrame{}, BeginEnv "text") ->
pushFrame (TextFrame 1) st
(TextFrame n, InvisibleBraceL) ->
modifyTopFrame (const (TextFrame (n + 1))) st
(TextFrame n, InvisibleBraceR)
| n > 1 ->
modifyTopFrame (const (TextFrame (n - 1))) st
(TextFrame 1, EndEnv "text") ->
popFrame st
_ ->
st
-- |
-- A token stream as input stream for a parser. Contains the raw input
-- before tokenization as 'Text' for showing error messages.
--
data TokStream = TokStream
{ rawInput :: !Text
, unTokStream :: ![[Located Token]]
} deriving (Show, Eq)
instance Semigroup TokStream where
TokStream raw1 toks1 <> TokStream raw2 toks2 = TokStream (raw1 <> raw2) (toks1 <> toks2)
instance Monoid TokStream where
mempty = TokStream mempty mempty
-- | A LaTeX token.
-- Invisible delimiters 'InvisibleBraceL' and 'InvisibleBraceR' are
-- unescaped braces used for grouping in TEX (@{@),
-- visibles braces are escaped braces (@\\{@).
data Token
= Word !Text
| Variable !Text
| Symbol !Text
| Integer !Int
| Command !Text
| Label Text -- ^ A /@\\label{...}@/ command (case-sensitive).
| Ref (NonEmpty Text) -- ^ A /@\\ref{...}@/ command (case-sensitive).
| BeginEnv !Text
| EndEnv !Text
| ParenL | ParenR
| BracketL | BracketR
| VisibleBraceL | VisibleBraceR
| InvisibleBraceL | InvisibleBraceR
deriving (Show, Eq, Ord, Generic, Hashable, NFData)
instance IsString Token where
fromString w = Word (Text.pack w)
data VariableDisplay = VariableDisplay
{ variableBaseText :: !Text
, variableSuffix :: !(Maybe VariableSuffix)
} deriving (Show, Eq, Ord)
data VariableSuffix
= VariableSubscript !Text
| VariableTicks !Int
deriving (Show, Eq, Ord)
displayVariable :: Text -> VariableDisplay
displayVariable rawName =
case splitVariableBase rawName of
Nothing ->
VariableDisplay rawName Nothing
Just (baseText, suffixText) ->
VariableDisplay baseText (displayVariableSuffix suffixText)
renderVariableText :: Text -> Text
renderVariableText rawName =
case displayVariable rawName of
VariableDisplay baseText Nothing ->
baseText
VariableDisplay baseText (Just (VariableTicks n)) ->
baseText <> Text.replicate n "'"
VariableDisplay baseText (Just (VariableSubscript subscriptText)) ->
baseText <> renderSubscriptText subscriptText
splitVariableBase :: Text -> Maybe (Text, Text)
splitVariableBase rawName =
matchBlackboardBase rawName <|> matchGreekBase rawName <|> matchSingleLetterBase rawName
matchBlackboardBase :: Text -> Maybe (Text, Text)
matchBlackboardBase rawName = do
suffixText <- Text.stripPrefix "bb" rawName
case Text.uncons suffixText of
Just (upper, rest)
| 'A' <= upper && upper <= 'Z' ->
Just ("bb" <> Text.singleton upper, rest)
_ ->
Nothing
matchGreekBase :: Text -> Maybe (Text, Text)
matchGreekBase rawName =
asum
[ (\suffixText -> (rendered, suffixText)) <$> Text.stripPrefix prefix rawName
| (prefix, rendered) <- greekVariables
]
matchSingleLetterBase :: Text -> Maybe (Text, Text)
matchSingleLetterBase rawName = do
(baseChar, suffixText) <- Text.uncons rawName
pure (Text.singleton baseChar, suffixText)
displayVariableSuffix :: Text -> Maybe VariableSuffix
displayVariableSuffix suffixText
| Text.null suffixText =
Nothing
| Text.all (== '_') suffixText =
Just (VariableTicks (Text.length suffixText))
| otherwise =
Just (VariableSubscript (Text.replace "_" "'" suffixText))
renderSubscriptText :: Text -> Text
renderSubscriptText subscriptText
| Text.length subscriptText == 1 =
"_" <> subscriptText
| otherwise =
"_{" <> subscriptText <> "}"
greekVariables :: [(Text, Text)]
greekVariables =
[ ("alpha", "α"), ("beta", "β"), ("gamma", "γ"), ("delta", "δ")
, ("epsilon", "ε"), ("zeta", "ζ"), ("eta", "η"), ("theta", "θ")
, ("iota", "ι"), ("kappa", "κ"), ("lambda", "λ"), ("mu", "μ")
, ("nu", "ν"), ("xi", "ξ"), ("pi", "π"), ("rho", "ρ"), ("sigma", "σ")
, ("tau", "τ"), ("upsilon", "υ"), ("phi", "φ"), ("chi", "χ")
, ("psi", "ψ"), ("omega", "ω")
, ("Gamma", "Γ"), ("Delta", "Δ"), ("Theta", "Θ"), ("Lambda", "Λ")
, ("Xi", "Ξ"), ("Pi", "Π"), ("Sigma", "Σ"), ("Upsilon", "Υ")
, ("Phi", "Φ"), ("Psi", "Ψ"), ("Omega", "Ω")
]
tokToText :: Token -> Text
tokToText = \case
Word w -> w
Variable v -> renderVariableText v
Symbol s -> s
Integer n -> Text.pack (show n)
Command cmd -> Text.cons '\\' cmd
Label m -> "\\label{" <> m <> "}"
Ref ms -> "\\ref{" <> Text.intercalate ", " (toList ms) <> "}"
BeginEnv "math" -> "$"
EndEnv "math" -> "$"
BeginEnv env -> "\\begin{" <> env <> "}"
EndEnv env -> "\\end{" <> env <> "}"
ParenL -> "("
ParenR -> ")"
BracketL -> "["
BracketR -> "]"
VisibleBraceL -> "\\{"
VisibleBraceR -> "\\}"
InvisibleBraceL -> "{"
InvisibleBraceR -> "}"
tokToString :: Token -> String
tokToString = Text.unpack . tokToText
instance Pretty Token where
pretty = \case
Word w -> pretty w
Variable v -> pretty (renderVariableText v)
Symbol s -> pretty s
Integer n -> pretty n
Command cmd -> "\\" <> pretty cmd
Label m -> "\\label{" <> pretty m <> "}"
Ref m -> "\\ref{" <> pretty m <> "}"
BeginEnv env -> "\\begin{" <> pretty env <> "}"
EndEnv env -> "\\end{" <> pretty env <> "}"
ParenL -> "("
ParenR -> ")"
BracketL -> "["
BracketR -> "]"
VisibleBraceL -> "\\{"
VisibleBraceR -> "\\}"
InvisibleBraceL -> "{"
InvisibleBraceR -> "}"
data Located a = Located
{ startPos :: !Location
, unLocated :: !a
, postWhitespace :: Whitespace
} deriving (Show, Functor)
data Whitespace = NoSpace | Space deriving (Show)
collapseWhitespace :: [Whitespace] -> Whitespace
collapseWhitespace = \case
Space : _ -> Space
NoSpace : ws -> collapseWhitespace ws
[] -> NoSpace
instance Eq a => Eq (Located a) where (==) = (==) `on` unLocated
instance Ord a => Ord (Located a) where compare = compare `on` unLocated
document :: Lexer ([FilePath], [[Located Token]])
document = do
is <- importBlock
es <- many environment
eof
return (unLocated <$> is, es)
importBlock :: Lexer [Located FilePath]
importBlock = do
void (skipManyTill skipChar importLineOrBeginEnvOrEof)
many importLine
where
-- When skipping to the import block, we first need to try parsing whitespace to properly handle comments and avoid picking up a commented import line at the start of the import block.
skipChar, importLineOrBeginEnvOrEof :: Lexer ()
skipChar = comment <|> void anySingle
importLineOrBeginEnvOrEof =
lookAhead
(void (Char.string "\\import{")
<|> void beginToplevelEnvironment)
<|> eof
importLine :: Lexer (Located FilePath) = lexeme do
Char.string "\\import{"
path <- some (satisfy isTheoryNameChar)
Char.char '}'
pure path
isTheoryNameChar :: Char -> Bool
isTheoryNameChar c =
c /= '}' && c /= '\n' && c /= '\r' && c /= '\0'
-- | Scan only the leading import block. Source-graph construction uses this
-- authority-free pass before parsing modules under their composed syntax.
gatherImports
:: FileId
-> String
-> Text
-> Either (ParseErrorBundle Text Void) [Located FilePath]
gatherImports fileId file =
runParser (evalStateT importBlock (initLexerState fileId)) file
beginToplevelEnvironment :: Lexer (Located Text)
beginToplevelEnvironment = lexeme do
Char.string "\\begin{"
env :: Text <- asum (Char.string <$> ["definition", "theorem", "lemma", "axiom", "proof", "corollary", "proposition", "claim", "abbreviation", "datatype", "inductive", "signature", "struct"])
Char.char '}'
pure env
-- | Parses tokens, switching tokenizing frames when encountering math and text environments.
environment :: Lexer [Located Token]
environment = do
env <- skipManyTill (comment <|> void anySingle) beginToplevelEnvironment
lts <- go (unLocated env) id
pure ((BeginEnv <$> env) : lts)
where
go env f = do
frame <- topFrame
r <- optional (nextTokenFor frame)
case r of
Nothing ->
pure (f [])
Just t@Located{unLocated = EndEnv env'}
| frame == TopText && env == env' ->
pure (f [t])
Just t -> do
modify' (advance (unLocated t))
go env (f . (t:))
{-# INLINE environment #-}
nextTokenFor :: Frame -> Lexer (Located Token)
nextTokenFor = \case
TopText -> normalToken
MathFrame{} -> mathToken
TextFrame n -> textToken n
-- | Parses a single normal-mode token.
normalToken :: Lexer (Located Token)
normalToken =
word <|> symbol <|> beginMath <|> beginAlign <|> subEnvironment <|> opening <|> closing <|> label <|> ref <|> end <|> command
-- | Parses a single math mode token.
mathToken :: Lexer (Located Token)
mathToken =
var <|> symbol <|> number <|> beginCases <|> endAlign <|> endCases <|> opening <|> closing <|> beginText <|> beginExplanation <|> endMath <|> command
beginText :: Lexer (Located Token)
beginText = lexeme do
Char.string "\\text{" <|> Char.string "\\textbox{"
pure (BeginEnv "text")
-- | Same as text modulo spacing, so we treat it synonymously
beginExplanation :: Lexer (Located Token)
beginExplanation = lexeme do
Char.string "\\explanation{"
pure (BeginEnv "text")
subEnvironment :: Lexer (Located Token)
subEnvironment = beginOrEnd ["enumerate", "subproof", "byCase"]
where
beginOrEnd envs = asum [beginEnv env <|> endEnv env | env <- envs]
beginEnv env = lexeme do
Char.string ("\\begin{" <> env <> "}")
pure (BeginEnv env)
endEnv env = lexeme do
Char.string ("\\end{" <> env <> "}")
pure (EndEnv env)
-- | Normal mode embedded into math mode via @\text{...}@.
textToken :: Int -> Lexer (Located Token)
textToken n = word <|> symbol <|> textEnd <|> beginMath <|> beginAlign <|> opening' <|> closing' <|> ref <|> command
where
textEnd = lexeme do
guard (n == 1)
Char.char '}'
pure (EndEnv "text")
opening' = lexeme (group <|> optional (Char.string "\\left") *> (brace <|> paren <|> bracket))
where
brace = VisibleBraceL <$ lexeme (Char.string "\\{")
group = InvisibleBraceL <$ lexeme (Char.char '{')
paren = ParenL <$ lexeme (Char.char '(')
bracket = BracketL <$ lexeme (Char.char '[')
closing' = lexeme (group <|> optional (Char.string "\\right") *> (brace <|> paren <|> bracket))
where
brace = VisibleBraceR <$ lexeme (Char.string "\\}")
group = InvisibleBraceR <$ lexeme (Char.char '}')
paren = ParenR <$ lexeme (Char.char ')')
bracket = BracketR <$ lexeme (Char.char ']')
-- | Parses a single begin math token.
beginMath :: Lexer (Located Token)
beginMath = lexeme do
Char.string "\\(" <|> Char.string "\\[" <|> Char.string "$"
pure (BeginEnv "math")
beginAlign :: Lexer (Located Token)
beginAlign = lexeme do
Char.string "\\begin{align*}"
pure (BeginEnv "align*")
beginCases :: Lexer (Located Token)
beginCases = lexeme do
Char.string "\\begin{cases}"
pure (BeginEnv "cases")
-- | Parses a single end math token.
endMath :: Lexer (Located Token)
endMath = lexeme do
Char.string "\\)" <|> Char.string "\\]" <|> Char.string "$"
pure (EndEnv "math")
endAlign :: Lexer (Located Token)
endAlign = lexeme do
Char.string "\\end{align*}"
pure (EndEnv "align*")
endCases :: Lexer (Located Token)
endCases = lexeme do
Char.string "\\end{cases}"
pure (EndEnv "cases")
-- | Parses the end of an environment.
-- Commits only after having seen "\end{".
end :: Lexer (Located Token)
end = lexeme do
notFollowedBy (Char.string "\\end{cases}")
Char.string "\\end{"
env <- some (Char.letterChar <|> Char.char '*')
Char.char '}'
pure (EndEnv (Text.pack env))
-- | Parses a word. Words are returned casefolded, since we want to ignore their case later on.
word :: Lexer (Located Token)
word = lexeme do
w <- some (Char.letterChar <|> Char.char '\'' <|> Char.char '-')
let t = Word (Text.toCaseFold (Text.pack w))
pure t
number :: Lexer (Located Token)
number = lexeme $ Integer <$> Lexer.decimal
var :: Lexer (Located Token)
var = lexeme (fmap Variable var')
where
var' = do
alphabeticPart <- letter <|> bb <|> greek
variationPart <- subscript <|> ticked <|> pure ""
pure (alphabeticPart <> variationPart)
subscript :: Lexer Text
subscript = do
Char.char '_'
unbraced <|> braced <|> text
where
unbraced = Text.singleton <$> Char.alphaNumChar
braced = Text.pack <$> (Char.char '{' *> some (Char.alphaNumChar <|> tick) <* Char.char '}')
text = Char.string "\\text" *> braced -- for rendering the subscript in roman type
-- A bit of a hack to fit the TPTP format.
tick :: Lexer Char
tick = '_' <$ Char.char '\''
ticked :: Lexer Text
ticked = do
ticks <- some tick
pure (Text.pack ticks)
letter :: Lexer Text
letter = fmap Text.singleton Char.letterChar
greek :: Lexer Text
greek = try do
Char.char '\\'
l <- symbolParser greeks
notFollowedBy Char.letterChar
pure l
greeks :: [Text]
greeks =
[ "alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta"
, "iota", "kappa", "lambda", "mu", "nu", "xi", "pi", "rho", "sigma"
, "tau", "upsilon", "phi", "chi", "psi", "omega"
, "Gamma", "Delta", "Theta", "Lambda", "Xi", "Pi", "Sigma", "Upsilon"
, "Phi", "Psi", "Omega"
]
bb :: Lexer Text
bb = do
Char.string "\\mathbb{"
l <- symbolParser bbs
Char.char '}'
pure $ "bb" <> l
bbs :: [Text]
bbs = Text.singleton <$> ['A'..'Z']
symbolParser :: [Text] -> Lexer Text
symbolParser symbols = asum (fmap Char.string symbols)
symbol :: Lexer (Located Token)
symbol = lexeme do
symb <- some (satisfy (`elem` symbols))
pure (Symbol (Text.pack symb))
where
symbols :: [Char]
symbols = ".,:;!?@=≠+-/|^><≤≥*&≈⊂⊃⊆⊇∈“”‘’"
-- | Parses a TEX-style command.
command :: Lexer (Located Token)
command = lexeme do
Char.char '\\'
cmd <- some Char.letterChar
pure (Command (Text.pack cmd))
-- | Parses a label command and extracts its marker.
label :: Lexer (Located Token)
label = lexeme do
Char.string "\\label{"
m <- marker
Char.char '}'
pure (Label m)
-- | Parses a label command and extracts its marker.
ref :: Lexer (Located Token)
ref = lexeme do
-- @\\cref@ is from @cleveref@ and @\\hyperref@ is from @hyperref@
cmd <- Char.string "\\ref{" <|> Char.string "\\cref{" <|> Char.string "\\hyperref["
ms <- NonEmpty.fromList <$> marker `sepBy1` Char.char ','
case cmd of
"\\hyperref[" -> Char.string "]{" *> some (satisfy (/= '}')) *> Char.char '}' *> pure (Ref ms)
_ -> Char.char '}' *> pure (Ref ms)
marker :: Lexer Text
marker = do
c <- satisfy isAsciiLetter
cs <- takeWhileP Nothing isAsciiAlphaNumOrUnderscore
pure (Text.cons c cs)
-- | Parses an opening delimiter.
opening :: Lexer (Located Token)
opening = lexeme (group <|> optional (Char.string "\\left") *> (paren <|> brace <|> bracket))
where
brace = VisibleBraceL <$ lexeme (Char.string "\\{")
group = InvisibleBraceL <$ lexeme (Char.char '{')
paren = ParenL <$ lexeme (Char.char '(')
bracket = BracketL <$ lexeme (Char.char '[')
-- | Parses a closing delimiter.
closing :: Lexer (Located Token)
closing = lexeme (group <|> optional (Char.string "\\right") *> (paren <|> brace <|> bracket))
where
brace = VisibleBraceR <$ lexeme (Char.string "\\}")
group = InvisibleBraceR <$ lexeme (Char.char '}')
paren = ParenR <$ lexeme (Char.char ')')
bracket = BracketR <$ lexeme (Char.char ']')
-- | Turns a Lexer into one that tracks the source position of the token
-- and consumes trailing whitespace.
lexeme :: Lexer a -> Lexer (Located a)
lexeme p = do
fileId <- gets currentFileId
start <- getSourcePos
location <-
either
(fail . show)
pure
(fromSourcePosChecked fileId start)
t <- p
w <- whitespace
pure (Located location t w)
space :: Lexer Whitespace
space = Space <$ (Char.char ' ' <|> Char.char '\n' <|> Char.char '\r')
<|> Space <$ (Char.string "\\ " <|> Char.string "\\\\" <|> Char.string "\\!" <|> Char.string "\\," <|> Char.string "\\:" <|> Char.string "\\;" <|> Char.string "\\;")
whitespace :: Lexer Whitespace
whitespace = do
ws <- many (spaces <|> NoSpace <$ comment)
pure (collapseWhitespace ws)
where
spaces = collapseWhitespace <$> some space
comment :: Lexer ()
comment = Lexer.skipLineComment "%"
|