From 8bfb0f9b2072d35c05a4bb55a3d07209f24ab352 Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Wed, 8 Feb 2023 20:22:28 +0100 Subject: [PATCH] parse braces --- app/Parser.hs | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/app/Parser.hs b/app/Parser.hs index 43b38b5..1dd86ba 100644 --- a/app/Parser.hs +++ b/app/Parser.hs @@ -45,7 +45,7 @@ import Text.Megaparsec.Char (string) import IR (PrlgStr(..)) import Operators (ArgKind(..), Fixity(..), Op(..), Ops) -singleToks = ",;|()[]!" +singleToks = ",;|()[]{}!" identParts = "_" @@ -160,7 +160,7 @@ ws = many $ satisfy isBlank free = (<* ws) -- we eat blanks _after_ the token. -isNormalTokStr = (`notElem` [".", "[", "]", "(", "|", ")"]) +isNormalTokStr = (`notElem` [".", "[", "]", "(", "|", ")", "{", "}"]) isNormalTok (Tok x) = isNormalTokStr x isNormalTok (QTok x _) = isNormalTokStr x @@ -172,16 +172,20 @@ unTok (QTok t _) = t literal :: Parser PAST literal = Literal . unTok <$> free (satisfy isNormalTok <* notFollowedBy lParen) -call = do - fn <- unTok <$> satisfy isNormalTok -- not free - (Call fn [] <$ try emptyParens) <|> do - Seq inner <- free parens - return $ Call fn $ splitOn [Literal ","] inner +makeParams (Seq inner) = splitOn [Literal ","] inner + +call eb contents fmod = do + fn <- fmod . unTok <$> satisfy isNormalTok -- not free + (Call fn [] <$ try eb) <|> (Call fn . makeParams <$> free contents) parens = Seq <$> (free lParen *> some seqItem <* free rParen) +braces = Seq <$> (free lBrace *> some seqItem <* free rBrace) + emptyParens = Literal "()" <$ (free lParen >> free rParen) +emptyBraces = Literal "{}" <$ (free lBrace >> free rBrace) + list = do free lBracket (List [] Nothing <$ free rBracket) <|> do @@ -189,7 +193,17 @@ list = do (List items Nothing <$ free rBracket) <|> (List items . Just <$> (free listTail *> some seqItem <* free rBracket)) -seqItem = choice [try call, literal, try emptyParens, parens, list] +seqItem = + choice + [ try $ call emptyParens parens id + , try $ call emptyBraces braces (++ "{}") + , literal + , try emptyParens + , parens + , try emptyBraces + , Call "{}" . makeParams <$> braces + , list + ] simpleTok :: String -> Parser () simpleTok s = void $ single (Tok s) @@ -206,6 +220,10 @@ listTail = simpleTok "|" rBracket = simpleTok "]" +lBrace = simpleTok "{" + +rBrace = simpleTok "}" + clause :: Parser PAST clause = Seq <$> some (free seqItem) <* free period