From: Mart Lubbers Date: Fri, 19 Feb 2016 19:34:24 +0000 (+0100) Subject: negative numbers are lexed X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=f4ee031491f6d753513b03f46a602321569e1114;p=cc1516.git negative numbers are lexed --- diff --git a/lex.icl b/lex.icl index 876e302..0adb20c 100644 --- a/lex.icl +++ b/lex.icl @@ -17,9 +17,9 @@ SingleCharTokens = fromList [ ('(', BraceOpenToken), (')', BraceCloseToken), ('{', CBraceOpenToken), ('}', CBraceCloseToken), ('[', SquareOpenToken), (']', SquareCloseToken), (',', CommaToken), (':', ColonToken), (';', SColonToken), - ('.', DotToken), ('+', PlusToken), ('-', DashToken), ('*', StarToken), - ('/', SlashToken), ('%', PercentToken), ('=', AssignmentToken), - ('<', LesserToken), ('>', BiggerToken), ('!', ExclamationToken)] + ('.', DotToken), ('+', PlusToken), ('*', StarToken), ('/', SlashToken), + ('%', PercentToken), ('=', AssignmentToken), ('<', LesserToken), + ('>', BiggerToken), ('!', ExclamationToken)] EscapeMap :: Map Char Char EscapeMap = fromList [('a', toChar 7), ('b', '\b'), ('f', '\f'), ('n', '\n'), @@ -34,7 +34,8 @@ lexer x = case lex x of lex :: [Char] -> (Either String Token, [Char]) lex [] = (Right EndOfFileToken, []) lex ['/':'/':xs] = lex $ dropWhile ((<>) '\n') xs -lex ['/':'*':xs] = lex $ tl $ map snd $ dropWhile ((<>) ('*', '/')) $ zip2 [zero:xs] xs +lex ['/':'*':xs] = + lex $ tl $ map snd $ dropWhile ((<>) ('*', '/')) $ zip2 [zero:xs] xs lex ['v':'a':'r':xs] = (Right VarToken, xs) lex ['V':'o':'i':'d':xs] = (Right VoidToken, xs) lex ['r':'e':'t':'u':'r':'n':xs] = (Right ReturnToken, xs) @@ -58,12 +59,14 @@ lex ['\'':x:'\'':xs] = (Right (CharToken x), xs) lex ['\'':'\\':x:'\'':xs] = case get x EscapeMap of Just t = (Right (CharToken t), xs) _ = (Left ("Unknown escape: \\" +++ toString x), []) -lex [x:xs] -# t = get x SingleCharTokens -| isJust t = (Right (fromJust t), xs) -| isSpace x = lex xs -| isDigit x = let (is, xs`) = span isDigit xs in - (Right (NumberToken [x:is]), xs`) -| isAlpha x = let (is, xs`) = span (\c.isAlphanum c || c == '_') xs in - (Right (IdentToken [x:is]), xs`) -| otherwise = (Left ("Unexpected character: " +++ toString x), []) +lex ['-':t=:[x:xs]] +| isDigit x = let (i, r) = span isDigit t in (Right (NumberToken ['-':i]), r) +| otherwise = (Right DashToken, xs) +lex t=:[x:xs] = case get x SingleCharTokens of + (Just tok) = (Right tok, xs) + Nothing + | isSpace x = lex xs + | isDigit x = let (i, r) = span isDigit t in (Right (NumberToken i), r) + | isAlpha x = let (i, r) = span isIdent t in (Right (IdentToken i), r) + with isIdent c = isAlphanum c || c == '_' + | otherwise = (Left ("Unexpected character: " +++ toString x), [])