parse braces

This commit is contained in:
Mirek Kratochvil 2023-02-08 20:22:28 +01:00
parent cce22d561b
commit 8bfb0f9b20

View file

@ -45,7 +45,7 @@ import Text.Megaparsec.Char (string)
import IR (PrlgStr(..)) import IR (PrlgStr(..))
import Operators (ArgKind(..), Fixity(..), Op(..), Ops) import Operators (ArgKind(..), Fixity(..), Op(..), Ops)
singleToks = ",;|()[]!" singleToks = ",;|()[]{}!"
identParts = "_" identParts = "_"
@ -160,7 +160,7 @@ ws = many $ satisfy isBlank
free = (<* ws) -- we eat blanks _after_ the token. free = (<* ws) -- we eat blanks _after_ the token.
isNormalTokStr = (`notElem` [".", "[", "]", "(", "|", ")"]) isNormalTokStr = (`notElem` [".", "[", "]", "(", "|", ")", "{", "}"])
isNormalTok (Tok x) = isNormalTokStr x isNormalTok (Tok x) = isNormalTokStr x
isNormalTok (QTok x _) = isNormalTokStr x isNormalTok (QTok x _) = isNormalTokStr x
@ -172,16 +172,20 @@ unTok (QTok t _) = t
literal :: Parser PAST literal :: Parser PAST
literal = Literal . unTok <$> free (satisfy isNormalTok <* notFollowedBy lParen) literal = Literal . unTok <$> free (satisfy isNormalTok <* notFollowedBy lParen)
call = do makeParams (Seq inner) = splitOn [Literal ","] inner
fn <- unTok <$> satisfy isNormalTok -- not free
(Call fn [] <$ try emptyParens) <|> do call eb contents fmod = do
Seq inner <- free parens fn <- fmod . unTok <$> satisfy isNormalTok -- not free
return $ Call fn $ splitOn [Literal ","] inner (Call fn [] <$ try eb) <|> (Call fn . makeParams <$> free contents)
parens = Seq <$> (free lParen *> some seqItem <* free rParen) parens = Seq <$> (free lParen *> some seqItem <* free rParen)
braces = Seq <$> (free lBrace *> some seqItem <* free rBrace)
emptyParens = Literal "()" <$ (free lParen >> free rParen) emptyParens = Literal "()" <$ (free lParen >> free rParen)
emptyBraces = Literal "{}" <$ (free lBrace >> free rBrace)
list = do list = do
free lBracket free lBracket
(List [] Nothing <$ free rBracket) <|> do (List [] Nothing <$ free rBracket) <|> do
@ -189,7 +193,17 @@ list = do
(List items Nothing <$ free rBracket) <|> (List items Nothing <$ free rBracket) <|>
(List items . Just <$> (free listTail *> some seqItem <* 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 :: String -> Parser ()
simpleTok s = void $ single (Tok s) simpleTok s = void $ single (Tok s)
@ -206,6 +220,10 @@ listTail = simpleTok "|"
rBracket = simpleTok "]" rBracket = simpleTok "]"
lBrace = simpleTok "{"
rBrace = simpleTok "}"
clause :: Parser PAST clause :: Parser PAST
clause = Seq <$> some (free seqItem) <* free period clause = Seq <$> some (free seqItem) <* free period