-module(handles@internal@tokenizer). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/handles/internal/tokenizer.gleam"). -export([run/1]). -export_type([token/0, action/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC(false). -type token() :: {constant, integer(), binary()} | {property, integer(), list(binary())} | {partial, integer(), binary(), list(binary())} | {block_start, integer(), handles@internal@block:kind(), list(binary())} | {block_end, integer(), handles@internal@block:kind()}. -type action() :: {add_token, token(), integer(), binary()} | {stop, handles@error:tokenizer_error()} | done. -file("src/handles/internal/tokenizer.gleam", 31). ?DOC(false). -spec split_body(binary()) -> list(binary()). split_body(Body) -> _pipe = Body, _pipe@1 = gleam@string:trim(_pipe), _pipe@2 = gleam@string:split(_pipe@1, <<" "/utf8>>), gleam@list:filter( _pipe@2, fun(It) -> begin _pipe@3 = It, _pipe@4 = gleam@string:trim(_pipe@3), string:length(_pipe@4) end > 0 end ). -file("src/handles/internal/tokenizer.gleam", 43). ?DOC(false). -spec split_arg(binary()) -> list(binary()). split_arg(Arg) -> case begin _pipe = Arg, gleam@string:trim(_pipe) end of <<"."/utf8>> -> []; Arg@1 -> gleam@string:split(Arg@1, <<"."/utf8>>) end. -file("src/handles/internal/tokenizer.gleam", 50). ?DOC(false). -spec capture_tag_body(binary(), integer()) -> {ok, {binary(), binary()}} | {error, handles@error:tokenizer_error()}. capture_tag_body(Input, Index) -> _pipe = Input, _pipe@1 = gleam@string:split_once(_pipe, <<"}}"/utf8>>), gleam@result:map_error(_pipe@1, fun(_) -> _pipe@2 = Index + 2, {unbalanced_tag, _pipe@2} end). -file("src/handles/internal/tokenizer.gleam", 62). ?DOC(false). -spec stop(integer(), fun((integer()) -> handles@error:tokenizer_error())) -> action(). stop(Index, To_error) -> _pipe = Index + 2, _pipe@1 = To_error(_pipe), {stop, _pipe@1}. -file("src/handles/internal/tokenizer.gleam", 68). ?DOC(false). -spec add_block_sized_token(token(), integer(), binary(), binary()) -> action(). add_block_sized_token(Token, Index, Consumed, Rest) -> {add_token, Token, (Index + 5) + string:length(Consumed), Rest}. -file("src/handles/internal/tokenizer.gleam", 81). ?DOC(false). -spec tokenize(binary(), integer()) -> action(). tokenize(Input, Index) -> case Input of <<""/utf8>> -> done; <<"{{>"/utf8, Rest/binary>> -> case capture_tag_body(Rest, Index) of {error, Err} -> {stop, Err}; {ok, {Body, Rest@1}} -> case split_body(Body) of [] -> stop( Index, fun(Field@0) -> {missing_partial_id, Field@0} end ); [_] -> stop( Index, fun(Field@0) -> {missing_argument, Field@0} end ); [Id, Arg] -> _pipe = {partial, Index + 2, Id, split_arg(Arg)}, add_block_sized_token(_pipe, Index, Body, Rest@1); _ -> stop( Index, fun(Field@0) -> {unexpected_multiple_arguments, Field@0} end ) end end; <<"{{#"/utf8, Rest@2/binary>> -> case capture_tag_body(Rest@2, Index) of {error, Err@1} -> {stop, Err@1}; {ok, {Body@1, Rest@3}} -> case split_body(Body@1) of [] -> stop( Index, fun(Field@0) -> {missing_block_kind, Field@0} end ); [_] -> stop( Index, fun(Field@0) -> {missing_argument, Field@0} end ); [<<"if"/utf8>>, Arg@1] -> _pipe@1 = {block_start, Index + 2, 'if', split_arg(Arg@1)}, add_block_sized_token( _pipe@1, Index, Body@1, Rest@3 ); [<<"unless"/utf8>>, Arg@2] -> _pipe@2 = {block_start, Index + 2, unless, split_arg(Arg@2)}, add_block_sized_token( _pipe@2, Index, Body@1, Rest@3 ); [<<"each"/utf8>>, Arg@3] -> _pipe@3 = {block_start, Index + 2, each, split_arg(Arg@3)}, add_block_sized_token( _pipe@3, Index, Body@1, Rest@3 ); [_, _] -> stop( Index, fun(Field@0) -> {unexpected_block_kind, Field@0} end ); _ -> stop( Index, fun(Field@0) -> {unexpected_multiple_arguments, Field@0} end ) end end; <<"{{/"/utf8, Rest@4/binary>> -> case capture_tag_body(Rest@4, Index) of {error, Err@2} -> {stop, Err@2}; {ok, {Body@2, Rest@5}} -> case split_body(Body@2) of [] -> stop( Index, fun(Field@0) -> {missing_block_kind, Field@0} end ); [_, _] -> stop( Index, fun(Field@0) -> {unexpected_argument, Field@0} end ); [<<"if"/utf8>>] -> _pipe@4 = {block_end, Index + 2, 'if'}, add_block_sized_token( _pipe@4, Index, Body@2, Rest@5 ); [<<"unless"/utf8>>] -> _pipe@5 = {block_end, Index + 2, unless}, add_block_sized_token( _pipe@5, Index, Body@2, Rest@5 ); [<<"each"/utf8>>] -> _pipe@6 = {block_end, Index + 2, each}, add_block_sized_token( _pipe@6, Index, Body@2, Rest@5 ); [_] -> stop( Index, fun(Field@0) -> {unexpected_block_kind, Field@0} end ); _ -> stop( Index, fun(Field@0) -> {unexpected_argument, Field@0} end ) end end; <<"{{"/utf8, Rest@6/binary>> -> case capture_tag_body(Rest@6, Index) of {error, Err@3} -> {stop, Err@3}; {ok, {Body@3, Rest@7}} -> case split_body(Body@3) of [] -> stop( Index, fun(Field@0) -> {missing_argument, Field@0} end ); [Arg@4] -> {add_token, {property, Index + 2, split_arg(Arg@4)}, (Index + 4) + string:length(Body@3), Rest@7}; _ -> stop( Index, fun(Field@0) -> {unexpected_multiple_arguments, Field@0} end ) end end; _ -> case begin _pipe@7 = Input, _pipe@8 = gleam@string:split_once(_pipe@7, <<"{{"/utf8>>), gleam@result:map( _pipe@8, fun(_capture) -> gleam@pair:map_second( _capture, fun(It) -> <<"{{"/utf8, It/binary>> end ) end ) end of {ok, {Str, Rest@8}} -> {add_token, {constant, Index, Str}, Index + string:length(Str), Rest@8}; _ -> {add_token, {constant, Index, Input}, Index + string:length(Input), <<""/utf8>>} end end. -file("src/handles/internal/tokenizer.gleam", 180). ?DOC(false). -spec do_run(binary(), integer(), list(token())) -> {ok, list(token())} | {error, handles@error:tokenizer_error()}. do_run(Input, Index, Tokens) -> case tokenize(Input, Index) of done -> {ok, lists:reverse(Tokens)}; {stop, Err} -> {error, Err}; {add_token, Token, Index@1, Rest} -> do_run(Rest, Index@1, [Token | Tokens]) end. -file("src/handles/internal/tokenizer.gleam", 192). ?DOC(false). -spec run(binary()) -> {ok, list(token())} | {error, handles@error:tokenizer_error()}. run(Input) -> do_run(Input, 0, []).