From b5b044ceb4c178a656c0ddc27adec4a719b35893 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Fri, 24 Jul 2020 21:53:41 +0200 Subject: Support tuples --- parser/CC/Parser.hs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'parser') diff --git a/parser/CC/Parser.hs b/parser/CC/Parser.hs index 856a7b9..a097f98 100644 --- a/parser/CC/Parser.hs +++ b/parser/CC/Parser.hs @@ -51,7 +51,16 @@ pType :: Parser Type pType = chainr1 pTypeAtom (symbol "->" >> return TFun) pTypeAtom :: Parser Type -pTypeAtom = (wordToken "Int" >> return TInt) <|> between (token "(") (token ")") pType +pTypeAtom = (wordToken "Int" >> return TInt) <|> pParenType + +pParenType :: Parser Type +pParenType = do + token "(" + tys <- pType `sepBy` token "," + token ")" + case tys of + [ty] -> return ty + _ -> return (TTup tys) pExpr :: Parser Expr pExpr = label (pCall <|> pLam) "expression" @@ -84,7 +93,18 @@ pExprAtom :: Parser Expr pExprAtom = choice [ uncurry (flip Int) <$> pInt , uncurry (flip Var) <$> pName - , between (token "(") (token ")") pExpr ] + , pParenExpr ] + +pParenExpr :: Parser Expr +pParenExpr = do + p1 <- getPosition + token "(" + exprs <- pExpr `sepBy` token "," + token ")" + p2 <- getPosition + case exprs of + [expr] -> return expr + _ -> return (Tup (SourceRange p1 p2) exprs) pInt :: Parser (Int, SourceRange) pInt = try (whitespace >> pInt0) -- cgit v1.2.3-70-g09d2