('(', 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'),
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)
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), [])