parse braces
This commit is contained in:
parent
cce22d561b
commit
8bfb0f9b20
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue