X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=src%2Fparse.icl;h=acd7d9c5941ada0eb377ad866065ca65d57b93e7;hb=fa2b27e75142781978149dc3e906e8b5aae8e603;hp=edc23ac51c1b35e0f0470935d8671c684e1d48e4;hpb=da0449d1b0998b76941699ace4a806022b52523d;p=cc1516.git diff --git a/src/parse.icl b/src/parse.icl index edc23ac..acd7d9c 100644 --- a/src/parse.icl +++ b/src/parse.icl @@ -18,19 +18,17 @@ import yard import lex parser :: LexerOutput -> ParserOutput -parser (Left e) = Left $ toString $ LexError e -parser (Right r) = case runParser parseProgram r of - (Right p, _) = Right p - (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) +parseProgram = liftM2 AST (many parseVarDecl) (some parseFunDecl) parseFunDecl :: Parser Token FunDecl parseFunDecl = liftM5 FunDecl (parseIdent <* satTok BraceOpenToken) (parseSepList CommaToken parseIdent <* satTok BraceCloseToken) - (parseFunType <* satTok CBraceOpenToken) + (optional parseFunType <* satTok CBraceOpenToken) (many parseVarDecl) (many parseStmt <* satTok CBraceCloseToken) @@ -70,7 +68,7 @@ parseStmt = parseIfStmt <|> parseWhileStmt <|> parseOneLine = liftM pure parseStmt parseFunType :: Parser Token FunType -parseFunType = satTok DoubleColonToken *> +parseFunType = satTok DoubleColonToken *> (parseInOutType <|> (liftM (FunType []) parseVoidOrType)) where parseInOutType :: Parser Token FunType @@ -79,7 +77,7 @@ parseFunType = satTok DoubleColonToken *> parseVoidOrType :: Parser Token (Maybe Type) parseVoidOrType = (satTok VoidToken *> pure Nothing) <|> - (liftM Just parseType) + (liftM Just parseType) <|> pure Nothing parseVarDecl :: Parser Token VarDecl parseVarDecl = liftM3 VarDecl @@ -150,8 +148,7 @@ parseVarDef = liftM2 VarDef (parseIdent >>= \i.if (i == "snd") (pure FieldSnd) empty)))) parseOp1 :: Parser Token Op1 -parseOp1 = trans1 DashToken UnMinus <|> - trans1 ExclamationToken UnNegation +parseOp1 = trans1 DashToken UnMinus <|> trans1 ExclamationToken UnNegation parseBBraces :: (Parser Token a) -> Parser Token a parseBBraces p = satTok BraceOpenToken *> p <* satTok BraceCloseToken @@ -168,7 +165,7 @@ parseTuple p = satTok BraceOpenToken *> <* satTok BraceCloseToken trans2 :: TokenValue (TokenValue -> a) -> Parser Token a -trans2 t f = satTok t >>= \(_, r).pure (f r) +trans2 t f = liftM (\{token}->f token) $ satTok t trans1 :: TokenValue a -> Parser Token a trans1 t r = trans2 t $ const r @@ -176,14 +173,13 @@ trans1 t r = trans2 t $ const r derive gPrint TokenValue derive gEq TokenValue satTok :: TokenValue -> Parser Token Token -satTok t = top >>= \tok=:(pos, tv) -> if (eq t tok) - (return tok) - (fail (printToString tv+++printToString t, pos)) +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 = @@ -191,7 +187,7 @@ parseSepList sep p = (liftM pure p) <|> pure empty parseIdent :: Parser Token String -parseIdent = trans2 (IdentToken []) (\(IdentToken e).toString e) +parseIdent = trans2 (IdentToken "") (\(IdentToken e).toString e) instance toString AST where toString (AST v f) = concat ( @@ -206,7 +202,7 @@ printersperse i j = intercalate [i] (map print j) instance print FunDecl where print (FunDecl i as t vs ss) = ["\n", i, " (":printersperse "," as] ++ - [") :: ":print t] ++ + [")"] ++ maybe [] (\tt->[" :: ":print tt]) t ++ ["{\n\t":printersperse "\n\t" vs] ++ ["\n":printStatements ss 1] ++ ["}\n"]