@@ -423,18 +423,33 @@ cs(Module, Filename, Options, Context) ->
423423
424424compile_tokens ({ok , {extends , {string_literal , _ , Extend }, Elements }}, CState , _Options ) ->
425425 Blocks = find_blocks (Elements ),
426- {Ws , BlockAsts } = compile_blocks (Blocks , CState ),
427- {ok , {Extend , Ws # ws .includes , BlockAsts , undefined , Ws # ws .is_autoid_var }};
426+ case check_duplicate_blocks (Blocks ) of
427+ ok ->
428+ {Ws , BlockAsts } = compile_blocks (Blocks , CState ),
429+ {ok , {Extend , Ws # ws .includes , BlockAsts , undefined , Ws # ws .is_autoid_var }};
430+ {error , _ } = Error ->
431+ Error
432+ end ;
428433compile_tokens ({ok , {overrules , Elements }}, CState , _Options ) ->
429434 Blocks = find_blocks (Elements ),
430- {Ws , BlockAsts } = compile_blocks (Blocks , CState ),
431- {ok , {overrules , Ws # ws .includes , BlockAsts , undefined , Ws # ws .is_autoid_var }};
435+ case check_duplicate_blocks (Blocks ) of
436+ ok ->
437+ {Ws , BlockAsts } = compile_blocks (Blocks , CState ),
438+ {ok , {overrules , Ws # ws .includes , BlockAsts , undefined , Ws # ws .is_autoid_var }};
439+ {error , _ } = Error ->
440+ Error
441+ end ;
432442compile_tokens ({ok , {base , Elements }}, CState , _Options ) ->
433443 Blocks = find_blocks (Elements ),
434- {Ws , BlockAsts } = compile_blocks (Blocks , CState ),
435- CStateElts = CState # cs {blocks = BlockAsts },
436- {Ws1 , TemplateAsts } = template_compiler_element :compile (Elements , CStateElts , Ws ),
437- {ok , {undefined , Ws1 # ws .includes , BlockAsts , TemplateAsts , Ws1 # ws .is_autoid_var }};
444+ case check_duplicate_blocks (Blocks ) of
445+ ok ->
446+ {Ws , BlockAsts } = compile_blocks (Blocks , CState ),
447+ CStateElts = CState # cs {blocks = BlockAsts },
448+ {Ws1 , TemplateAsts } = template_compiler_element :compile (Elements , CStateElts , Ws ),
449+ {ok , {undefined , Ws1 # ws .includes , BlockAsts , TemplateAsts , Ws1 # ws .is_autoid_var }};
450+ {error , _ } = Error ->
451+ Error
452+ end ;
438453compile_tokens ({error , {Loc , template_compiler_parser , Msg }}, # cs { filename = Filename }, Options ) ->
439454 % Try format the Yecc error
440455 Err = split_loc (Loc ),
@@ -522,6 +537,20 @@ block_elements({filter, _, Elts}) -> Elts;
522537block_elements (_ ) -> [].
523538
524539
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+
525554% % @doc Optionally drop text before {% extends %} or {% overrules %}.
526555maybe_drop_text ([{text , _SrcRef , _Text }|Rest ], OrgTks ) ->
527556 maybe_drop_text (Rest , OrgTks );
0 commit comments