implementation module lex
-import Control.Monad
-import Data.Either, Data.Maybe, Data.Map, Data.Func
-import StdString
-import StdTuple
+import Control.Monad, Control.Applicative
+import Data.Either, Data.Func, Data.Maybe, Data.Functor
+from StdFunc import o
import StdBool
import StdList
import StdChar
-SingleCharTokens :: Map Char TokenValue
-SingleCharTokens = fromList [
- ('(', BraceOpenToken), (')', BraceCloseToken), ('{', CBraceOpenToken),
- ('}', CBraceCloseToken), ('[', SquareOpenToken), (']', SquareCloseToken),
- (',', CommaToken), (':', ColonToken), (';', SColonToken),
- ('.', DotToken), ('+', PlusToken), ('*', StarToken), ('/', SlashToken),
- ('%', PercentToken), ('=', AssignmentToken), ('<', LesserToken),
- ('>', BiggerToken), ('!', ExclamationToken), ('-', DashToken)]
-
-EscapeMap :: Map Char Char
-EscapeMap = fromList [('a', toChar 7), ('b', '\b'), ('f', '\f'), ('n', '\n'),
- ('r', '\r'), ('t', '\t'), ('v', '\v')]
+import yard
lexer :: [Char] -> LexerOutput
-lexer x = lexer` x 1
- where
- lexer` :: [Char] Int -> LexerOutput
- lexer` [] i = Right []
- lexer` x i = case lex x i of
- (Right t, rest, i) = lexer` rest i >>= \ts.Right [t:ts]
- (Left e, _, _) = Left e
-
-ret:: TokenValue [Char] Int -> (Either String Token, [Char], Int)
-ret tv rest i = (Right (i, tv), rest, i)
-
-err :: String Int -> (Either String Token, [Char], Int)
-err s i = (Left ("Line " +++ toString i +++ ": " +++ s), [], i)
+lexer r = case runParser lexProgram r of
+ (Right p, _) = Right p
+ (Left e, _) = Left $ toString e
-lex :: [Char] -> (Int -> (Either String Token, [Char], Int))
-lex [] = ret EndOfFileToken []
-lex ['/':'/':xs] = lex (dropWhile ((<>) '\n') xs)
-lex ['/':'*':xs] = \i.let (c, r) = span ((<>) ('*','/')) $ zip2 [' ':xs] xs in
- lex (tl $ map snd $ tl $ r) (i + length [x\\(x, _)<-c | x == '\n'])
-lex ['v':'a':'r':xs] = ret VarToken xs
-lex ['V':'o':'i':'d':xs] = ret VoidToken xs
-lex ['r':'e':'t':'u':'r':'n':xs] = ret ReturnToken xs
-lex ['i':'f':xs] = ret IfToken xs
-lex ['e':'l':'s':'e':xs] = ret ElseToken xs
-lex ['w':'h':'i':'l':'e':xs] = ret WhileToken xs
-lex ['T':'r':'u':'e':xs] = ret TrueToken xs
-lex ['F':'a':'l':'s':'e':xs] = ret FalseToken xs
-lex ['I':'n':'t':xs] = ret IntTypeToken xs
-lex ['C':'h':'a':'r':xs] = ret CharTypeToken xs
-lex ['B':'o':'o':'l':xs] = ret BoolTypeToken xs
-lex [':':':':xs] = ret DoubleColonToken xs
-lex ['!':'=':xs] = ret NotEqualToken xs
-lex ['<':'=':xs] = ret LesserEqToken xs
-lex ['>':'=':xs] = ret GreaterEqToken xs
-lex ['=':'=':xs] = ret EqualsToken xs
-lex ['&':'&':xs] = ret AmpersandsToken xs
-lex ['|':'|':xs] = ret PipesToken xs
-lex ['[':']':xs] = ret EmptyListToken xs
-lex ['-':'>':xs] = ret ArrowToken xs
-lex ['\'':x:'\'':xs] = ret (CharToken x) xs
-lex ['\'':'\\':x:'\'':xs] = case get x EscapeMap of
- Just t = ret (CharToken t) xs
- _ = err ("Unknown escape: \\" +++ toString x)
-lex t=:[x:xs] = case get x SingleCharTokens of
- (Just tok) = ret tok xs
- Nothing
- | x == '\n' = \i.lex xs (i+1)
- | isSpace x = lex xs
- | isDigit x = let (v, r) = span isDigit t in
- ret (NumberToken (toInt $ toString v)) r
- | isAlpha x = let (v, r) = span isIdent t in ret (IdentToken v) r
- with isIdent c = isAlphanum c || c == '_'
- | otherwise = err ("Unexpected character: " +++ toString x)
+lexProgram :: Parser Char [Token]
+lexProgram = catMaybes <$> some lexToken <* eof
+ >>= \ts->pure $ (map (\t->(0, 0, t)) ts)
-
-derive gEq TokenValue
-
-(===) :: TokenValue Token -> Bool
-(===) (IdentToken _) (_, IdentToken _) = True
-(===) (NumberToken _) (_, NumberToken _) = True
-(===) (CharToken _) (_, CharToken _) = True
-(===) x (_, y) = gEq {|*|} x y
+lexToken :: Parser Char (Maybe TokenValue)
+lexToken =
+ //Comments
+ (list (fromString "//") >>| lexUntilNL >>| pure Nothing) <|>
+ (list (fromString "/*") >>| lexUntilCommentClose >>| pure Nothing) <|>
+ //Keyword tokens
+ lexKw "var" VarToken <|>
+ lexKw "Void" VoidToken <|>
+ lexKw "return" ReturnToken <|>
+ lexKw "if" IfToken <|>
+ lexKw "else" ElseToken <|>
+ lexKw "while" WhileToken <|>
+ lexKw "True" TrueToken <|>
+ lexKw "False" FalseToken <|>
+ lexKw "Int" IntTypeToken <|>
+ lexKw "Bool" BoolTypeToken <|>
+ lexKw "Char" CharTypeToken <|>
+ //Escape chars tokens
+ liftM (Just o CharToken)
+ (list (fromString "'\\") *> lexEscape <* item '\'') <|>
+ //Normal chars tokens
+ liftM (Just o CharToken)
+ (item '\'' *> satisfy ((<>) '\'') <* item '\'') <|>
+ //Two char ops tokens
+ lexOp "::" DoubleColonToken <|> lexOp "!=" NotEqualToken <|>
+ lexOp "<=" LesserEqToken <|> lexOp ">=" GreaterEqToken <|>
+ lexOp "==" EqualsToken <|> lexOp "&&" AmpersandsToken <|>
+ lexOp "||" PipesToken <|> lexOp "[]" EmptyListToken <|>
+ lexOp "->" ArrowToken <|>
+ //One char ops tokens
+ lexOp "(" BraceOpenToken <|>
+ lexOp ")" BraceCloseToken <|> lexOp "{" CBraceOpenToken <|>
+ lexOp "}" CBraceCloseToken <|> lexOp "[" SquareOpenToken <|>
+ lexOp "]" SquareCloseToken <|> lexOp "," CommaToken <|>
+ lexOp ":" ColonToken <|> lexOp ";" SColonToken <|>
+ lexOp "." DotToken <|> lexOp "+" PlusToken <|>
+ lexOp "*" StarToken <|> lexOp "/" SlashToken <|>
+ lexOp "%" PercentToken <|> lexOp "=" AssignmentToken <|>
+ lexOp "<" LesserToken <|> lexOp ">" BiggerToken <|>
+ lexOp "!" ExclamationToken <|> lexOp "-" DashToken <|>
+ //Number tokens
+ liftM (Just o NumberToken o toInt o toString) (some $ satisfy isDigit) <|>
+ //Ident tokens
+ liftM (Just o IdentToken o toString) (some $ satisfy isIdentChar) <|>
+ (satisfy isSpace >>| pure Nothing)
+ where
+ lexUntilNL = top until (eof <|> (item '\n' >>| pure Void))
+ lexUntilCommentClose = top until (list (fromString "*/"))
+ isIdentChar c = isAlphanum c || c == '_'
+ lexOp s tv = list (fromString s) >>| pure (Just tv)
+ lexKw kw tv = lexOp kw tv <* check (not o isIdentChar)
+ lexEscape = fromJust <$> ((
+ lexOp "a" (toChar 7) <|> lexOp "b" '\b' <|> lexOp "f" '\f' <|>
+ lexOp "n" '\n' <|> lexOp "r" '\t' <|> lexOp "v" '\v' <|>
+ lexOp "'" '\'') <?> LexError "Unknown escape")