summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2019-11-21 12:55:57 +0100
committerTom Smeding <tom.smeding@gmail.com>2019-11-21 12:55:57 +0100
commit9b0be72fec761d37146288d62bf13a2ace14bbbf (patch)
tree3ef1c19e09fe65b9aa636c18b534b6877c8342be
parentfc7f2d08c1801ee6ab0d8b27ad70d9fe08f6760d (diff)
Proper string parsing
-rw-r--r--Parser.hs16
1 files changed, 15 insertions, 1 deletions
diff --git a/Parser.hs b/Parser.hs
index d7f3872..44e58cb 100644
--- a/Parser.hs
+++ b/Parser.hs
@@ -83,9 +83,23 @@ symbol s = try (string s) >> pWhiteComment
pString :: Parser String
pString = flip label "string" $ do
void $ char '"'
- s <- manyTill anyChar (symbol "\"")
+ s <- manyTill pStringChar (symbol "\"")
return s
+pStringChar :: Parser Char
+pStringChar =
+ (char '\\' >> ((char 'n' >> return '\n') <|>
+ (char 'r' >> return '\r') <|>
+ (char 't' >> return '\t') <|>
+ (char '0' >> return '\0') <|>
+ (char 'x' >> pHexDigit >>= \a -> pHexDigit >>= \b -> return (chr (16 * a + b))))) <|>
+ anyChar
+ where
+ pHexDigit :: Parser Int
+ pHexDigit = (subtract 48 . fromEnum <$> digit)
+ <|> ((+ (10 - 97)) . ord <$> oneOf "abcdef")
+ <|> ((+ (10 - 65)) . ord <$> oneOf "ABCDEF")
+
pWhiteComment :: Parser ()
pWhiteComment = do
pWhitespace