From: Mart Lubbers Date: Fri, 20 May 2016 18:40:16 +0000 (+0200) Subject: fix escapes in literal strings X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=7559640a953e75811b865573c035d079aa1c29ad;p=cc1516.git fix escapes in literal strings --- diff --git a/examples/test.spl b/examples/test.spl index 85acc01..2a2235b 100644 --- a/examples/test.spl +++ b/examples/test.spl @@ -1,3 +1,4 @@ main(){ - var a = "hello world"; + var b = '\t'; +var a = "hello \t\"world"; } diff --git a/grammar/grammar.txt b/grammar/grammar.txt index 98bb06e..4325c92 100644 --- a/grammar/grammar.txt +++ b/grammar/grammar.txt @@ -23,6 +23,7 @@ | | '[]' | '(' ',' ')' + | '"' '"' ::= ('.' ('hd'|'tl'|'fst'|'snd))* ::= ['(' + ')'] ::= [',' ActArgs] diff --git a/lex.icl b/lex.icl index 3e711c8..a792ce3 100644 --- a/lex.icl +++ b/lex.icl @@ -2,6 +2,7 @@ implementation module lex import Control.Monad, Control.Applicative import Data.Either, Data.Func, Data.Void +import Data.Map from StdFunc import o import StdBool import StdList @@ -19,6 +20,10 @@ import AST | LexEOF | LexItemError String +escapes :: Map Char Char +escapes = fromList [('a', toChar 7), ('b', '\b'), ('f', '\f'), ('n', '\n'), + ('r', '\r'), ('t', '\t'), ('v', '\v'), ('\'', '\''), ('"', '"')] + lexer :: [Char] -> LexerOutput lexer r = fst $ runParser (lexProgram 1 1) r @@ -106,18 +111,15 @@ lexToken = lexEscape :: Parser Char LexItem lexEscape = item '\'' *> item '\\' *> top <* item '\'' - >>= \char->pure case char of - 'a' = LexToken 4 (CharToken $ toChar 7) - 'b' = LexToken 4 (CharToken '\b') - 'f' = LexToken 4 (CharToken '\f') - 'n' = LexToken 4 (CharToken '\n') - 'r' = LexToken 4 (CharToken '\r') - 't' = LexToken 4 (CharToken '\t') - 'v' = LexToken 4 (CharToken '\v') - '\'' =LexToken 4 (CharToken '\'') - c = (LexItemError $ "Unknown escape: " +++ toString c) + >>= \char->pure case get char escapes of + Just e = LexToken 4 (CharToken e) + Nothing = LexItemError $ "Unknown escape: " +++ toString char lexString :: Parser Char LexItem lexString = item '"' *> ( - many (satisfy ((<>) '"'))) <* item '"' >>= \cs-> - pure $ LexToken (length cs) (StringToken cs) + many ( + (satisfy (\c->c <> '"' && c <> '\\')) <|> + (item '\\' *> top >>= \char->case get char escapes of + Just e = pure e + Nothing = empty) + ))<* item '"' >>= \cs-> pure $ LexToken (length cs) (StringToken cs)