import Control.Monad, Control.Applicative
import Data.Either, Data.Func, Data.Void
+import Data.Map
from StdFunc import o
import StdBool
import StdList
| 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
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)