-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.hs
814 lines (678 loc) · 26.9 KB
/
Parser.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
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE ViewPatterns #-}
module Parser where
import Prelude hiding (replicate)
import Text.Parsec hiding (spaces, parse, many, (<|>))
import Text.Parsec.Pos (newPos)
import System.Environment
import Common hiding (tail, head)
import AST
data Expr = Expr SourcePos (AbsExpr Expr) deriving (Show)
data ParserState = ParserState {indents::[Int], debugIndent::Int}
type Parser = ParsecT String ParserState (WriterT Text Identity)
instance Render Expr where
render (Expr _ e) = render e
pretty (Expr _ e) = pretty e
instance IsExpr Expr where
unExpr (Expr _ abstr) = abstr
instance Eq Expr where
Expr _ e == Expr _ e' = e == e'
------------------------------------------------------------
------------------- High-level parsers -------------------
------------------------------------------------------------
-- | Entry point parser.
pTopLevel :: Parser Expr
pTopLevel = do
input <- getInput
debug "OK here we go"
logInput
go <* eof
where
go = logged "main" (blockOf pStatement) >>= \case
[expr] -> return expr
exprs@(e:_) -> return $ Expr (getPos e) $ Block exprs
-- | A collection of statements separated by newlines or semicolons.
pBlock :: Parser Expr
pBlock = item . logged "block" $ Block <$> indented pStatement
-- | A statement. Possibly nothing.
pStatement :: Parser Expr
pStatement = logged "statement" $ pExpr -- <|> item' EmptyExpr
pExpr :: Parser Expr
pExpr = do
expr <- pSmallExpr
option expr $ lookAhead (logged "some keyword" pAnyKeyword) >>= \case
"if" -> pEmbeddedIf expr
"unless" -> pEmbeddedUnless expr
"while" -> pEmbeddedWhile expr
"for" -> pEmbeddedFor expr
_ -> unexpected "Unknown keyword expression"
-- | An expression. If statements, unary/binary operations, etc.
pSmallExpr :: Parser Expr
pSmallExpr = choice [ logged "if" pIf
, logged "while" pWhile
, logged "for" pFor
, logged "switch" pSwitch
, logged "try/catch" pTryCatch
, logged "function" pFunction
, logged "assignment" pAssign
, logged "bare object" pBareObject
, logged "newed" pNew
, logged "return" pReturn
, logged "break" pBreak
, logged "continue" pContinue
, pBinaryOp]
-- | For when either an expression or a block a block is valid.
pExprOrBlock :: Parser Expr
pExprOrBlock = pBlock <|> pInlineBlock
-- | An inline-only block
pInlineBlock :: Parser Expr
pInlineBlock = item . logged "inline block" $ Block <$> pStatement `sepBy1` schar ';'
-- | Smallest unit (lower than function application or binary).
pTerm :: Parser Expr
pTerm = choice [ pNumber
, pVariable
, pString
, pRegex
, pParens
, pArray
, pObject ]
-- | An expression wrapped in parentheses.
pParens :: Parser Expr
pParens = enclose "()" pExpr
section p = p <* many emptyLine <* same
------------------------------------------------------------
----------------------- Primitives -----------------------
------------------------------------------------------------
-- | Parses any valid, non-keyword identifier. Doesn't store position.
pIdent :: Parser Name
pIdent = checkKeyword pAnyIdent
-- | Same as @pIdent@, but doesn't check if it's a keyword. Used for .member
-- vars.
pAnyIdent :: Parser Name
pAnyIdent = do
first <- letter <|> char '_' <|> char '@' <|> char '$'
rest <- many (letter <|> digit <|> char '_' <|> char '$')
return $ pack (first : rest)
-- | Same as @pIdent@ but skips spaces after.
pIdent' :: Parser Name
pIdent' = pIdent <* spaces
-- | Same as @pAnyIdent@ but skips spaces.
pAnyIdent' :: Parser Name
pAnyIdent' = pAnyIdent <* spaces
-- | Wraps an ident parse in a Variable and records the position.
pVariable :: Parser Expr
pVariable = item $ Variable <$> pIdent
-- | Parses a number. Must start with a digit.
pNumber :: Parser Expr
pNumber = item $ do
first <- many1 digit
option (Number $ read first) $ do
dot <- try (char '.' <* notFollowedBy (char '.'))
rest <- many1 digit
return $ Number $ read $ first <> (dot : rest)
------------------------------------------------------------
------------------- Strings & regexes --------------------
------------------------------------------------------------
-- | Interpolated strings: surrounded with ""
pInString :: Parser Expr
pInString = item $ InString <$> (char '"' *> go) where
go = do
str <- strTill (oneOf "\\\"#")
let escape c = (Plain (str `snoc` c) <>) <$> go
oneOf "\\\"#" >>= \case
'\\' -> anyChar >>= \case
'n' -> escape '\n'
'\\' -> escape '\\'
't' -> escape '\t'
'r' -> escape '\r'
'b' -> escape '\b'
c | c `elem` [' ', '\n', '\t'] -> consume
| otherwise -> escape c
where consume = anySpaces >> (Plain str <>) <$> go
'#' -> anyChar >>= \case
'{' -> do
expr <- unExpr <$> pSmallExpr <* char '}'
Interpolated (Plain str) <$> (pure expr) <*> go
c -> escape c
'"' -> return (Plain str)
c -> error $ "wtf is " <> [c]
-- | Regular strings: surrounded with ''
pRegString :: Parser Expr
pRegString = item $ String <$> (char '\'' >> go) where
go = do
str <- strTill (oneOf "\\'")
let escape c = ((str `snoc` c) <>) <$> go
oneOf "\\'" >>= \case
'\\' -> anyChar >>= \case
'n' -> escape '\n'
'\\' -> escape '\\'
't' -> escape '\t'
'r' -> escape '\r'
'b' -> escape '\b'
c | c `elem` [' ', '\n', '\t'] -> consume
| otherwise -> escape c
where consume = anySpaces >> (str <>) <$> go
'\'' -> return str
c -> error $ "wtf is " <> [c]
-- | Long strings, python-style
pLongString :: Parser Expr
pLongString = item $ String <$> blockStr "'''"
-- | Parses either a regular string, or an interpolated one
pString :: Parser Expr
pString = pLongString <|> pRegString <|> pInString
-- | Parses a regex.
pRegex :: Parser Expr
pRegex = item $ do
char '/'
lookAhead anyChar >>= \case
' ' -> unexpected "Not a regex"
c -> Regex <$> strTillConsume (char '/')
------------------------------------------------------------
-------------- Classes, functions, literals --------------
------------------------------------------------------------
-- | Parses a class definition.
pClass :: Parser Expr
pClass = item $ Class <$ pKeyword "class" <*> name <*> extends <*> decs where
name = optionMaybe (pIdent <* spaces)
extends = optionMaybe $ pKeyword "extends" *> pExpr
decs = optionMaybe (indented pClassDec) >>= \case
Nothing -> return []
Just decs -> return decs
-- | Parses a declaration in a class, e.g. a constructor or side-effect.
pClassDec :: Parser (ClassDec Expr)
pClassDec = choice [ try $ ClassDecDef <$> pIdent <* schar ':' <*> pExpr
, ClassDecExpr <$> pStatement ]
-- | Parses a function declaration.
pFunction :: Parser Expr
pFunction = item $ do
args <- optionMaybe pArgs >>= \case
Nothing -> return []
Just as -> return as
arrow <- sstring "->" <|> sstring "=>"
body <- pExprOrBlock <|> item' EmptyExpr
return $ case arrow of
"->" -> Function args body
"=>" -> FatArrowFunction args body
where pArgs = schar '(' *> pIdent `sepBy` schar ',' <* schar ')'
-- | Parses a variable declaration/assignment.
pAssign :: Parser Expr
pAssign = itry $ Assign <$> pPattern <* pExactSym "=" <*> pExpr
-- | Patterns are a restricted subset of expressions.
pPattern :: Parser Expr
pPattern = choice $ [pCallChain, pArray, objPattern]
where objPattern = item $ Object <$> pObjectWithBraces
-- | Parses an array literal. Currently only supports comma separation.
pArray :: Parser Expr
pArray = item $ do
exprs <- enclose "[]" $
anySpaces *> pSmallExpr `sepEndBy` pObjectDivider <* anySpaces
return $ Array exprs
-- | Parses an object literal.
pObject :: Parser Expr
pObject = go where
go = item $ Object <$> (withIndent <|> pObjectWithBraces)
inline = keyVal `sepBy1` schar ','
withIndent = logged "indented keyval" $ indented keyVal
-- | Parses an object using curly braces.
pObjectWithBraces :: Parser [(Name, Expr)]
pObjectWithBraces = enclose "{}" $ keyValOrJustKey `sepBy` pObjectDivider
-- | Parses a "bare object", that is, one that isn't preceeded by indents or
-- curly braces.
pBareObject :: Parser Expr
pBareObject = item $ Object <$> (kv >>= go) where
kv = logged "key/value pair" $ try keyVal
go k = option [k] $ try $ do
-- Will not consume the object divider UNLESS it parses another keyval.
fmap (k :) (pObjectDivider *> keyVal >>= go)
-- | Parses the separaters in an object: line breaks, commas, or semicolons.
pObjectDivider :: Parser ()
pObjectDivider = choice [ignore $ schar ',',
ignore $ many1 $ schar ';' <|> schar '\n']
-- | In CoffeeScript, if in a dictionary we write just a key @foo@, it's the
-- same as writing @foo: foo@.
keyValOrJustKey :: Parser (Name, Expr)
keyValOrJustKey = try keyVal <|> do
ident <- pIdent'
(,) ident <$> item' (Variable ident)
keyVal :: Parser (Name, Expr)
keyVal = (,) <$> pAnyIdent' <* schar ':' <*> logged "obj value" pExpr
---------------------------------------------------
----------------- Control flow ------------------
---------------------------------------------------
-- | If or Unless statements.
pIf :: Parser Expr
pIf = item $ k <*> cond <*> _then <*> _else where
k = choice [pKeyword "if" >> return If, pKeyword "unless" >> return Unless]
cond = logged "if condition" pExpr
_then = logged "then branch" pThen <* many same
_else = logged "else branch" pElse
-- | Used by a few structures to do inlines (if a then b; c; d)
pThen :: Parser Expr
pThen = pBlock <|> pKeyword "then" *> pInlineBlock -- pBlock -- <|>
-- | Parses an 'else'. Used by @pIf@ and @pSwitch@.
pElse :: Parser (Maybe Expr)
pElse = optionMaybe $ pKeyword "else" *> pExprOrBlock
-- | Embedded if statements. `foo = a if b`
pEmbeddedIf :: Expr -> Parser Expr
pEmbeddedIf expr = item $ EmbeddedIf expr <$ pKeyword "if" <*> pExpr
-- | Embedded unless statements. `foo = a unless b`
pEmbeddedUnless :: Expr -> Parser Expr
pEmbeddedUnless expr = item $ EmbeddedUnless expr <$ pKeyword "unless" <*> pExpr
-- | For loops, two kinds: either `in` or `of`.
pFor :: Parser Expr
pFor = item $ do
debug "trying to get keyword for"
pKeyword "for"
debug "trying to get identifiers"
names <- pIdent' `sepBy1` schar ','
(pKeyword "in" <|> pKeyword "of") >>= \case
"in" -> ForIn names <$> pSmallExpr <*> pThen
"of" -> ForOf names <$> pSmallExpr <*> pThen
-- | For comprehensions (a for b in c).
pEmbeddedFor :: Expr -> Parser Expr
pEmbeddedFor expr = item $ logged "embedded for" $ do
pKeyword "for"
names <- pIdent' `sepBy1` schar ','
(pKeyword "in" <|> pKeyword "of") >>= \case
"in" -> EmbeddedForIn expr names <$> pExpr
"of" -> EmbeddedForOf expr names <$> pExpr
-- | While loops.
pWhile :: Parser Expr
pWhile = item $ While <$ pKeyword "while"
<*> logged "while condition" pExpr
<*> logged "while body" pThen
pEmbeddedWhile :: Expr -> Parser Expr
pEmbeddedWhile expr = item $ EmbeddedWhile expr <$ pKeyword "while" <*> pExpr
-- | Parses a switch statement.
pSwitch :: Parser Expr
pSwitch = item $ do
pKeyword "switch"
cond <- pExpr
(cases, _else) <- getCases
return $ Switch cond cases _else
where sCase :: Parser ([Expr], Expr)
sCase = (,) <$ pKeyword "when" <*> pSmallExpr `sepBy1` schar ',' <*> pThen
getCases = do
indent
cases <- blockOf sCase
_else <- pElse
outdent
return (cases, _else)
-- | Parses a try/catch/finally.
pTryCatch :: Parser Expr
pTryCatch = item $ TryCatch <$ pKeyword "try" <*> toTry <*> catch <*> finally
where
toTry = pExprOrBlock
catch = optionMaybe $ do
pKeyword "catch"
name <- pIdent'
expr <- pThen <|> item' EmptyExpr
return (name, expr)
finally = optionMaybe $
pKeyword "finally" *> (pExprOrBlock <|> item' EmptyExpr)
------------------------------------------------------------
------------ Calling functions and attributes ------------
------------------------------------------------------------
-- | Function application has low precedence in CoffeeScript,
-- unless the arguments are not separated by spaces (see below).
-- So we parse a "call chain" (which is space-sensitive) first.
pCall :: Parser Expr
pCall = lexeme $ do
func <- pCallChain
debug $ "parsed 'func' " <> render func
args <- logged "function args" $ optionMaybe $ try $ do
emptyTuple <|> pSmallExpr `sepBy1` schar ','
case args of
Nothing -> return func
Just args -> return $ Expr (getPos func) $ Call func args
where emptyTuple = try $ schar '(' *> char ')' *> pure []
-- | This parser will grab a chain of function applications and dots.
-- For example, `foo.bar().baz(a, b).qux`. In CoffeeScript, there is
-- a syntactic distinction between `a (b) 1` and `a(b) 1`. The former
-- means `a(b(1))` and the latter `a(b)(1)`. Similarly, `a[b]` means
-- `a[b]`, while `a [b]` means `a([b])`.
pCallChain :: Parser Expr
pCallChain = lexeme $ pTerm >>= go where
go :: Expr -> Parser Expr
go expr = do
lookAhead anyChar >>= \case
-- If there is a parens immediately following the term,
-- it's a function call.
'(' -> do
-- Grab the arguments, then recurse.
args <- schar '(' *> pSmallExpr `sepBy` schar ',' <* char ')'
go $ Expr (getPos expr) $ Call expr args
-- If there is a square bracket, it's an object dereference.
'[' -> do
-- Grab the arguments, then recurse.
ref <- schar '[' *> pExpr
ref2 <- optionMaybe $ try $ sstring ".." *> pExpr
char ']'
case ref2 of
Nothing -> go $ Expr (getPos expr) $ ObjectDeref expr ref
Just e -> go $ Expr (getPos expr) $ ArraySlice expr ref e
-- Otherwise, we can skip spaces.
c -> spaces *> do
option expr $ try $ do
char '.' >> notFollowedBy (char '.')
member <- pAnyIdent'
go $ Expr (getPos expr) $ Dotted expr member
-- It's possible that the lookAhead will fail, if we have no input left.
-- Put this in just in case.
<|> return expr
---------------------------------------------------
------------ Keyworded expressions --------------
---------------------------------------------------
-- | A new'ed expression
pNew :: Parser Expr
pNew = item $ New <$ pKeyword "new" <*> pExpr
-- | A returned expression
pReturn :: Parser Expr
pReturn = item $ Return <$ pKeyword "return" <*> optionMaybe pExpr
-- | A break statement
pBreak :: Parser Expr
pBreak = item $ Break <$ pKeyword "break"
-- | A continue statement
pContinue :: Parser Expr
pContinue = item $ Continue <$ pKeyword "continue"
---------------------------------------------------
--------------- Binary operators ----------------
---------------------------------------------------
-- | Entry point for binary operators.
pBinaryOp :: Parser Expr
pBinaryOp = logged "binary operator" pLogical
-- | Lowest level precedence of binary operation are logical operators.
pLogical :: Parser Expr
pLogical = pLeftBinary ["&&", "||", "and", "or"] pComparative
-- | Comparisons are next higher precedence
pComparative :: Parser Expr
pComparative = pLeftBinary [ "<", ">", "<=", ">=", "=="
, "!=", "is", "isnt", "in"] pAdditive
-- | Addition, subtraction
pAdditive :: Parser Expr
pAdditive = pLeftBinary ["+", "-"] pMultiplicative
-- | Multiplication and division
pMultiplicative :: Parser Expr
pMultiplicative = pLeftBinary ["*", "//", "/"] pExponent
-- | Exponentiation
pExponent :: Parser Expr
pExponent = pLeftBinary ["**"] pCall
-- | Left-associative binary parser. Takes a list of operators, and the
-- next-higher-precedence parser to run first.
pLeftBinary :: [String] -> Parser Expr -> Parser Expr
pLeftBinary ops higher = higher >>= go where
ops' = try $ choice (map pExactSym ops) <* anySpaces
go left = spaces *> optionMaybe ops' >>= \case
Nothing -> return left
Just op -> do
right <- pLeftBinary ops higher
go $ Expr (getPos left) $ Binary op left right
---------------------------------------------------------
----------------------- Comments ----------------------
---------------------------------------------------------
pComment :: Parser Expr
pComment = pLineComment <|> pBlockComment
pLineComment :: Parser Expr
pLineComment = item $ logged "line comment" $ do
char '#'
Comment <$> strTill (char '\n')
pBlockComment :: Parser Expr
pBlockComment = item $ Comment <$> blockStr "###"
---------------------------------------------------------
----------------------- Helpers -----------------------
---------------------------------------------------------
-- | Parses spaces only. Tabs are not allowed, and newlines are meaningful.
spaces :: Parser ()
spaces = (many $ char ' ') *> return ()
-- | Parses any whitespace, for when we don't care.
anySpaces :: Parser ()
anySpaces = (many $ oneOf " \t\n\r") *> return ()
-- | Parses an item and consumes any following whitespace.
lexeme :: Parser a -> Parser a
lexeme parser = parser <* spaces
-- | Tries, and if successful then skips spaces.
ltry :: Parser a -> Parser a
ltry = lexeme . try
-- | Wrapper for an abstract expression parser. Gets the current position,
-- runs the parser, and stores the result in an `Expr`.
item :: Parser (AbsExpr Expr) -> Parser Expr
item parser = Expr <$> getPosition <*> parser
-- | Lifts the intended thing into the context and records the position.
item' :: (AbsExpr Expr) -> Parser Expr
item' = item . return
-- | Tries, and if successful records the position.
itry :: Parser (AbsExpr Expr) -> Parser Expr
itry = item . try
-- | String + following whitespace.
sstring :: String -> Parser Text
sstring s = fmap pack (lexeme $ string s)
-- | Same as @sstring@ but for chars.
schar :: Char -> Parser Char
schar = lexeme . char
-- | Wraps common @between@ usage.
enclose :: String -> Parser a -> Parser a
enclose (c1:c2:[]) = between (schar c1) (schar c2)
enclose _ = error "Argument to `enclose` should be a string of length 2"
-- | Parses a given keyword. If it fails, it consumes no input.
pKeyword :: Text -> Parser Text
pKeyword (unpack -> s) = ltry $ pack <$> string s <* notFollowedBy identChar
pAnyKeyword :: Parser Text
pAnyKeyword = choice $ map pKeyword $ toList keywords
-- | Parses the exact symbol given, or consumes nothing.
pExactSym :: String -> Parser Text
pExactSym s = ltry $ pack <$> string s <* notFollowedBy (oneOf symChars)
-- | Fails if the parsed identifier is a keyword.
checkKeyword :: Parser Text -> Parser Text
checkKeyword p = try p >>= \case
ident | ident `member` keywords -> unexpected $ "keyword " <> show ident
| otherwise -> return ident
-- | The set of keywords.
keywords :: Set Text
keywords = fromList
[ "catch", "class", "else", "extends", "false", "finally", "for"
, "if", "in", "is", "isnt", "new", "return", "switch", "then"
, "this", "true", "try", "when", "while"]
-- | Parses any character valid in an identifier.
identChar :: Parser Char
identChar = choice [letter, digit, char '_', char '$']
-- | Gets the position out of an expr.
getPos :: Expr -> SourcePos
getPos (Expr pos _) = pos
-- | Creates an expression with dummy source position, for testing.
toExpr :: AbsExpr Expr -> Expr
toExpr = Expr (newPos "" 0 0)
---------------------------------------------------------
----------------------- Indentation -------------------
---------------------------------------------------------
-- | Succeeds if indentation has increased.
indent :: Parser ()
indent = logged "an indent" $ try $ do
newline
newIndent <- length <$> many (char ' ')
debug $ "the new indent is " <> render newIndent
oldIndent:_ <- indents <$> getState
case newIndent > oldIndent of
True -> pushIndent newIndent
False -> unexpected "Not an indent"
where pushIndent i = modifyState $ \s -> s{indents = i: indents s}
-- | Succeeds if indentation has decreased. Doesn't consume input.
outdent :: Parser ()
outdent = logged "an outdent" $ try $ do
isOutdent <- (eof >> return True) <|> lookAhead (
do newline
newIndent <- length <$> many (char ' ')
oldIndent:_ <- indents <$> getState
return (newIndent < oldIndent))
case isOutdent of
True -> popIndent
False -> unexpected "Not an outdent"
where popIndent = modifyState $ \s -> s {indents = tail $ indents s}
-- | Succeeds if there is a new line with the same indentation.
nodent :: Parser ()
nodent = try $ do
newline
newIndent <- length <$> many (char ' ')
oldIndent:_ <- indents <$> getState
case newIndent == oldIndent of
True -> return ()
False -> unexpected "Not a nodent"
-- | Succeeds if there's an empty line, one with only whitespace, or a comment
emptyLine :: Parser ()
emptyLine = try $ newline *> spaces *> finish
where
finish = (ignore $ lookAhead $ char '\n') <|> ignore pLineComment
-- | Indents, outdents, nodents which also grab up preceding emptylines.
indent', outdent', nodent' :: Parser ()
indent' = try $ many emptyLine >> indent
outdent' = try $ many emptyLine >> outdent
nodent' = try $ many emptyLine >> nodent
-- | In CoffeeScript, a semicolon is (mostly) the same as same indentation.
same :: Parser ()
same = nodent' <|> ignore (schar ';')
-- | Parses its argument one or more times, separated by @same@.
blockOf :: Render a => Parser a -> Parser [a]
blockOf p = p `sepEndBy1` same
-- | Parses an indented block of @p@s.
indented :: Render a => Parser a -> Parser [a]
--indented p = between (logged "indent!" indent') (logged "outdent!" outdent') $ logged "some block" $ blockOf p
indented p = do
debug "starting an indent"
indent'
debug "got the indent"
block <- blockOf p
debug "finished getting lines"
outdent'
debug "w00t!"
return block
ignore :: Parser a -> Parser ()
ignore p = p >> return ()
perhaps :: Parser a -> Parser ()
perhaps = ignore . optionMaybe
--skip = spaces *> emptyLine' `sepEndBy` newline
-- | Grabs any character until the stopping parser. Doesn't consume @stop@.
strTill :: Parser a -> Parser Text
strTill stop = fmap pack $ anyChar `manyTill` lookAhead stop
-- | Same as @strTill@, but consumes the stopping parser.
strTillConsume :: Parser a -> Parser Text
strTillConsume stop = fmap pack $ anyChar `manyTill` try stop
-- | For block comments and long strings.
blockStr :: String -> Parser Text
blockStr start = try (string start) >> strTillConsume (string start)
----------------------------------------------------------
---------------------- Debugging ---------------------
----------------------------------------------------------
-- | Wraps a parser with some logging.
logged :: Render a => Text -> Parser a -> Parser a
logged desc p = do
debug $ "Attempting to parse '" <> desc <> "'"
logInput
ind <- debugIndent <$> getState
(optionMaybe . try . withIndent) p >>= \case
Nothing -> do
debug $ "Failed to parse '" <> desc <> "'"
unexpected $ unpack $ "Failed to parse '" <> desc <> "'"
Just a -> do
debug $ "+++++++++++++++++++++++++++++ " <> desc
<> " succeeded with `" <> render a <> "`"
logInput
return a
-- | Logs the current remaining input.
logInput :: Parser ()
logInput = do input <- getInput
debug $ "Remaining input: " <> pack (show input)
level <- head . indents <$> getState
debug $ "Indentation level: " <> render level
-- | Logs a message. Indents it according to the level.
debug :: Text -> Parser ()
debug msg = do
ind <- debugIndent <$> getState
let msg' = replicate ind " " <> msg <> "\n"
lift $ tell msg'
-- | Wraps a parser, increments/decrements the debug.
withIndent :: Parser a -> Parser a
withIndent p = do
modifyState $ \s -> s {debugIndent = debugIndent s + 1}
finally p $ modifyState $ \s -> s {debugIndent = debugIndent s - 1}
-- | Render-prints.
print' :: Render a => a -> IO ()
print' = putStrLn . unpack . pretty
-- | Guarantees that @action@ will be taken even if @parser@ fails.
finally :: Parser a -> Parser b -> Parser a
finally parser action = (try parser <* action) <|> (action *> unexpected "failure")
----------------------------------------------------------
----------------- Running the parser -------------------
----------------------------------------------------------
wrapParser :: Parser a -> Parser a
wrapParser p = do
debug "hi..."
lookAhead anyChar >>= \case
'\n' -> same
' ' -> same
'#' -> same
_ -> return ()
res <- p
logInput
logged "is this the end?" eof
return res
where
skipEmpties = logged "trying an empty" emptyLine `sepEndBy` logged "trying a newline" newline
-- | Initial state of the parser
initState :: ParserState
initState = ParserState {indents = [0], debugIndent = 0}
-- | Parses a string, retuns logs.
parseString :: String -> (Either ParseError Expr, Text)
parseString = parseWith pTopLevel
-- | Parses a string, drops logs.
parse :: String -> Either ParseError Expr
parse = fst . parseString
parse' :: String -> Either ParseError (AbsExpr Expr)
parse' s = fmap unExpr $ parse s
-- | Parses a string with a specific parser.
parseWith :: Parser a -> String -> (Either ParseError a, Text)
parseWith parser = runIdentity . runWriterT . runParserT parser initState ""
-- | Parses a file.
parseFile :: FilePath -> IO (Either ParseError Expr, Text)
parseFile = parseFileWith pTopLevel
-- | Parses a file with a specific parser.
parseFileWith :: Parser a -> FilePath -> IO (Either ParseError a, Text)
parseFileWith parser path = readFile path >>= return . parseWith parser
-- | Parses a file and prints the result.
testFile :: FilePath -> IO ()
testFile = readFile >=> testString
-- | Parses a file and prints the result.
testFileVerbose :: FilePath -> IO ()
testFileVerbose = readFile >=> testStringWithVerbose pTopLevel
-- | Parses a file and prints the result.
testFileWithVerbose p = readFile >=> testStringWithVerbose p
-- | Parses a string and prints the result.
testString :: String -> IO ()
testString = testStringWith pTopLevel
-- | Tests with a specific parser.
testStringWith :: Render a => Parser a -> String -> IO ()
testStringWith parser s = do
let parser' = wrapParser parser
s' = s ++ "\n"
case parseWith parser' s' of
(Right expr, _) -> print' expr
(Left err, _) -> error $ show err
-- | Parses a string and prints the result.
testStringVerbose :: String -> IO ()
testStringVerbose = testStringWithVerbose pTopLevel
-- | Tests with a specific parser.
testStringWithVerbose :: Render a => Parser a -> String -> IO ()
testStringWithVerbose parser s = do
let parser' = wrapParser parser
s' = "\n" ++ s ++ "\n"
let (res, logs) = parseWith parser' s'
putStrLn $ unpack logs
case res of
Right expr -> print' expr
Left err -> error $ show err
main :: IO ()
main = getArgs >>= \case
"-p":path:_ -> testFile path
opt@('-':_):_ -> error $ "Unknown option '" <> opt <> "'"
input:_ -> testString input
_ -> putStrLn "Please provide arguments!"