X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=src%2Flex.icl;h=4cf915ca825978e82112a79546841b6ddbc18d58;hb=eb91d7c6b2010be6a43de0a978373654ba3deacc;hp=fd4b564ede74a6a2984070a52e4a6dc82fefc97a;hpb=3d1c57710eb0b86f13df392f03131157aec22c21;p=cc1516.git diff --git a/src/lex.icl b/src/lex.icl index fd4b564..4cf915c 100644 --- a/src/lex.icl +++ b/src/lex.icl @@ -16,13 +16,13 @@ lexer r = case runParser lexProgram r of lexProgram :: Parser Char [Token] lexProgram = some lexToken <* many (satisfy isSpace) <* eof - >>= \ts->pure (map (\t->(0, 0, t)) ts) + >>= \ts->pure $ (map (\t->(0, 0, t)) ts) ++ [(0, 0, EndOfFileToken)] lexToken :: Parser Char TokenValue lexToken = //Comments - (list (fromString "//") >>| until top (item '\n') >>| lexToken) <|> - (list (fromString "/*") >>| until top (list (fromString "*/")) >>| lexToken) <|> + (list (fromString "//") >>| lexUntilNL >>| lexToken) <|> + (list (fromString "/*") >>| lexUntilCommentClose >>| lexToken) <|> //Keyword tokens (lexKw "var" VarToken) <|> (lexKw "Void" VoidToken) <|> @@ -59,10 +59,11 @@ lexToken = //Number tokens (liftM (NumberToken o toInt o toString) $ some $ satisfy isDigit) <|> //Ident tokens - (liftM IdentToken $ some $ satisfy isIdentChar) <|> + (liftM (IdentToken o toString) $ some $ satisfy isIdentChar) <|> (satisfy isSpace >>| lexToken) -// (eof >>| pure EndOfFileToken) 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 tv lexKw kw tv = lexOp kw tv <* check (not o isIdentChar) >>| pure tv