-module(chomp). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([run/2, return/1, succeed/1, throw/1, fail/1, lazy/1, backtrackable/1, do/2, then/2, map/2, replace/2, or_error/2, replace_error/2, get_pos/0, is_at_end/0, token/1, 'end'/0, one_of/1, 'or'/2, optional/1, loop/2, until_end/1, sequence/2, many/1, many1/1, take_if/1, any/0, take_map/1, until/2, in/2, do_in/3, inspect/2]). -export_type([parser/4, step/4, state/2, committed/0, error/2, loop/2]). -opaque parser(GVA, GVB, GVC, GVD) :: {parser, fun((state(GVC, GVD)) -> step(GVA, GVB, GVC, GVD))}. -type step(GVE, GVF, GVG, GVH) :: {cont, committed(), GVE, state(GVG, GVH)} | {fail, committed(), error(GVF, GVG), chomp@span:span(), list({chomp@span:span(), GVH})}. -type state(GVI, GVJ) :: {state, gleam@dict:dict(integer(), chomp@lexer:token(GVI)), integer(), chomp@span:span(), list({chomp@span:span(), GVJ})}. -type committed() :: {committed, boolean()}. -type error(GVK, GVL) :: {custom, GVK} | end_of_input | {expected, GVL, GVL} | {unexpected, GVL} | {bad_parser, binary()}. -type loop(GVM, GVN) :: {continue, GVN} | {break, GVM}. -spec runwrap(state(GWD, GWE), parser(GWH, GWI, GWD, GWE)) -> step(GWH, GWI, GWD, GWE). runwrap(State, Parser) -> {parser, Parse} = Parser, Parse(State). -spec run(list(chomp@lexer:token(GVO)), parser(GVR, GVS, GVO, GVT)) -> {ok, GVR} | {error, {error(GVS, GVO), chomp@span:span(), list({chomp@span:span(), GVT})}}. run(Src, Parser) -> Src@1 = gleam@list:index_fold( Src, gleam@dict:new(), fun(Dict, Tok, Idx) -> gleam@dict:insert(Dict, Idx, Tok) end ), Init = {state, Src@1, 0, {span, 1, 1, 1, 1}, []}, case runwrap(Init, Parser) of {cont, _, A, _} -> {ok, A}; {fail, _, Problem, Pos, Ctx} -> {error, {Problem, Pos, Ctx}} end. -spec next(state(GWR, GWS)) -> {gleam@option:option(GWR), state(GWR, GWS)}. next(State) -> case gleam@dict:get(erlang:element(2, State), erlang:element(3, State)) of {error, _} -> {none, State}; {ok, {token, Span, _, Tok}} -> {{some, Tok}, erlang:setelement( 4, erlang:setelement(3, State, erlang:element(3, State) + 1), Span )} end. -spec return(GWY) -> parser(GWY, any(), any(), any()). return(Value) -> {parser, fun(State) -> {cont, {committed, false}, Value, State} end}. -spec succeed(GXG) -> parser(GXG, any(), any(), any()). succeed(Value) -> return(Value). -spec throw(GXO) -> parser(any(), GXO, any(), any()). throw(Error) -> {parser, fun(State) -> Error@1 = {custom, Error}, {fail, {committed, false}, Error@1, erlang:element(4, State), erlang:element(5, State)} end}. -spec fail(GXW) -> parser(any(), GXW, any(), any()). fail(Error) -> throw(Error). -spec lazy(fun(() -> parser(GYE, GYF, GYG, GYH))) -> parser(GYE, GYF, GYG, GYH). lazy(Parser) -> {parser, fun(State) -> runwrap(State, Parser()) end}. -spec backtrackable(parser(GYQ, GYR, GYS, GYT)) -> parser(GYQ, GYR, GYS, GYT). backtrackable(Parser) -> {parser, fun(State) -> case runwrap(State, Parser) of {cont, _, A, State@1} -> {cont, {committed, false}, A, State@1}; {fail, _, Problem, Pos, Ctx} -> {fail, {committed, false}, Problem, Pos, Ctx} end end}. -spec should_commit(committed(), committed()) -> committed(). should_commit(A, B) -> {committed, A@1} = A, {committed, B@1} = B, {committed, A@1 orelse B@1}. -spec do(parser(GZC, GZD, GZE, GZF), fun((GZC) -> parser(GZK, GZD, GZE, GZF))) -> parser(GZK, GZD, GZE, GZF). do(Parser, F) -> {parser, fun(State) -> case runwrap(State, Parser) of {cont, To_a, A, State@1} -> case runwrap(State@1, F(A)) of {cont, To_b, B, State@2} -> {cont, should_commit(To_a, To_b), B, State@2}; {fail, To_b@1, Problem, Pos, Ctx} -> {fail, should_commit(To_a, To_b@1), Problem, Pos, Ctx} end; {fail, Committed, Problem@1, Pos@1, Ctx@1} -> {fail, Committed, Problem@1, Pos@1, Ctx@1} end end}. -spec then(parser(HAK, HAL, HAM, HAN), fun((HAK) -> parser(HAS, HAL, HAM, HAN))) -> parser(HAS, HAL, HAM, HAN). then(Parser, F) -> do(Parser, F). -spec map(parser(HBB, HBC, HBD, HBE), fun((HBB) -> HBJ)) -> parser(HBJ, HBC, HBD, HBE). map(Parser, F) -> do(Parser, fun(A) -> return(F(A)) end). -spec replace(parser(any(), HBP, HBQ, HBR), HBW) -> parser(HBW, HBP, HBQ, HBR). replace(Parser, B) -> map(Parser, fun(_) -> B end). -spec or_error(parser(HCB, HCC, HCD, HCE), HCC) -> parser(HCB, HCC, HCD, HCE). or_error(Parser, Error) -> {parser, fun(State) -> case runwrap(State, Parser) of {fail, Committed, end_of_input, Pos, Ctx} -> {fail, Committed, end_of_input, Pos, Ctx}; {fail, {committed, false}, _, Pos@1, Ctx@1} -> {fail, {committed, false}, {custom, Error}, Pos@1, Ctx@1}; Result -> Result end end}. -spec replace_error(parser(HCN, HCO, HCP, HCQ), HCO) -> parser(HCN, HCO, HCP, HCQ). replace_error(Parser, Error) -> {parser, fun(State) -> case runwrap(State, Parser) of {fail, Committed, _, Pos, Ctx} -> {fail, Committed, {custom, Error}, Pos, Ctx}; Result -> Result end end}. -spec get_pos() -> parser(chomp@span:span(), any(), any(), any()). get_pos() -> {parser, fun(State) -> {cont, {committed, false}, erlang:element(4, State), State} end}. -spec is_at_end() -> parser(boolean(), any(), any(), any()). is_at_end() -> {parser, fun(State) -> case next(State) of {{some, _}, _} -> {cont, {committed, false}, false, State}; {none, _} -> {cont, {committed, false}, true, State} end end}. -spec token(HDU) -> parser(chomp@span:span(), any(), HDU, any()). token(Tok) -> {parser, fun(State) -> case next(State) of {{some, T}, State@1} when Tok =:= T -> {cont, {committed, true}, erlang:element(4, State@1), State@1}; {{some, T@1}, State@2} -> {fail, {committed, false}, {expected, Tok, T@1}, erlang:element(4, State@2), erlang:element(5, State@2)}; {none, State@3} -> {fail, {committed, false}, end_of_input, erlang:element(4, State@3), erlang:element(5, State@3)} end end}. -spec 'end'() -> parser(nil, any(), any(), any()). 'end'() -> {parser, fun(State) -> case next(State) of {{some, Tok}, State@1} -> {fail, {committed, false}, {unexpected, Tok}, erlang:element(4, State@1), erlang:element(5, State@1)}; {none, _} -> {cont, {committed, false}, nil, State} end end}. -spec one_of(list(parser(HEI, HEJ, HEK, HEL))) -> parser(HEI, HEJ, HEK, HEL). one_of(Parsers) -> {parser, fun(State) -> case Parsers of [] -> {fail, {committed, false}, {bad_parser, <<"one_of requires at least one parser"/utf8>>}, erlang:element(4, State), erlang:element(5, State)}; [Parser] -> runwrap(State, Parser); [Parser@1 | Rest] -> case runwrap(State, Parser@1) of {cont, _, _, _} = Result -> Result; {fail, {committed, true}, _, _, _} = Result@1 -> Result@1; {fail, _, _, _, _} -> runwrap(State, one_of(Rest)) end end end}. -spec 'or'(parser(HEV, HEW, HEX, HEY), HEV) -> parser(HEV, HEW, HEX, HEY). 'or'(Parser, Default) -> one_of([Parser, return(Default)]). -spec optional(parser(HFH, HFI, HFJ, HFK)) -> parser(gleam@option:option(HFH), HFI, HFJ, HFK). optional(Parser) -> one_of([map(Parser, fun(Field@0) -> {some, Field@0} end), return(none)]). -spec loop_help( fun((ICP) -> parser(loop(IDB, ICP), ICU, ICV, ICW)), committed(), ICP, state(ICV, ICW) ) -> step(IDB, ICU, ICV, ICW). loop_help(F, Commit, Loop_state, State) -> case runwrap(State, F(Loop_state)) of {cont, Can_backtrack, {continue, Next_loop_state}, Next_state} -> loop_help( F, should_commit(Commit, Can_backtrack), Next_loop_state, Next_state ); {cont, Can_backtrack@1, {break, Result}, Next_state@1} -> {cont, should_commit(Commit, Can_backtrack@1), Result, Next_state@1}; {fail, Can_backtrack@2, Problem, Pos, Ctx} -> {fail, should_commit(Commit, Can_backtrack@2), Problem, Pos, Ctx} end. -spec loop(HJE, fun((HJE) -> parser(loop(HJF, HJE), HJI, HJJ, HJK))) -> parser(HJF, HJI, HJJ, HJK). loop(Init, Step) -> {parser, fun(State) -> loop_help(Step, {committed, false}, Init, State) end}. -spec until_end(parser(HHM, HHN, HHO, HHP)) -> parser(list(HHM), HHN, HHO, HHP). until_end(Parser) -> loop([], fun(Xs) -> do(is_at_end(), fun(Is_at_end) -> case Is_at_end of true -> return({break, lists:reverse(Xs)}); false -> do( Parser, fun(X) -> return({continue, [X | Xs]}) end ) end end) end). -spec more(HIM, parser(HIM, HIN, HIO, HIP), parser(any(), HIN, HIO, HIP)) -> parser(list(HIM), HIN, HIO, HIP). more(X, Parser, Separator) -> loop( [X], fun(Xs) -> Break = fun() -> return({break, lists:reverse(Xs)}) end, Continue = (do( Separator, fun(_) -> do(Parser, fun(X@1) -> return({continue, [X@1 | Xs]}) end) end )), one_of([Continue, lazy(Break)]) end ). -spec sequence(parser(HFU, HFV, HFW, HFX), parser(any(), HFV, HFW, HFX)) -> parser(list(HFU), HFV, HFW, HFX). sequence(Parser, Sep) -> one_of( [begin _pipe = Parser, then(_pipe, fun(_capture) -> more(_capture, Parser, Sep) end) end, return([])] ). -spec many(parser(HGM, HGN, HGO, HGP)) -> parser(list(HGM), HGN, HGO, HGP). many(Parser) -> sequence(Parser, return(nil)). -spec many1(parser(HGZ, HHA, HHB, HHC)) -> parser(list(HGZ), HHA, HHB, HHC). many1(Parser) -> do(Parser, fun(X) -> do(many(Parser), fun(Xs) -> return([X | Xs]) end) end). -spec take_if(fun((HJY) -> boolean())) -> parser(HJY, any(), HJY, any()). take_if(Predicate) -> {parser, fun(State) -> {Tok, State@1} = next(State), case {Tok, gleam@option:map(Tok, Predicate)} of {{some, Tok@1}, {some, true}} -> {cont, {committed, true}, Tok@1, State@1}; {{some, Tok@2}, {some, false}} -> {fail, {committed, false}, {unexpected, Tok@2}, erlang:element(4, State@1), erlang:element(5, State@1)}; {_, _} -> {fail, {committed, false}, end_of_input, erlang:element(4, State@1), erlang:element(5, State@1)} end end}. -spec any() -> parser(HDN, any(), HDN, any()). any() -> take_if(fun(_) -> true end). -spec take_map(fun((HKF) -> gleam@option:option(HKG))) -> parser(HKG, any(), HKF, any()). take_map(F) -> {parser, fun(State) -> {Tok, State@1} = next(State), case {Tok, gleam@option:then(Tok, F)} of {none, _} -> {fail, {committed, false}, end_of_input, erlang:element(4, State@1), erlang:element(5, State@1)}; {{some, Tok@1}, none} -> {fail, {committed, false}, {unexpected, Tok@1}, erlang:element(4, State@1), erlang:element(5, State@1)}; {_, {some, A}} -> {cont, {committed, true}, A, State@1} end end}. -spec until(parser(HHZ, HIA, HIB, HIC), HIB) -> parser(list(HHZ), HIA, HIB, HIC). until(Parser, Tok) -> loop( [], fun(Xs) -> Break = take_map(fun(T) -> case T =:= Tok of true -> {some, {break, lists:reverse(Xs)}}; false -> none end end), Continue = (do(Parser, fun(X) -> return({continue, [X | Xs]}) end)), one_of([Break, Continue]) end ). -spec push_context(state(HLA, HLB), HLB) -> state(HLA, HLB). push_context(State, Context) -> erlang:setelement( 5, State, [{erlang:element(4, State), Context} | erlang:element(5, State)] ). -spec pop_context(state(HLG, HLH)) -> state(HLG, HLH). pop_context(State) -> case erlang:element(5, State) of [] -> State; [_ | Context] -> erlang:setelement(5, State, Context) end. -spec in(parser(HKO, HKP, HKQ, HKR), HKR) -> parser(HKO, HKP, HKQ, HKR). in(Parser, Context) -> {parser, fun(State) -> case runwrap(push_context(State, Context), Parser) of {cont, Committed, A, State@1} -> {cont, Committed, A, pop_context(State@1)}; {fail, Committed@1, Problem, Pos, Ctx} -> {fail, Committed@1, Problem, Pos, Ctx} end end}. -spec do_in( GZT, parser(GZU, GZV, GZW, GZT), fun((GZU) -> parser(HAB, GZV, GZW, GZT)) ) -> parser(HAB, GZV, GZW, GZT). do_in(Context, Parser, F) -> _pipe = do(Parser, F), in(_pipe, Context). -spec inspect(parser(HLM, HLN, HLO, HLP), binary()) -> parser(HLM, HLN, HLO, HLP). inspect(Parser, Message) -> {parser, fun(State) -> gleam@io:println(<>), _pipe = runwrap(State, Parser), gleam@io:debug(_pipe) end}.