Packages

Handles is a templating language written in pure Gleam. Heavily inspired by Mustache and Handlebars.js

Current section

Files

Jump to
handles src handles@internal@tokenizer.erl
Raw

src/handles@internal@tokenizer.erl

-module(handles@internal@tokenizer).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([run/3]).
-export_type([token/0]).
-type token() :: {constant, integer(), binary()} |
{property, integer(), list(binary())} |
{if_block_start, integer(), list(binary())} |
{if_block_end, integer()} |
{unless_block_start, integer(), list(binary())} |
{unless_block_end, integer()} |
{each_block_start, integer(), list(binary())} |
{each_block_end, integer()}.
-spec run(binary(), integer(), list(token())) -> {ok, list(token())} |
{error, handles@error:tokenizer_error()}.
run(Input, Index, Tokens) ->
case Input of
<<"{{/if"/utf8, Rest/binary>> ->
case begin
_pipe = Rest,
gleam@string:split_once(_pipe, <<"}}"/utf8>>)
end of
{ok, {<<""/utf8>>, Rest@1}} ->
run(Rest@1, Index + 7, [{if_block_end, Index + 2} | Tokens]);
_ ->
{error, {unexpected_block_argument, Index + 2}}
end;
<<"{{#if"/utf8, Rest@2/binary>> ->
case begin
_pipe@1 = Rest@2,
gleam@string:split_once(_pipe@1, <<"}}"/utf8>>)
end of
{ok, {Arg, Rest@3}} ->
case begin
_pipe@2 = Arg,
_pipe@3 = gleam@string:trim(_pipe@2),
_pipe@4 = gleam@string:trim(_pipe@3),
gleam@string:split(_pipe@4, <<"."/utf8>>)
end of
[<<""/utf8>>] ->
{error, {missing_block_argument, Index + 2}};
Path ->
run(
Rest@3,
(Index + 7) + gleam@string:length(Arg),
[{if_block_start, Index + 2, Path} | Tokens]
)
end;
{error, _} ->
{error, {unbalanced_tag, Index + 2}}
end;
<<"{{/unless"/utf8, Rest@4/binary>> ->
case begin
_pipe@5 = Rest@4,
gleam@string:split_once(_pipe@5, <<"}}"/utf8>>)
end of
{ok, {<<""/utf8>>, Rest@5}} ->
run(
Rest@5,
Index + 11,
[{unless_block_end, Index + 2} | Tokens]
);
_ ->
{error, {unexpected_block_argument, Index + 2}}
end;
<<"{{#unless"/utf8, Rest@6/binary>> ->
case begin
_pipe@6 = Rest@6,
gleam@string:split_once(_pipe@6, <<"}}"/utf8>>)
end of
{ok, {Arg@1, Rest@7}} ->
case begin
_pipe@7 = Arg@1,
_pipe@8 = gleam@string:trim(_pipe@7),
gleam@string:split(_pipe@8, <<"."/utf8>>)
end of
[<<""/utf8>>] ->
{error, {missing_block_argument, Index + 2}};
Path@1 ->
run(
Rest@7,
(Index + 11) + gleam@string:length(Arg@1),
[{unless_block_start, Index + 2, Path@1} |
Tokens]
)
end;
{error, _} ->
{error, {unbalanced_tag, Index + 2}}
end;
<<"{{/each"/utf8, Rest@8/binary>> ->
case begin
_pipe@9 = Rest@8,
gleam@string:split_once(_pipe@9, <<"}}"/utf8>>)
end of
{ok, {<<""/utf8>>, Rest@9}} ->
run(
Rest@9,
Index + 9,
[{each_block_end, Index + 2} | Tokens]
);
_ ->
{error, {unexpected_block_argument, Index + 2}}
end;
<<"{{#each"/utf8, Rest@10/binary>> ->
case begin
_pipe@10 = Rest@10,
gleam@string:split_once(_pipe@10, <<"}}"/utf8>>)
end of
{ok, {Arg@2, Rest@11}} ->
case begin
_pipe@11 = Arg@2,
_pipe@12 = gleam@string:trim(_pipe@11),
gleam@string:split(_pipe@12, <<"."/utf8>>)
end of
[<<""/utf8>>] ->
{error, {missing_block_argument, Index + 2}};
Path@2 ->
run(
Rest@11,
(Index + 9) + gleam@string:length(Arg@2),
[{each_block_start, Index + 2, Path@2} | Tokens]
)
end;
{error, _} ->
{error, {unbalanced_tag, Index + 2}}
end;
<<"{{/"/utf8, _/binary>> ->
{error, {unexpected_block_kind, Index + 2}};
<<"{{#"/utf8, _/binary>> ->
{error, {unexpected_block_kind, Index + 2}};
<<"{{"/utf8, Rest@12/binary>> ->
case begin
_pipe@13 = Rest@12,
gleam@string:split_once(_pipe@13, <<"}}"/utf8>>)
end of
{ok, {Body, Rest@13}} ->
case begin
_pipe@14 = Body,
_pipe@15 = gleam@string:trim(_pipe@14),
gleam@string:split(_pipe@15, <<"."/utf8>>)
end of
[<<""/utf8>>] ->
{error, {missing_property_path, Index + 2}};
[<<""/utf8>>, <<""/utf8>>] ->
run(
Rest@13,
(Index + 4) + gleam@string:length(Body),
[{property, Index + 2, []} | Tokens]
);
Path@3 ->
run(
Rest@13,
(Index + 4) + gleam@string:length(Body),
[{property, Index + 2, Path@3} | Tokens]
)
end;
{error, _} ->
{error, {unbalanced_tag, Index + 2}}
end;
_ ->
case begin
_pipe@16 = Input,
gleam@string:split_once(_pipe@16, <<"{{"/utf8>>)
end of
{ok, {Str, Rest@14}} ->
run(
<<"{{"/utf8, Rest@14/binary>>,
Index + gleam@string:length(Str),
[{constant, Index, Str} | Tokens]
);
_ ->
case Input of
<<""/utf8>> ->
{ok, lists:reverse(Tokens)};
Str@1 ->
{ok,
lists:reverse(
[{constant, Index, Str@1} | Tokens]
)}
end
end
end.