X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=src%2Flex.icl;h=9a414cbdcc953575434fb4e1b8985b20e5e788f3;hb=fa2b27e75142781978149dc3e906e8b5aae8e603;hp=ac5168f4d36ffcebd808061c60e8019996c07bb7;hpb=65f6bf827fa2f4d2f79f9e3bda990b7e4c4df5f1;p=cc1516.git diff --git a/src/lex.icl b/src/lex.icl index ac5168f..9a414cb 100644 --- a/src/lex.icl +++ b/src/lex.icl @@ -6,70 +6,115 @@ from StdFunc import o import StdBool import StdList import StdChar +import StdString + +from Text import class Text(textSize,concat), instance Text String import yard lexer :: [Char] -> LexerOutput -lexer r = case runParser lexProgram r of +lexer r = case runParser (lexProgram 1 1) r of (Right p, _) = Right p - (Left e, _) = Left $ toString e + (Left e, _) = Left e + +lexProgram :: Int Int -> Parser Char [Token] +lexProgram line column = lexToken >>= \t->case t of + LexEOF = pure [] + (LexItemError e) = fail + PositionalError line column ("LexerError: " +++ e) + (LexToken c t) = lexProgram line (column+c) + >>= \rest->pure [{line=line, column=column, token=t}:rest] + LexNL = lexProgram (line+1) 1 + (LexSpace l c) = lexProgram (line+l) (column+c) -lexProgram :: Parser Char [Token] -lexProgram = catMaybes <$> some lexToken <* eof - >>= \ts->pure $ (map (\t->(0, 0, t)) ts) +:: LexItem + = LexToken Int TokenValue + | LexSpace Int Int + | LexNL + | LexEOF + | LexItemError String -lexToken :: Parser Char (Maybe TokenValue) +lexToken :: Parser Char LexItem lexToken = - //Comments - (list (fromString "//") >>| lexUntilNL >>| pure Nothing) <|> - (list (fromString "/*") >>| lexUntilCommentClose >>| pure Nothing) <|> + //Comments + lexBlockComment <|> lexComment <|> //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 "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 '\'') <|> + //Character tokens + lexEscape <|> lexCharacter <|> //Two char ops tokens - lexOp "::" DoubleColonToken <|> lexOp "!=" NotEqualToken <|> - lexOp "<=" LesserEqToken <|> lexOp ">=" GreaterEqToken <|> - lexOp "==" EqualsToken <|> lexOp "&&" AmpersandsToken <|> - lexOp "||" PipesToken <|> lexOp "[]" EmptyListToken <|> - lexOp "->" ArrowToken <|> + lexWord "::" DoubleColonToken <|> lexWord "!=" NotEqualToken <|> + lexWord "<=" LesserEqToken <|> lexWord ">=" GreaterEqToken <|> + lexWord "==" EqualsToken <|> lexWord "&&" AmpersandsToken <|> + lexWord "||" PipesToken <|> lexWord "[]" EmptyListToken <|> + lexWord "->" 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) + lexWord "(" BraceOpenToken <|> + lexWord ")" BraceCloseToken <|> lexWord "{" CBraceOpenToken <|> + lexWord "}" CBraceCloseToken <|> lexWord "[" SquareOpenToken <|> + lexWord "]" SquareCloseToken <|> lexWord "," CommaToken <|> + lexWord ":" ColonToken <|> lexWord ";" SColonToken <|> + lexWord "." DotToken <|> lexWord "+" PlusToken <|> + lexWord "*" StarToken <|> lexWord "/" SlashToken <|> + lexWord "%" PercentToken <|> lexWord "=" AssignmentToken <|> + lexWord "<" LesserToken <|> lexWord ">" BiggerToken <|> + lexWord "!" ExclamationToken <|> lexWord "-" DashToken <|> + //Number and identifier tokens + lexNumber <|> lexIdentifier <|> + (item '\n' >>| pure LexNL) <|> + (satisfy isSpace >>| (pure $ LexSpace 0 1)) <|> + (eof >>| pure LexEOF) where + lexWord :: String TokenValue -> Parser Char LexItem + lexWord s tv = list (fromString s) >>| pure (LexToken (textSize s) tv) + + lexKw :: String TokenValue -> Parser Char LexItem + lexKw kw tv = lexWord kw tv <* check (not o isIdentChar) + lexUntilNL = top until (eof <|> (item '\n' >>| pure Void)) - lexUntilCommentClose = top until (list (fromString "*/")) + + lexComment :: Parser Char LexItem + lexComment = list (fromString "//") >>| lexUntilNL + >>= \chars->pure LexNL + + lexBlockComment :: Parser Char LexItem + lexBlockComment = list (fromString "/*") + >>| (top until (list (fromString "*/"))) + >>= \chars->pure $ widthHeight chars 0 0 + where + widthHeight :: [Char] Int Int -> LexItem + widthHeight [] l c = LexSpace l c + widthHeight ['\n':xs] l _ = widthHeight xs (l+1) 0 + widthHeight [x:xs] l c = widthHeight xs l (c+1) + + lexNumber :: Parser Char LexItem + lexNumber = toString <$> some (satisfy isDigit) + >>= \si->pure $ LexToken (textSize si) (NumberToken $ toInt si) + + lexIdentifier :: Parser Char LexItem + lexIdentifier = toString <$> some (satisfy isIdentChar) + >>= \si->pure $ LexToken (textSize si) (IdentToken si) + 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 "'" '\'') ("Unknown escape", 0)) + + lexCharacter :: Parser Char LexItem + lexCharacter = item '\'' *> satisfy ((<>) '\'') <* item '\'' + >>= \char->pure $ LexToken 3 (CharToken char) + + 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') + 'b' = LexToken 4 (CharToken '\b') + 'f' = LexToken 4 (CharToken '\f') + 'n' = LexToken 4 (CharToken '\n') + 'r' = LexToken 4 (CharToken '\t') + 'v' = LexToken 4 (CharToken '\v') + '\'' =LexToken 4 (CharToken '\'') + c = (LexItemError $ "Unknown escape: " +++ toString c)