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
-//Of hier een record van maken
-//lexToken :: Parser Char (Maybe (Int, Int, Int, TokenValue))
-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 "'" '\'') <?> LexError "Unknown escape")
+
+ 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)
import lex
parser :: LexerOutput -> ParserOutput
-parser (Left e) = Left $ toString $ LexError e
-parser (Right r) = case runParser parseProgram r of
- (Right p, []) = Right p
- (Right p, x) = Left "Unparsed tokens: "//TODO
- (Left e, _) = Left $ toString e
+parser (Left e) = Left e
+parser (Right r) = fst $ runParser parseProgram r
parseProgram :: Parser Token AST
parseProgram = liftM2 AST (many parseVarDecl) (some parseFunDecl)
<* satTok BraceCloseToken
trans2 :: TokenValue (TokenValue -> a) -> Parser Token a
-trans2 t f = liftM (f o thd3) $ satTok t
+trans2 t f = liftM (\{token}->f token) $ satTok t
trans1 :: TokenValue a -> Parser Token a
trans1 t r = trans2 t $ const r
derive gPrint TokenValue
derive gEq TokenValue
satTok :: TokenValue -> Parser Token Token
-satTok t = top >>= \tok=:(pos1, pos2, tv) -> if (eq t tok)
- (return tok)
- (fail <?> Unexpected (printToString tv) (pos1, pos2))
+satTok t = top >>= \tok=:{line,column,token} -> if (eq t token)
+ (return tok) (fail <?> PositionalError line column ("ParseError: Unexpected token: " +++ printToString token))
where
- eq (IdentToken _) (_, _, IdentToken _) = True
- eq (NumberToken _) (_, _, NumberToken _) = True
- eq (CharToken _) (_, _, CharToken _) = True
- eq x (_, _, y) = gEq {|*|} x y
+ eq (IdentToken _) (IdentToken _) = True
+ eq (NumberToken _) (NumberToken _) = True
+ eq (CharToken _) (CharToken _) = True
+ eq x y = gEq {|*|} x y
parseSepList :: TokenValue (Parser Token a) -> Parser Token [a]
parseSepList sep p =
import Data.Void
instance toString Error where
- toString ParseError = "General parse error"
- toString (LexError e) = "Lexer error: " +++ e
- toString (Unexpected e (l,c)) = "Unexpected " +++ e +++ " at "
- +++ (toString l) +++ ":" +++ (toString c)
+ toString (PositionalError l c e) =
+ concat [toString l,":",toString c,": ",e, "\n"]
+ toString (Error e) = concat ["-:-: ", e, "\n"]
runParser :: (Parser a b) [a] -> (Either Error b, [a])
runParser (Parser f) i = f i
instance + Error where
- (+) ParseError r = r
- (+) r ParseError = r
- (+) r _ = r
+ (+) r1 r2 = r2 //TODO
instance Functor (Parser a) where
fmap f m = liftM f m
(Left e, _) = (Left e, i)
instance Alternative (Parser a) where
- empty = Parser \i -> (Left ParseError, i)
+ empty = Parser \i -> (Left $ Error "" , i)
(<|>) p1 p2 = Parser \i -> case runParser p1 i of
(Right r, rest) = (Right r, rest)
(Left e1, rest) = case runParser p2 i of
top :: Parser a a
top = Parser \i -> case i of
- [] = (Left ParseError, [])
+ [] = (Left $ Error "", [])
[x:xs] = (Right x, xs)
peek :: Parser a a
peek = Parser \i -> case i of
- [] = (Left ParseError, [])
+ [] = (Left $ Error "", [])
[x:xs] = (Right x, [x:xs])
//runs the left parser until the right parser succeeds. Returns the result of the left parser
eof :: Parser a Void
eof = Parser \i -> case i of
[] = (Right Void, [])
- _ = (Left ParseError, i)
+ _ = (Left $ Error "", i)
satisfy :: (a -> Bool) -> Parser a a
satisfy f = top >>= \r -> if (f r) (return r) fail