@@ -13,7 +13,6 @@ data Expr
1313 = TextExpr T. Text
1414 | FunCallExpr T. Text
1515 [Expr ]
16- | VarExpr T. Text
1716 deriving (Eq , Show )
1817
1918type NameTable = ()
@@ -28,38 +27,47 @@ stringLiteral = do
2827 _ <- charP ' "'
2928 return $ TextExpr value
3029
31- funcallarg :: Parser Expr
32- funcallarg = funcall <|> var <|> stringLiteral
30+ funCallArg :: Parser Expr
31+ funCallArg = funCall <|> stringLiteral
3332
34- funcall :: Parser Expr
35- funcall = do
36- _ <- charP ' %'
37- name <- symbol
38- _ <- whitespaces >> charP ' (' >> whitespaces
33+ funCallArgList :: Parser [Expr ]
34+ funCallArgList = do
35+ _ <- charP ' (' <* whitespaces
3936 args <-
40- sepBy funcallarg (whitespaces >> charP ' ,' >> whitespaces) <|> return []
37+ sepBy funCallArg (whitespaces >> charP ' ,' >> whitespaces) <|> return []
4138 _ <- whitespaces >> charP ' )'
39+ return args
40+
41+ funCall :: Parser Expr
42+ funCall = do
43+ name <- charP ' %' *> symbol
44+ args <- funCallArgList <|> return []
4245 return $ FunCallExpr name args
4346
4447whitespaces :: Parser T. Text
4548whitespaces = takeWhileP isSpace
4649
47- var :: Parser Expr
48- var = charP ' %' *> (VarExpr <$> symbol) <* charP ' %'
49-
5050textBlock :: Parser Expr
5151textBlock =
5252 Parser $ \ input ->
5353 case T. uncons input of
5454 Nothing -> Left EOF
55- Just (' %' , _) -> Left (SyntaxError " Text block does not start with %" )
55+ Just (' %' , input') ->
56+ return $ fmap (TextExpr . T. cons ' %' ) $ swap $ T. span (/= ' %' ) input'
5657 _ -> return $ fmap TextExpr $ swap $ T. span (/= ' %' ) input
5758
5859expr :: Parser Expr
59- expr = funcall <|> var <|> textBlock
60+ expr = funCall <|> textBlock
6061
6162exprs :: Parser [Expr ]
62- exprs = many expr
63+ exprs = normalizeExprs <$> many expr
64+ where
65+ normalizeExprs :: [Expr ] -> [Expr ]
66+ normalizeExprs [] = []
67+ normalizeExprs (TextExpr t1: TextExpr t2: rest) =
68+ normalizeExprs (TextExpr (t1 <> t2) : rest)
69+ normalizeExprs (x: rest) = x : normalizeExprs rest
70+
6371-- TODO(#600): interpretExprs is not implemented
6472-- interpretExprs :: NameTable -> [Expr] -> Effect T.Text
6573-- interpretExprs = undefined
0 commit comments