@@ -422,27 +422,24 @@ cs(Module, Filename, Options, Context) ->
422422 }.
423423
424424compile_tokens ({ok , {extends , {string_literal , _ , Extend }, Elements }}, CState , _Options ) ->
425- Blocks = find_blocks (Elements ),
426- case check_duplicate_blocks (Blocks ) of
427- ok ->
425+ case find_blocks (Elements ) of
426+ {ok , Blocks } ->
428427 {Ws , BlockAsts } = compile_blocks (Blocks , CState ),
429428 {ok , {Extend , Ws # ws .includes , BlockAsts , undefined , Ws # ws .is_autoid_var }};
430429 {error , _ } = Error ->
431430 Error
432431 end ;
433432compile_tokens ({ok , {overrules , Elements }}, CState , _Options ) ->
434- Blocks = find_blocks (Elements ),
435- case check_duplicate_blocks (Blocks ) of
436- ok ->
433+ case find_blocks (Elements ) of
434+ {ok , Blocks } ->
437435 {Ws , BlockAsts } = compile_blocks (Blocks , CState ),
438436 {ok , {overrules , Ws # ws .includes , BlockAsts , undefined , Ws # ws .is_autoid_var }};
439437 {error , _ } = Error ->
440438 Error
441439 end ;
442440compile_tokens ({ok , {base , Elements }}, CState , _Options ) ->
443- Blocks = find_blocks (Elements ),
444- case check_duplicate_blocks (Blocks ) of
445- ok ->
441+ case find_blocks (Elements ) of
442+ {ok , Blocks } ->
446443 {Ws , BlockAsts } = compile_blocks (Blocks , CState ),
447444 CStateElts = CState # cs {blocks = BlockAsts },
448445 {Ws1 , TemplateAsts } = template_compiler_element :compile (Elements , CStateElts , Ws ),
@@ -517,12 +514,26 @@ reset_block_ws(Ws) ->
517514
518515% % @doc Extract all block definitions from the parse tree, returns deepest nested blocks first
519516find_blocks (Elements ) ->
520- find_blocks (Elements , []).
517+ case find_blocks (Elements , {ok , [], []}) of
518+ {ok , Acc , _Stack } ->
519+ {ok , Acc };
520+ {error , _ } = Error ->
521+ Error
522+ end .
521523
524+ find_blocks (_ , {error , _ } = Error ) ->
525+ Error ;
522526find_blocks (List , Acc ) when is_list (List ) ->
523527 lists :foldl (fun find_blocks /2 , Acc , List );
524- find_blocks ({block , _Name , Elements } = Block , Acc ) ->
525- find_blocks (Elements , [Block |Acc ]);
528+ find_blocks ({block , {identifier , _Pos , Name }, Elements } = Block , {ok , Acc , Stack }) ->
529+ case lists :member (Name , Stack ) of
530+ true ->
531+ {error , {duplicate_block , Name }};
532+ false ->
533+ Acc1 = [ Block | Acc ],
534+ Stack1 = [ Name | Stack ],
535+ find_blocks (Elements , {ok , Acc1 , Stack1 })
536+ end ;
526537find_blocks (Element , Acc ) ->
527538 find_blocks (block_elements (Element ), Acc ).
528539
@@ -537,20 +548,6 @@ block_elements({filter, _, Elts}) -> Elts;
537548block_elements (_ ) -> [].
538549
539550
540- check_duplicate_blocks (Blocks ) ->
541- check_duplicate_blocks_1 (Blocks , #{}).
542-
543- check_duplicate_blocks_1 ([], _Acc ) ->
544- ok ;
545- check_duplicate_blocks_1 ([{block , {identifier , _Pos , Name }, _Elements }|Blocks ], Acc ) ->
546- case maps :is_key (Name , Acc ) of
547- true ->
548- {error , {duplicate_block , Name }};
549- false ->
550- check_duplicate_blocks_1 (Blocks , Acc #{ Name => true })
551- end .
552-
553-
554551% % @doc Optionally drop text before {% extends %} or {% overrules %}.
555552maybe_drop_text ([{text , _SrcRef , _Text }|Rest ], OrgTks ) ->
556553 maybe_drop_text (Rest , OrgTks );
0 commit comments