-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPreprocess.hs
More file actions
28 lines (22 loc) · 734 Bytes
/
Preprocess.hs
File metadata and controls
28 lines (22 loc) · 734 Bytes
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
module Preprocess(preprocess) where
import Text.ParserCombinators.Parsec
lineComment = do
string "//"
manyTill anyChar (try $ (do{newline; return ()}) <|> eof)
substituteWithSpaces x = case x of
'\n' -> '\n'
'\t' -> '\t'
_ -> ' '
blockComment = do
string "/*"
xs <- manyTill anyChar (try $ string "*/")
return $ map substituteWithSpaces ("/*" ++ xs ++ "*/")
removeComment =
(try $ do{eof; return ""})
<|> (try $ do{lineComment; xs <- removeComment; return $ '\n':xs})
<|> (try $ do{xs <- blockComment; ys <- removeComment; return $ xs ++ ys})
<|> do{x <- anyChar; xs <- removeComment; return $ x:xs}
preprocess str =
let Right uncommented = runParser removeComment () "" str
in
uncommented