Skip to content

Commit

Permalink
Fragment call now supports a trailing symbol.
Browse files Browse the repository at this point in the history
The example `body= brs` was working but not `div= brs` since we defined
`div` to be a variable present in the environment, instead of being
parsed as BlockElem (like `body` still is).

Now `BlockFragmentCall` is supporting a trailing symbol in the same way
`BlockElem` does.
  • Loading branch information
noteed committed Jun 19, 2024
1 parent 14805e1 commit f5e63da
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 20 deletions.
4 changes: 4 additions & 0 deletions examples/expr-block.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
<br>
<br>
</body>
<div>
<br>
<br>
</div>
1 change: 1 addition & 0 deletions examples/expr-block.slab
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
let brs = br + br
body= brs
div= brs
10 changes: 5 additions & 5 deletions src/Slab/Evaluate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ eval env stack = \case
Nothing ->
pure $ BlockInclude mname path Nothing
node@(BlockFragmentDef _ _ _) -> pure node
BlockFragmentCall name attrs values args -> do
BlockFragmentCall name mdot attrs values args -> do
body <- call env stack name values args
let body' = setAttrs attrs body
pure $ BlockFragmentCall name attrs values body'
pure $ BlockFragmentCall name mdot attrs values body'
BlockFor name mindex values nodes -> do
-- Re-use BlockFor to construct a single node to return.
let zero :: Int
Expand Down Expand Up @@ -169,7 +169,7 @@ namedBlock _ = pure []

unnamedBlock :: Monad m => Block -> ExceptT Error.Error m [Block]
unnamedBlock (BlockImport path _ args) =
pure [BlockFragmentCall (T.pack path) [] [] args]
pure [BlockFragmentCall (T.pack path) NoSym [] [] args]
unnamedBlock (BlockFragmentDef _ _ _) = pure []
unnamedBlock node = pure [node]

Expand Down Expand Up @@ -293,7 +293,7 @@ extractVariables env = concatMap f
f (BlockInclude _ _ children) = maybe [] (extractVariables env) children
f (BlockFor _ _ _ _) = []
f (BlockFragmentDef name names children) = [(name, Frag names env children)]
f (BlockFragmentCall _ _ _ _) = []
f (BlockFragmentCall _ _ _ _ _) = []
f (BlockComment _ _) = []
f (BlockFilter _ _) = []
f (BlockRawElem _ _) = []
Expand Down Expand Up @@ -329,7 +329,7 @@ simplify' = \case
node@(BlockText _ _) -> [node]
BlockInclude _ _ mnodes -> maybe [] simplify mnodes
BlockFragmentDef _ _ _ -> []
BlockFragmentCall _ _ _ args -> simplify args
BlockFragmentCall _ _ _ _ args -> simplify args
BlockFor _ _ _ nodes -> simplify nodes
node@(BlockComment _ _) -> [node]
node@(BlockFilter _ _) -> [node]
Expand Down
4 changes: 2 additions & 2 deletions src/Slab/Execute.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ exec ctx = \case
Syntax.BlockFragmentDef name params nodes -> do
nodes' <- execute ctx nodes
pure $ Syntax.BlockFragmentDef name params nodes'
Syntax.BlockFragmentCall name attrs values nodes -> do
Syntax.BlockFragmentCall name mdot attrs values nodes -> do
nodes' <- execute ctx nodes
pure $ Syntax.BlockFragmentCall name attrs values nodes'
pure $ Syntax.BlockFragmentCall name mdot attrs values nodes'
node@(Syntax.BlockFor _ _ _ _) -> pure node
node@(Syntax.BlockComment _ _) -> pure node
node@(Syntax.BlockFilter _ _) -> pure node
Expand Down
14 changes: 8 additions & 6 deletions src/Slab/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ parserElement :: Parser (L.IndentOpt Parser Block Block)
parserElement = do
ref <- L.indentLevel
header <- parserDiv
parserElemBody ref header

parserElemBody :: Pos -> ([Block] -> Block) -> Parser (L.IndentOpt Parser Block Block)
parserElemBody ref header =
case trailingSym $ header [] of
HasDot -> do
template <- parseInlines
Expand Down Expand Up @@ -464,16 +468,14 @@ parserParameters = parserList' "{" "}" parserIdentifier <?> "arguments"
--------------------------------------------------------------------------------
parserFragmentCall :: Parser (L.IndentOpt Parser Block Block)
parserFragmentCall = do
ref <- L.indentLevel
-- TODO Use parserNameWithAttrs.
name <- parserIdentifier
attrs <- concat <$> many parserAttrs'
trailing <- parserTrailingSym
args <- maybe [] id <$> optional parserArguments
template <- parseInlines
case template of
[] ->
pure $ L.IndentMany Nothing (pure . BlockFragmentCall name attrs args) parserNode
_ ->
pure $ L.IndentNone $ BlockFragmentCall name attrs args [BlockText Normal template]
let header = BlockFragmentCall name trailing attrs args
parserElemBody ref header

-- E.g. {}, {1, 'a'}
parserArguments :: Parser [Expr]
Expand Down
4 changes: 2 additions & 2 deletions src/Slab/PreProcess.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ preproc ctx@Context {..} = \case
BlockFragmentDef name params nodes -> do
nodes' <- preprocess ctx nodes
pure $ BlockFragmentDef name params nodes'
BlockFragmentCall name attrs values nodes -> do
BlockFragmentCall name mdot attrs values nodes -> do
nodes' <- preprocess ctx nodes
pure $ BlockFragmentCall name attrs values nodes'
pure $ BlockFragmentCall name mdot attrs values nodes'
node@(BlockFor _ _ _ _) -> pure node
node@(BlockComment _ _) -> pure node
node@(BlockFilter _ _) -> pure node
Expand Down
4 changes: 2 additions & 2 deletions src/Slab/Render.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ renderBlock (Syntax.BlockInclude (Just "escape-html") _ (Just nodes)) =
renderBlock (Syntax.BlockInclude _ _ (Just nodes)) = mapM_ renderBlock nodes
renderBlock (Syntax.BlockInclude _ path Nothing) = H.stringComment $ "include " <> path
renderBlock (Syntax.BlockFragmentDef _ _ _) = mempty
renderBlock (Syntax.BlockFragmentCall _ _ _ nodes) = mapM_ renderBlock nodes -- TODO attrs
renderBlock (Syntax.BlockFragmentCall _ _ _ _ nodes) = mapM_ renderBlock nodes
renderBlock (Syntax.BlockFor _ _ _ nodes) = mapM_ renderBlock nodes
renderBlock (Syntax.BlockComment b content) =
case b of
Expand Down Expand Up @@ -129,7 +129,7 @@ extractText = f
f (Syntax.BlockText _ _) = error "extractTexts called on unevaluated BlockText"
f (Syntax.BlockInclude _ _ _) = error "extractTexts called on a BlockInclude"
f (Syntax.BlockFragmentDef _ _ _) = error "extractTexts called on a BlockFragmentDef"
f (Syntax.BlockFragmentCall _ _ _ _) = error "extractTexts called on a BlockFragmentCall"
f (Syntax.BlockFragmentCall _ _ _ _ _) = error "extractTexts called on a BlockFragmentCall"
f (Syntax.BlockFor _ _ _ _) = error "extractTexts called on a BlockFor"
f (Syntax.BlockComment _ _) = error "extractTexts called on a BlockComment"
f (Syntax.BlockFilter _ _) = error "extractTexts called on a BlockFilter"
Expand Down
8 changes: 5 additions & 3 deletions src/Slab/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ data Block
| -- | This doesn't exist in Pug. This is like a mixin than receive block arguments.
-- Or like a parent template that can be @extended@ by a child template.
BlockFragmentDef Text [Text] [Block]
| BlockFragmentCall Text [Attr] [Expr] [Block]
| BlockFragmentCall Text TrailingSym [Attr] [Expr] [Block]
| BlockFor Text (Maybe Text) Expr [Block]
| -- TODO Should we allow string interpolation here ?
BlockComment CommentType Text
Expand Down Expand Up @@ -72,6 +72,7 @@ isDoctype _ = False

trailingSym :: Block -> TrailingSym
trailingSym (BlockElem _ sym _ _) = sym
trailingSym (BlockFragmentCall _ sym _ _ _) = sym
trailingSym _ = NoSym

-- | Takes two blocks and returns a BlockList containing both, but peel the
Expand Down Expand Up @@ -247,7 +248,7 @@ extractClasses = nub . sort . concatMap f
f (BlockText _ _) = []
f (BlockInclude _ _ children) = maybe [] extractClasses children
f (BlockFragmentDef _ _ _) = [] -- We extract them in BlockFragmentCall instead.
f (BlockFragmentCall _ attrs _ children) = concatMap g attrs <> extractClasses children
f (BlockFragmentCall _ _ attrs _ children) = concatMap g attrs <> extractClasses children
f (BlockFor _ _ _ children) = extractClasses children
f (BlockComment _ _) = []
f (BlockFilter _ _) = []
Expand Down Expand Up @@ -283,7 +284,8 @@ extractFragments = concatMap f
f (BlockText _ _) = []
f (BlockInclude _ _ children) = maybe [] extractFragments children
f (BlockFragmentDef name _ children) = [BlockFragmentDef' name children]
f (BlockFragmentCall name _ _ children) = [BlockFragmentCall' name] <> extractFragments children
f (BlockFragmentCall name _ _ _ children) =
[BlockFragmentCall' name] <> extractFragments children
f (BlockFor _ _ _ children) = extractFragments children
f (BlockComment _ _) = []
f (BlockFilter _ _) = []
Expand Down

0 comments on commit f5e63da

Please sign in to comment.