Current section

Files

Jump to
caffeine_lang src caffeine_lang@frontend@ast.erl
Raw

src/caffeine_lang@frontend@ast.erl

-module(caffeine_lang@frontend@ast).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/caffeine_lang/frontend/ast.gleam").
-export([promote_measurements_file/1, promote_expects_file/1, extendable_kind_to_string/1, build_type_alias_pairs/1, literal_to_string/1, value_to_string/1]).
-export_type([comment/0, parsed/0, validated/0, measurements_file/1, type_alias/0, expects_file/1, extendable/0, extendable_kind/0, measurement_item/0, expectation_type/0, expect_item/0, assumes/0, dependency/0, dependency_kind/0, guarantees/0, duration_literal/0, measured_by/0, struct/0, field/0, value/0, literal/0, match_clause/0, value_extraction/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.
-type comment() :: {line_comment, binary()} |
{section_comment, binary()} |
{doc_comment, binary()}.
-type parsed() :: any().
-type validated() :: any().
-type measurements_file(HBM) :: {measurements_file,
list(type_alias()),
list(extendable()),
list(measurement_item()),
list(comment())} |
{gleam_phantom, HBM}.
-type type_alias() :: {type_alias,
binary(),
caffeine_lang@types:parsed_type(),
list(comment())}.
-type expects_file(HBN) :: {expects_file,
list(extendable()),
list(expect_item()),
list(comment())} |
{gleam_phantom, HBN}.
-type extendable() :: {extendable,
binary(),
extendable_kind(),
struct(),
list(comment())}.
-type extendable_kind() :: extendable_requires | extendable_provides.
-type measurement_item() :: {measurement_item,
binary(),
gleam@option:option(expectation_type()),
list(binary()),
struct(),
struct(),
list(comment())}.
-type expectation_type() :: success_rate_type | time_slice_type.
-type expect_item() :: {expect_item,
binary(),
list(binary()),
gleam@option:option(assumes()),
guarantees(),
list(comment())}.
-type assumes() :: {assumes, list(dependency()), list(comment())}.
-type dependency() :: {dependency, dependency_kind(), binary(), list(comment())}.
-type dependency_kind() :: hard_dep | soft_dep.
-type guarantees() :: {guarantees,
float(),
gleam@option:option(duration_literal()),
duration_literal(),
gleam@option:option(measured_by())}.
-type duration_literal() :: {duration_literal, float(), binary()}.
-type measured_by() :: {measured_by, binary(), struct()}.
-type struct() :: {struct, list(field()), list(comment())}.
-type field() :: {field, binary(), value(), list(comment())}.
-type value() :: {type_value, caffeine_lang@types:parsed_type()} |
{literal_value, literal()}.
-type literal() :: {literal_string, binary()} |
{literal_integer, integer()} |
{literal_float, float()} |
{literal_percentage, float()} |
{literal_duration, float(), binary()} |
literal_true |
literal_false |
{literal_list, list(literal())} |
{literal_struct, list(field()), list(comment())} |
{literal_external_indicator,
binary(),
list(match_clause()),
gleam@option:option(value_extraction())}.
-type match_clause() :: {match_clause, binary(), literal()}.
-type value_extraction() :: {value_extraction,
binary(),
caffeine_lang@types:parsed_type()}.
-file("src/caffeine_lang/frontend/ast.gleam", 65).
?DOC(false).
-spec promote_measurements_file(measurements_file(any())) -> measurements_file(any()).
promote_measurements_file(File) ->
{measurements_file,
erlang:element(2, File),
erlang:element(3, File),
erlang:element(4, File),
erlang:element(5, File)}.
-file("src/caffeine_lang/frontend/ast.gleam", 78).
?DOC(false).
-spec promote_expects_file(expects_file(any())) -> expects_file(any()).
promote_expects_file(File) ->
{expects_file,
erlang:element(2, File),
erlang:element(3, File),
erlang:element(4, File)}.
-file("src/caffeine_lang/frontend/ast.gleam", 110).
?DOC(false).
-spec extendable_kind_to_string(extendable_kind()) -> binary().
extendable_kind_to_string(Kind) ->
case Kind of
extendable_requires ->
<<"Requires"/utf8>>;
extendable_provides ->
<<"Provides"/utf8>>
end.
-file("src/caffeine_lang/frontend/ast.gleam", 275).
?DOC(false).
-spec build_type_alias_pairs(list(type_alias())) -> list({binary(),
caffeine_lang@types:parsed_type()}).
build_type_alias_pairs(Type_aliases) ->
gleam@list:map(
Type_aliases,
fun(Ta) -> {erlang:element(2, Ta), erlang:element(3, Ta)} end
).
-file("src/caffeine_lang/frontend/ast.gleam", 292).
?DOC(false).
-spec literal_to_string(literal()) -> binary().
literal_to_string(Lit) ->
case Lit of
{literal_string, S} ->
<<<<"\""/utf8, S/binary>>/binary, "\""/utf8>>;
{literal_integer, N} ->
gleam@string:inspect(N);
{literal_float, F} ->
gleam@string:inspect(F);
{literal_percentage, F@1} ->
<<(gleam@string:inspect(F@1))/binary, "%"/utf8>>;
{literal_duration, Amount, Unit} ->
<<(gleam@string:inspect(Amount))/binary, Unit/binary>>;
literal_true ->
<<"true"/utf8>>;
literal_false ->
<<"false"/utf8>>;
{literal_list, _} ->
<<"[...]"/utf8>>;
{literal_struct, _, _} ->
<<"{...}"/utf8>>;
{literal_external_indicator, Source, _, _} ->
<<<<"from "/utf8, Source/binary>>/binary, " {...}"/utf8>>
end.
-file("src/caffeine_lang/frontend/ast.gleam", 283).
?DOC(false).
-spec value_to_string(value()) -> binary().
value_to_string(Value) ->
case Value of
{type_value, T} ->
caffeine_lang@types:parsed_type_to_string(T);
{literal_value, Lit} ->
literal_to_string(Lit)
end.