Packages

A URL path pattern matching library for the Gleam programming language

Current section

Files

Jump to
pathern src pathern@internal@parser.erl
Raw

src/pathern@internal@parser.erl

-module(pathern@internal@parser).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([parse/2]).
-file("/home/ram/code/gleam/pathern/src/pathern/internal/parser.gleam", 90).
-spec parse_param(binary(), binary()) -> binary().
parse_param(Path, Accum) ->
case gleam@string:first(Path) of
{ok, Grapheme} ->
case Grapheme of
<<"/"/utf8>> ->
Accum;
_ ->
parse_param(
gleam@string:drop_start(Path, 1),
<<Accum/binary, Grapheme/binary>>
)
end;
{error, _} ->
Accum
end.
-file("/home/ram/code/gleam/pathern/src/pathern/internal/parser.gleam", 101).
-spec first_or_none(list(FCI)) -> gleam@option:option(FCI).
first_or_none(Items) ->
case gleam@list:first(Items) of
{ok, Item} ->
{some, Item};
{error, _} ->
none
end.
-file("/home/ram/code/gleam/pathern/src/pathern/internal/parser.gleam", 108).
-spec find_slash(binary(), integer()) -> {ok, integer()} | {error, nil}.
find_slash(Path, Index) ->
case gleam@string:first(Path) of
{ok, <<"/"/utf8>>} ->
{ok, Index};
{ok, _} ->
find_slash(gleam@string:drop_start(Path, 1), Index + 1);
{error, _} ->
{error, nil}
end.
-file("/home/ram/code/gleam/pathern/src/pathern/internal/parser.gleam", 142).
-spec match_literal_grapheme(binary(), binary(), integer()) -> {ok, nil} |
{error, nil}.
match_literal_grapheme(Grapheme, Literal, Checking_index) ->
case gleam@string:first(gleam@string:drop_start(Literal, Checking_index)) of
{ok, Literal_grapheme} when Literal_grapheme =:= Grapheme ->
{ok, nil};
{ok, _} ->
{error, nil};
{error, _} ->
{error, nil}
end.
-file("/home/ram/code/gleam/pathern/src/pathern/internal/parser.gleam", 116).
-spec find_literal(binary(), binary(), integer(), integer()) -> {ok, integer()} |
{error, nil}.
find_literal(Path, Literal, Checking_index, Index) ->
Literal_length = string:length(Literal),
case gleam@string:first(Path) of
{ok, Grapheme} ->
case match_literal_grapheme(Grapheme, Literal, Checking_index) of
{ok, _} when Literal_length =:= (Checking_index + 1) ->
{ok, Index};
{ok, _} ->
find_literal(
gleam@string:drop_start(Path, 1),
Literal,
Checking_index + 1,
Index
);
{error, _} ->
find_literal(
gleam@string:drop_start(Path, 1),
Literal,
0,
Index + 1
)
end;
{error, _} ->
{error, nil}
end.
-file("/home/ram/code/gleam/pathern/src/pathern/internal/parser.gleam", 42).
-spec parse_token(
pathern@internal@lexer:token(),
gleam@option:option(pathern@internal@lexer:token()),
binary(),
gleam@dict:dict(binary(), binary())
) -> {ok, {gleam@dict:dict(binary(), binary()), binary()}} | {error, nil}.
parse_token(Token, Next_token, Path, Params) ->
case erlang:element(2, Token) of
slash ->
case gleam@string:slice(Path, 0, erlang:element(4, Token)) of
<<""/utf8>> ->
{error, nil};
Val ->
case Val =:= erlang:element(5, Token) of
true ->
{ok,
{Params,
gleam@string:drop_start(
Path,
erlang:element(4, Token)
)}};
false ->
{error, nil}
end
end;
literal ->
case gleam@string:slice(Path, 0, erlang:element(4, Token)) of
<<""/utf8>> ->
{error, nil};
Val ->
case Val =:= erlang:element(5, Token) of
true ->
{ok,
{Params,
gleam@string:drop_start(
Path,
erlang:element(4, Token)
)}};
false ->
{error, nil}
end
end;
param ->
Param = parse_param(Path, <<""/utf8>>),
Params@1 = gleam@dict:insert(
Params,
erlang:element(5, Token),
Param
),
{ok,
{Params@1, gleam@string:drop_start(Path, string:length(Param))}};
wildcard ->
case Next_token of
{some, Next_token@1} ->
case erlang:element(2, Next_token@1) of
slash ->
case find_slash(Path, 0) of
{ok, Index} ->
{ok,
{Params,
gleam@string:drop_start(Path, Index)}};
{error, _} ->
{error, nil}
end;
literal ->
case find_literal(
gleam@string:drop_start(Path, 1),
erlang:element(5, Next_token@1),
0,
0
) of
{ok, Index@1} ->
{ok,
{Params,
gleam@string:drop_start(
Path,
Index@1 + 1
)}};
{error, _} ->
{error, nil}
end;
_ ->
{ok, {Params, <<""/utf8>>}}
end;
none ->
{ok, {Params, <<""/utf8>>}}
end;
_ ->
{error, nil}
end.
-file("/home/ram/code/gleam/pathern/src/pathern/internal/parser.gleam", 15).
-spec parse_tokens(
list(pathern@internal@lexer:token()),
binary(),
gleam@dict:dict(binary(), binary())
) -> {ok, gleam@dict:dict(binary(), binary())} | {error, nil}.
parse_tokens(Tokens, Path, Params) ->
case Tokens of
[Token] when erlang:element(2, Token) =:= slash ->
case (Path =:= <<"/"/utf8>>) orelse (Path =:= <<""/utf8>>) of
true ->
{ok, Params};
false ->
{error, nil}
end;
[Head | Rest] ->
case parse_token(Head, first_or_none(Rest), Path, Params) of
{ok, {Params@1, New_path}} ->
parse_tokens(Rest, New_path, Params@1);
{error, _} ->
{error, nil}
end;
[] ->
case gleam@string:is_empty(Path) orelse (Path =:= <<"/"/utf8>>) of
true ->
{ok, Params};
false ->
{error, nil}
end
end.
-file("/home/ram/code/gleam/pathern/src/pathern/internal/parser.gleam", 7).
-spec parse(binary(), list(pathern@internal@lexer:token())) -> {ok,
gleam@dict:dict(binary(), binary())} |
{error, nil}.
parse(Path, Tokens) ->
Params = maps:new(),
parse_tokens(Tokens, Path, Params).