-
Notifications
You must be signed in to change notification settings - Fork 33
/
Lexer.hs
506 lines (437 loc) · 14.1 KB
/
Lexer.hs
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
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
{-| This module contains the logic for lexing Grace files.
The main reason for a separate lexing step using is because we would like
to use @Earley@ for LR parsing, but @Earley@ is not fast enough to handle
character-by-character parsing. Instead, we delegate lexing to a
lower-level parsing library that supports efficient bulk parsing
(@megaparsec@ in this case).
The main reason for not using @alex@ is because it uses a separate code
generation step, which leads to worse type errors and poor support for
interactive type-checking.
-}
module Grace.Lexer
( -- * Lexer
Token(..)
, LocatedToken(..)
, lex
, reserved
-- * Miscellaneous
, validRecordLabel
, validAlternativeLabel
-- * Errors related to parsing
, ParseError(..)
) where
import Control.Applicative (empty, (<|>))
import Control.Exception.Safe (Exception(..))
import Control.Monad.Combinators (many, manyTill, sepBy1)
import Data.HashSet (HashSet)
import Data.List.NonEmpty (NonEmpty(..))
import Data.Maybe (fromJust)
import Data.Scientific (Scientific)
import Data.Text (Text)
import Data.Void (Void)
import Grace.Location (Location(..), Offset(..))
import Prelude hiding (lex)
import Text.Megaparsec (ParseErrorBundle(..), try, (<?>))
import qualified Control.Monad as Monad
import qualified Control.Monad.Combinators as Combinators
import qualified Data.Char as Char
import qualified Data.HashSet as HashSet
import qualified Data.List as List
import qualified Data.Text as Text
import qualified Data.Text.Read as Read
import qualified Grace.Location as Location
import qualified Text.Megaparsec as Megaparsec
import qualified Text.Megaparsec.Char as Megaparsec.Char
import qualified Text.Megaparsec.Char.Lexer as Lexer
import qualified Text.Megaparsec.Error as Error
import qualified Text.URI as URI
-- | Short-hand type synonym used by lexing utilities
type Parser = Megaparsec.Parsec Void Text
space :: Parser ()
space = Lexer.space Megaparsec.Char.space1 (Lexer.skipLineComment "#") empty
symbol :: Text -> Parser Text
symbol = Lexer.symbol space
lexeme :: Parser a -> Parser a
lexeme = Lexer.lexeme space
parseToken :: Parser Token
parseToken =
Combinators.choice
[ -- `file` has to come before the lexer for `.` so that a file
-- prefix of `.` or `..` is not lexed as a field access
file
, uri
, label
, Combinators.choice
[ Or <$ symbol "||"
, And <$ symbol "&&"
, Plus <$ symbol "+"
, Times <$ symbol "*"
] <?> "operator"
, Combinators.choice
[ Exists <$ symbol "exists"
, Forall <$ symbol "forall"
, Let <$ symbol "let"
, In <$ symbol "in"
, If <$ symbol "if"
, Then <$ symbol "then"
, Else <$ symbol "else"
, Merge <$ symbol "merge"
, Type <$ symbol "Type"
, Fields <$ symbol "Fields"
, Alternatives <$ symbol "Alternatives"
] <?> "keyword"
, Combinators.choice
[ RealEqual <$ symbol "Real/equal"
, RealLessThan <$ symbol "Real/lessThan"
, RealNegate <$ symbol "Real/negate"
, RealShow <$ symbol "Real/show"
, ListDrop <$ symbol "List/drop"
, ListEqual <$ symbol "List/equal"
, ListFold <$ symbol "List/fold"
, ListHead <$ symbol "List/head"
, ListIndexed <$ symbol "List/indexed"
, ListLast <$ symbol "List/last"
, ListLength <$ symbol "List/length"
, ListMap <$ symbol "List/map"
, ListReverse <$ symbol "List/reverse"
, ListTake <$ symbol "List/take"
, IntegerAbs <$ symbol "Integer/abs"
, IntegerEven <$ symbol "Integer/even"
, IntegerNegate <$ symbol "Integer/negate"
, IntegerOdd <$ symbol "Integer/odd"
, JSONFold <$ symbol "JSON/fold"
, NaturalFold <$ symbol "Natural/fold"
, TextEqual <$ symbol "Text/equal"
, False_ <$ symbol "false"
, True_ <$ symbol "true"
, Null <$ symbol "null"
] <?> "built-in value"
, Combinators.choice
[ List <$ symbol "List"
, Optional <$ symbol "Optional"
, Real <$ symbol "Real"
, Integer <$ symbol "Integer"
, JSON <$ symbol "JSON"
, Natural <$ symbol "Natural"
, Bool <$ symbol "Bool"
, Text <$ symbol "Text"
] <?> "built-in type"
, OpenAngle <$ symbol "<"
, CloseAngle <$ symbol ">"
, OpenBrace <$ symbol "{"
, CloseBrace <$ symbol "}"
, OpenBracket <$ symbol "["
, CloseBracket <$ symbol "]"
, OpenParenthesis <$ symbol "("
, CloseParenthesis <$ symbol ")"
, Arrow <$ symbol "->"
, At <$ symbol "@"
, Bar <$ symbol "|"
, Colon <$ symbol ":"
, Comma <$ symbol ","
, Dash <$ symbol "-"
, Dot <$ symbol "."
, Equals <$ symbol "="
, Lambda <$ symbol "\\"
, number
, text
, alternative
, quotedAlternative
]
parseLocatedToken :: Parser LocatedToken
parseLocatedToken = do
start <- fmap Offset Megaparsec.getOffset
token <- parseToken
return LocatedToken{..}
parseLocatedTokens :: Parser [LocatedToken]
parseLocatedTokens = do
space
manyTill parseLocatedToken Megaparsec.eof
-- | Lex a complete expression
lex :: String
-- ^ Name of the input (used for error messages)
-> Text
-- ^ Source code
-> Either ParseError [LocatedToken]
lex name code =
case Megaparsec.parse parseLocatedTokens name code of
Left ParseErrorBundle{..} -> do
let bundleError :| _ = bundleErrors
let offset = Offset (Error.errorOffset bundleError)
Left (LexingFailed (Location{..}))
Right tokens -> do
return tokens
number :: Parser Token
number =
try parseInteger <|> parseScientific
where
parseInteger = Int
<$> lexeme Lexer.decimal
<* Megaparsec.notFollowedBy (Megaparsec.Char.char '.')
parseScientific = do
scientific <- lexeme Lexer.scientific
return (RealLiteral scientific)
file :: Parser Token
file = lexeme do
prefix <- ("../" <|> ("" <$ "./") <|> "/") <?> "path character"
let isPath c =
'\x21' == c
|| ('\x24' <= c && c <= '\x27')
|| ('\x2A' <= c && c <= '\x2B')
|| ('\x2D' <= c && c <= '\x2E')
|| ('\x30' <= c && c <= '\x3B')
|| '\x3D' == c
|| ('\x40' <= c && c <= '\x5A')
|| ('\x5E' <= c && c <= '\x7A')
|| ('\x7C' == c)
|| '\x7E' == c
let pathComponent = Megaparsec.takeWhile1P (Just "path character") isPath
suffix <- pathComponent `sepBy1` "/"
return (File (concatMap Text.unpack (prefix : List.intersperse "/" suffix)))
uri :: Parser Token
uri = (lexeme . try) do
u <- URI.parser
let schemes =
map (fromJust . URI.mkScheme) [ "https", "http", "env", "file" ]
if any (`elem` schemes) (URI.uriScheme u)
then return (URI u)
else fail "Unsupported Grace URI"
text :: Parser Token
text = lexeme do
"\""
let isText c =
('\x20' <= c && c <= '\x21')
|| ('\x23' <= c && c <= '\x5b')
|| ('\x5d' <= c && c <= '\x10FFFF')
let unescaped = Megaparsec.takeWhile1P (Just "text character") isText
let unicodeEscape = do
"\\u"
codepoint <- Combinators.count 4 Megaparsec.Char.hexDigitChar
case Read.hexadecimal (Text.pack codepoint) of
Right (n, "") -> do
return (Text.singleton (Char.chr n))
_ -> do
fail "Internal error - invalid unicode escape sequence"
let escaped =
Combinators.choice
[ "\"" <$ "\\\""
, "\\" <$ "\\\\"
, "/" <$ "\\/"
, "\b" <$ "\\b"
, "\f" <$ "\\f"
, "\n" <$ "\\n"
, "\r" <$ "\\r"
, "\t" <$ "\\t"
, unicodeEscape
] <?> "escape sequence"
texts <- many (unescaped <|> escaped)
"\""
return (TextLiteral (Text.concat texts))
isLabel0 :: Char -> Bool
isLabel0 c = Char.isLower c || c == '_'
isLabel :: Char -> Bool
isLabel c = Char.isAlphaNum c || c == '_' || c == '-' || c == '/'
-- | Returns `True` if the given record label is valid when unquoted
validRecordLabel :: Text -> Bool
validRecordLabel text_ =
case Text.uncons text_ of
Nothing ->
False
Just (h, t) ->
isLabel0 h
&& Text.all isLabel t
&& not (HashSet.member text_ reserved)
-- | Returns `True` if the given alternative label is a valid when unquoted
validAlternativeLabel :: Text -> Bool
validAlternativeLabel text_ =
case Text.uncons text_ of
Nothing ->
False
Just (h, t) ->
Char.isUpper h
&& Text.all isLabel t
&& not (HashSet.member text_ reserved)
-- | Reserved tokens, which can't be used for labels unless they are quoted
reserved :: HashSet Text
reserved =
HashSet.fromList
[ "Alternatives"
, "Bool"
, "Real"
, "Real/equal"
, "Real/lessThan"
, "Real/negate"
, "Real/show"
, "Fields"
, "Integer"
, "Integer/abs"
, "Integer/even"
, "Integer/negate"
, "Integer/odd"
, "JSON/fold"
, "List"
, "List/drop"
, "List/equal"
, "List/fold"
, "List/indexed"
, "List/last"
, "List/length"
, "List/map"
, "List/reverse"
, "List/take"
, "Natural"
, "Natural/fold"
, "Optional"
, "Text"
, "Text/equal"
, "Type"
, "else"
, "exists"
, "false"
, "forall"
, "if"
, "in"
, "let"
, "merge"
, "null"
, "then"
, "true"
]
label :: Parser Token
label = (lexeme . try) do
c0 <- Megaparsec.satisfy isLabel0 <?> "label character"
cs <- Megaparsec.takeWhileP (Just "label character") isLabel
let result = Text.cons c0 cs
Monad.guard (not (HashSet.member result reserved))
return (Label result)
alternative :: Parser Token
alternative = lexeme do
c0 <- Megaparsec.satisfy Char.isUpper <?> "alternative character"
cs <- Megaparsec.takeWhileP (Just "alternative character") isLabel
return (Alternative (Text.cons c0 cs))
quotedAlternative :: Parser Token
quotedAlternative = lexeme do
"'"
let isText c =
('\x20' <= c && c <= '\x26')
|| ('\x28' <= c && c <= '\x5b')
|| ('\x5d' <= c && c <= '\x10FFFF')
let unescaped = Megaparsec.takeWhile1P (Just "alternative character") isText
let unicodeEscape = do
"\\u"
codepoint <- Combinators.count 4 Megaparsec.Char.hexDigitChar
case Read.hexadecimal (Text.pack codepoint) of
Right (n, "") -> do
return (Text.singleton (Char.chr n))
_ -> do
fail "Internal error - invalid unicode escape sequence"
let escaped =
Combinators.choice
[ "'" <$ "\\\'"
, "\\" <$ "\\\\"
, "/" <$ "\\/"
, "\b" <$ "\\b"
, "\f" <$ "\\f"
, "\n" <$ "\\n"
, "\r" <$ "\\r"
, "\t" <$ "\\t"
, unicodeEscape
] <?> "escape sequence"
texts <- many (unescaped <|> escaped)
"'"
return (Alternative (Text.concat texts))
-- | Tokens produced by lexing
data Token
= Alternative Text
| Alternatives
| And
| Arrow
| At
| Bar
| Bool
| CloseAngle
| CloseBrace
| CloseBracket
| CloseParenthesis
| Colon
| Comma
| Dash
| Dot
| Real
| RealEqual
| RealLessThan
| RealLiteral Scientific
| RealNegate
| RealShow
| Else
| Equals
| Exists
| False_
| Fields
| File FilePath
| Forall
| If
| In
| Int Int
| Integer
| IntegerAbs
| IntegerEven
| IntegerNegate
| IntegerOdd
| JSON
| JSONFold
| Label Text
| Lambda
| Let
| List
| ListDrop
| ListEqual
| ListFold
| ListHead
| ListIndexed
| ListLast
| ListLength
| ListMap
| ListReverse
| ListTake
| Merge
| Natural
| NaturalFold
| Null
| OpenAngle
| OpenBrace
| OpenBracket
| OpenParenthesis
| Optional
| Or
| Plus
| Text
| TextEqual
| TextLiteral Text
| Then
| Times
| True_
| Type
| URI URI.URI
deriving stock (Eq, Show)
{-| A token with offset information attached, used for reporting line and
column numbers in error messages
-}
data LocatedToken = LocatedToken { token :: Token, start :: Offset }
deriving (Show)
-- | Errors related to lexing and parsing
data ParseError
= LexingFailed Location
| ParsingFailed Location
deriving (Eq, Show)
instance Exception ParseError where
displayException (LexingFailed location) = Text.unpack
(Location.renderError "Invalid input - Lexing failed" location)
displayException (ParsingFailed location) = Text.unpack
(Location.renderError "Invalid input - Parsing failed" location)