-
Notifications
You must be signed in to change notification settings - Fork 9
/
ParseCode.hs
68 lines (55 loc) · 1.79 KB
/
ParseCode.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
module ParseCode (top, paren) where
import Text.ParserCombinators.Parsec
-- Parser for retrieving well-bracketted Haskell code
top :: GenParser Char a String -> GenParser Char a String
top stop =
do
ss <- manyTill (try $ chunk stop) (try $ lookAhead $ stop)
s <- manyTill anyChar (try $ lookAhead stop)
return $ concat (ss ++ [s])
chunk :: GenParser Char a String -> GenParser Char a String
chunk stop =
let openers = "([{\"" in
do
s <- manyTill (noneOf openers) (try $ (lookAhead (oneOf openers) >> return "") <|> (lookAhead stop))
s' <- (block <|> do {try $ lookAhead stop; return ""})
return $ s ++ s'
-- disable single quotes because they can be used in names...
block :: GenParser Char a String
block = paren <|> bracket <|> brace <|> double -- <|> single
paren :: GenParser Char a String
paren = group '(' ')'
bracket :: GenParser Char a String
bracket = group '[' ']'
brace :: GenParser Char a String
brace = group '{' '}'
group :: Char -> Char -> GenParser Char a String
group open close =
do
char open
s <- groupBody
char close
return $ [open] ++ s ++ [close]
groupBody :: GenParser Char a String
groupBody =
let boring = noneOf "()[]{}\"\\" in
do
ss <- many $ try $ do {s <- many $ boring; s' <- block; return $ s ++ s'}
s <- many boring
return $ concat $ ss ++ [s]
double :: GenParser Char a String
double = quote '"'
-- single :: GenParser Char a String
-- single = quote '\''
quote :: Char -> GenParser Char a String
quote q =
do
char q
s <- quoteBody q
char q
return $ [q] ++ s ++ [q]
quoteBody q =
do
s <- many (noneOf [q,'\\'])
(do {lookAhead (char q); return $ s} <|>
do {char '\\'; c <- anyChar; s' <- quoteBody q; return $ s ++ "\\" ++ [c] ++ s'})