erlydtl_test_extension.erl 1.4 KB

12345678910111213141516171819202122232425262728293031
  1. -module(erlydtl_test_extension).
  2. -export([scan/1, parse/1, compile_ast/2]).
  3. -include("erlydtl_ext.hrl").
  4. %% look for a foo identifer followed by a #
  5. scan(#scanner_state{ template="#" ++ T,
  6. scanned=[{identifier, Loc, foo}|Scanned],
  7. pos={L,C} }=S) ->
  8. %% return new state with the hash dropped, and the foo identifer replaced with bar
  9. {ok, S#scanner_state{ template=T,
  10. scanned=[{identifier, Loc, "rab"}|Scanned],
  11. pos={L, C+1} }};
  12. scan(#scanner_state{ template="#" ++ _T, pos=Pos }) ->
  13. %% give error when # not follows foo
  14. {error, {Pos,erlydtl_scanner,{illegal_char, $#}}};
  15. scan(_) ->
  16. %% for anything else, fallback to the error message from erlydtl_scanner..
  17. undefined.
  18. parse(State) ->
  19. erlydtl_extension_testparser:resume(State).
  20. %% {{ varA or varB }} is equivalent to {% if varA %}{{ varA }}{% else %}{{ varB }}{% endif %}
  21. compile_ast({value_or, {Value1, Value2}}, TreeWalker) ->
  22. {{V1_Ast, V1_Info}, TW1} = erlydtl_beam_compiler:value_ast(Value1, false, false, TreeWalker),
  23. {{V2_Ast, V2_Info}, TW2} = erlydtl_beam_compiler:value_ast(Value2, false, false, TW1),
  24. {{erl_syntax:case_expr(V1_Ast,
  25. [erl_syntax:clause([erl_syntax:atom(undefined)], none, [V2_Ast]),
  26. erl_syntax:clause([erl_syntax:underscore()], none, [V1_Ast])
  27. ]), erlydtl_compiler_utils:merge_info(V1_Info, V2_Info)}, TW2}.