Skip to main content

src/yum@yaml@lexer@block_scalar.erl

-module(yum@yaml@lexer@block_scalar).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/yum/yaml/lexer/block_scalar.gleam").
-export([lexer/0, header/2]).

-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).

-file("src/yum/yaml/lexer/block_scalar.gleam", 89).
?DOC(false).
-spec is_line_break(binary()) -> boolean().
is_line_break(Value) ->
    (Value =:= <<"\n"/utf8>>) orelse (Value =:= <<"\r"/utf8>>).

-file("src/yum/yaml/lexer/block_scalar.gleam", 81).
?DOC(false).
-spec only_spaces(binary()) -> boolean().
only_spaces(Value) ->
    case Value of
        <<""/utf8>> ->
            true;

        <<" "/utf8, Rest/binary>> ->
            only_spaces(Rest);

        _ ->
            false
    end.

-file("src/yum/yaml/lexer/block_scalar.gleam", 74).
?DOC(false).
-spec split_indent_loop(binary(), integer()) -> {integer(), binary()}.
split_indent_loop(Line, Indent) ->
    case Line of
        <<" "/utf8, Rest/binary>> ->
            split_indent_loop(Rest, Indent + 1);

        _ ->
            {Indent, Line}
    end.

-file("src/yum/yaml/lexer/block_scalar.gleam", 70).
?DOC(false).
-spec split_indent(binary()) -> {integer(), binary()}.
split_indent(Line) ->
    split_indent_loop(Line, 0).

-file("src/yum/yaml/lexer/block_scalar.gleam", 7).
?DOC(false).
-spec lexer() -> nibble@lexer:matcher(yum@yaml@token:token(), yum@yaml@lexer@context:context()).
lexer() ->
    nibble@lexer:custom(
        fun(Ctx, Lexeme, Lookahead) ->
            {Prev@1, Parent_indent@1} = case Ctx of
                {block_scalar, Prev, Parent_indent} -> {Prev, Parent_indent};
                _assert_fail ->
                    erlang:error(#{gleam_error => let_assert,
                                message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                                file => <<?FILEPATH/utf8>>,
                                module => <<"yum/yaml/lexer/block_scalar"/utf8>>,
                                function => <<"lexer"/utf8>>,
                                line => 9,
                                value => _assert_fail,
                                start => 275,
                                'end' => 334,
                                pattern_start => 286,
                                pattern_end => 328})
            end,
            case {Lexeme, Lookahead} of
                {<<"\n"/utf8>>, <<""/utf8>>} ->
                    {drop, Prev@1};

                {<<"\n"/utf8>>, <<"\n"/utf8>>} ->
                    _pipe = {block_scalar_line, 0, <<""/utf8>>},
                    {keep, _pipe, Ctx};

                {<<"\n"/utf8>>, _} ->
                    case Lookahead of
                        <<" "/utf8>> ->
                            skip;

                        <<"\t"/utf8>> ->
                            skip;

                        _ ->
                            _pipe@1 = {indentation, 0},
                            {keep, _pipe@1, {flow_style, {block_style, 0}}}
                    end;

                {<<"\n"/utf8, Line/binary>>, <<"\n"/utf8>>} ->
                    {Indent, Content} = split_indent(Line),
                    _pipe@2 = {block_scalar_line, Indent, Content},
                    {keep, _pipe@2, Ctx};

                {<<"\n"/utf8, Line/binary>>, <<""/utf8>>} ->
                    {Indent, Content} = split_indent(Line),
                    _pipe@2 = {block_scalar_line, Indent, Content},
                    {keep, _pipe@2, Ctx};

                {<<"\n"/utf8, Spaces/binary>>, _} ->
                    case {only_spaces(Spaces),
                        is_line_break(Lookahead),
                        Lookahead} of
                        {true, false, <<" "/utf8>>} ->
                            skip;

                        {true, false, <<""/utf8>>} ->
                            skip;

                        {true, false, _} ->
                            Indent@1 = string:length(Spaces),
                            case Indent@1 =< Parent_indent@1 of
                                true ->
                                    _pipe@3 = {indentation, Indent@1},
                                    {keep,
                                        _pipe@3,
                                        {flow_style, {block_style, Indent@1}}};

                                false ->
                                    skip
                            end;

                        {_, _, _} ->
                            skip
                    end;

                {_, _} ->
                    skip
            end
        end
    ).

-file("src/yum/yaml/lexer/block_scalar.gleam", 50).
?DOC(false).
-spec header(binary(), integer()) -> gleam@option:option(yum@yaml@token:token()).
header(Lexeme, Parent_indent) ->
    Trimmed = gleam@string:trim(Lexeme),
    case Trimmed of
        <<"|"/utf8>> ->
            {some, {block_scalar_header, literal, clip, Parent_indent}};

        <<"|-"/utf8>> ->
            {some, {block_scalar_header, literal, strip, Parent_indent}};

        <<"|+"/utf8>> ->
            {some, {block_scalar_header, literal, keep, Parent_indent}};

        <<">"/utf8>> ->
            {some, {block_scalar_header, folded, clip, Parent_indent}};

        <<">-"/utf8>> ->
            {some, {block_scalar_header, folded, strip, Parent_indent}};

        <<">+"/utf8>> ->
            {some, {block_scalar_header, folded, keep, Parent_indent}};

        _ ->
            none
    end.