Current section
Files
Jump to
Current section
Files
src/yamleam.erl
-module(yamleam).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/yamleam.gleam").
-export([yaml_bool/1, yaml_int/1, yaml_float/1, yaml_string/1, yaml_list/1, yaml_map/1, parse_raw/1, parse/2, parse_documents_raw/1, parse_documents/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(
" yamleam — a pure-Gleam YAML parser.\n"
"\n"
" Public API. Implementation lives under `src/yamleam/`. See README.md\n"
" and ROADMAP.md for the supported subset, the planned coverage, and\n"
" the design philosophy.\n"
).
-file("src/yamleam.gleam", 27).
-spec yaml_bool(boolean()) -> yamleam@node:yaml_node().
yaml_bool(B) ->
{yaml_bool, B}.
-file("src/yamleam.gleam", 31).
-spec yaml_int(integer()) -> yamleam@node:yaml_node().
yaml_int(I) ->
{yaml_int, I}.
-file("src/yamleam.gleam", 35).
-spec yaml_float(float()) -> yamleam@node:yaml_node().
yaml_float(F) ->
{yaml_float, F}.
-file("src/yamleam.gleam", 39).
-spec yaml_string(binary()) -> yamleam@node:yaml_node().
yaml_string(S) ->
{yaml_string, S}.
-file("src/yamleam.gleam", 43).
-spec yaml_list(list(yamleam@node:yaml_node())) -> yamleam@node:yaml_node().
yaml_list(Items) ->
{yaml_list, Items}.
-file("src/yamleam.gleam", 47).
-spec yaml_map(list({binary(), yamleam@node:yaml_node()})) -> yamleam@node:yaml_node().
yaml_map(Pairs) ->
{yaml_map, Pairs}.
-file("src/yamleam.gleam", 58).
?DOC(
" Parse a YAML source string into a typed `YamlNode` tree.\n"
"\n"
" Returns a `ParseError` for invalid YAML or an `Unsupported` error for\n"
" features yamleam does not yet implement. Multi-document streams are\n"
" rejected; use `parse_documents_raw` for those.\n"
).
-spec parse_raw(binary()) -> {ok, yamleam@node:yaml_node()} |
{error, yamleam@error:yaml_error()}.
parse_raw(Source) ->
case yamleam@lexer:tokenize(Source) of
{error, E} ->
{error, E};
{ok, Lines} ->
yamleam@parser:parse(Lines)
end.
-file("src/yamleam.gleam", 71).
?DOC(
" Parse a YAML source string and run a decoder on the resulting tree.\n"
"\n"
" The decoder is a standard `gleam/dynamic/decode.Decoder(a)`, the same\n"
" type used by `gleam_json.parse`. Decoders written for JSON sources\n"
" can be reused unchanged against YAML sources. Multi-document streams\n"
" are rejected; use `parse_documents` for those.\n"
).
-spec parse(binary(), gleam@dynamic@decode:decoder(FDC)) -> {ok, FDC} |
{error, yamleam@error:yaml_error()}.
parse(Source, Decoder) ->
case parse_raw(Source) of
{error, E} ->
{error, E};
{ok, Tree} ->
Dyn = yamleam@decoder:to_dynamic(Tree),
case gleam@dynamic@decode:run(Dyn, Decoder) of
{ok, Value} ->
{ok, Value};
{error, Errors} ->
{error, {decode_error, Errors}}
end
end.
-file("src/yamleam.gleam", 94).
?DOC(
" Parse a YAML stream containing one or more documents into a list of\n"
" `YamlNode` trees. Documents are separated by `---` (start) or `...`\n"
" (end) markers as defined in YAML 1.2 §9.\n"
"\n"
" A stream with no markers and a single document returns `[node]`.\n"
" An empty stream returns `[]`.\n"
"\n"
" Use `parse_documents` if you want each document run through a decoder.\n"
).
-spec parse_documents_raw(binary()) -> {ok, list(yamleam@node:yaml_node())} |
{error, yamleam@error:yaml_error()}.
parse_documents_raw(Source) ->
case yamleam@lexer:tokenize(Source) of
{error, E} ->
{error, E};
{ok, Lines} ->
yamleam@parser:parse_all(Lines)
end.
-file("src/yamleam.gleam", 114).
-spec decode_each(
list(yamleam@node:yaml_node()),
gleam@dynamic@decode:decoder(FDP),
list(FDP)
) -> {ok, list(FDP)} | {error, yamleam@error:yaml_error()}.
decode_each(Trees, Decoder, Acc) ->
case Trees of
[] ->
{ok, lists:reverse(Acc)};
[Tree | Rest] ->
Dyn = yamleam@decoder:to_dynamic(Tree),
case gleam@dynamic@decode:run(Dyn, Decoder) of
{error, Errors} ->
{error, {decode_error, Errors}};
{ok, Value} ->
decode_each(Rest, Decoder, [Value | Acc])
end
end.
-file("src/yamleam.gleam", 104).
?DOC(
" Parse a YAML stream containing one or more documents and run the\n"
" given decoder against each document. Returns a list of decoded values\n"
" in document order.\n"
).
-spec parse_documents(binary(), gleam@dynamic@decode:decoder(FDJ)) -> {ok,
list(FDJ)} |
{error, yamleam@error:yaml_error()}.
parse_documents(Source, Decoder) ->
case parse_documents_raw(Source) of
{error, E} ->
{error, E};
{ok, Trees} ->
decode_each(Trees, Decoder, [])
end.