Current section

Files

Jump to
gchess src pgn.erl
Raw

src/pgn.erl

-module(pgn).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).
-export([pop_move/1, split_movetext/1, remove_tags/1]).
-spec pop_move(binary()) -> gleam@option:option({binary(), binary()}).
pop_move(Pgn) ->
case gleam@string:pop_grapheme(Pgn) of
{ok, {Index_first_digit, Rest}} when ((((((((Index_first_digit =:= <<"1"/utf8>>) orelse (Index_first_digit =:= <<"2"/utf8>>)) orelse (Index_first_digit =:= <<"3"/utf8>>)) orelse (Index_first_digit =:= <<"4"/utf8>>)) orelse (Index_first_digit =:= <<"5"/utf8>>)) orelse (Index_first_digit =:= <<"6"/utf8>>)) orelse (Index_first_digit =:= <<"7"/utf8>>)) orelse (Index_first_digit =:= <<"8"/utf8>>)) orelse (Index_first_digit =:= <<"9"/utf8>>) ->
case gleam@string:split_once(Rest, <<"."/utf8>>) of
{error, _} ->
(erlang:error(#{gleam_error => panic,
message => <<"panic expression evaluated"/utf8>>,
module => <<"pgn"/utf8>>,
function => <<"pop_move"/utf8>>,
line => 18}))(<<"Could not parse move index"/utf8>>);
{ok, {_, Rest@1}} ->
Rest@2 = gleam@string:trim(Rest@1),
case gleam@string:split_once(Rest@2, <<" "/utf8>>) of
{ok, {First_ply, Rest@3}} ->
case gleam@string:split_once(Rest@3, <<" "/utf8>>) of
{ok, {Second_ply, Rest@4}} ->
case gleam@string:first(Second_ply) of
{ok, <<"1"/utf8>>} ->
{some, {First_ply, <<""/utf8>>}};
{ok, <<"2"/utf8>>} ->
{some, {First_ply, <<""/utf8>>}};
{ok, <<"3"/utf8>>} ->
{some, {First_ply, <<""/utf8>>}};
{ok, <<"4"/utf8>>} ->
{some, {First_ply, <<""/utf8>>}};
{ok, <<"5"/utf8>>} ->
{some, {First_ply, <<""/utf8>>}};
{ok, <<"6"/utf8>>} ->
{some, {First_ply, <<""/utf8>>}};
{ok, <<"7"/utf8>>} ->
{some, {First_ply, <<""/utf8>>}};
{ok, <<"8"/utf8>>} ->
{some, {First_ply, <<""/utf8>>}};
{ok, <<"9"/utf8>>} ->
{some, {First_ply, <<""/utf8>>}};
{error, _} ->
{some, {First_ply, <<""/utf8>>}};
_ ->
{some,
{<<<<First_ply/binary,
" "/utf8>>/binary,
Second_ply/binary>>,
Rest@4}}
end;
{error, _} ->
{some,
{<<<<First_ply/binary, " "/utf8>>/binary,
Rest@3/binary>>,
<<""/utf8>>}}
end;
{error, _} ->
{some, {Rest@2, <<""/utf8>>}}
end
end;
{error, _} ->
none;
_ ->
(erlang:error(#{gleam_error => panic,
message => <<"panic expression evaluated"/utf8>>,
module => <<"pgn"/utf8>>,
function => <<"pop_move"/utf8>>,
line => 53}))(<<"could not parse move"/utf8>>)
end.
-spec split_movetext(binary()) -> list(binary()).
split_movetext(Pgn) ->
case pop_move(Pgn) of
{some, {Move, Rest}} ->
gleam@list:append([Move], split_movetext(Rest));
none ->
[]
end.
-spec remove_tags(binary()) -> binary().
remove_tags(Pgn) ->
case gleam@string:pop_grapheme(Pgn) of
{ok, {<<"["/utf8>>, Rest}} ->
case gleam@string:split_once(Rest, <<"]"/utf8>>) of
{error, _} ->
(erlang:error(#{gleam_error => panic,
message => <<"panic expression evaluated"/utf8>>,
module => <<"pgn"/utf8>>,
function => <<"remove_tags"/utf8>>,
line => 61}))(<<"Invalid PGN"/utf8>>);
{ok, {_, Rest@1}} ->
remove_tags(Rest@1)
end;
_ ->
Pgn
end.