-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(GUY, GUZ, GVA, GVB) :: {parser, fun((state(GVA, GVB)) -> step(GUY, GUZ, GVA, GVB))}. -type step(GVC, GVD, GVE, GVF) :: {cont, committed(), GVC, state(GVE, GVF)} | {fail, committed(), error(GVD, GVE), chomp@span:span(), list({chomp@span:span(), GVF})}. -type state(GVG, GVH) :: {state, gleam@dict:dict(integer(), chomp@lexer:token(GVG)), integer(), chomp@span:span(), list({chomp@span:span(), GVH})}. -type committed() :: {committed, boolean()}. -type error(GVI, GVJ) :: {custom, GVI} | end_of_input | {expected, GVJ, GVJ} | {unexpected, GVJ} | {bad_parser, binary()}. -type loop(GVK, GVL) :: {continue, GVL} | {break, GVK}. -spec runwrap(state(GWB, GWC), parser(GWF, GWG, GWB, GWC)) -> step(GWF, GWG, GWB, GWC). runwrap(State, Parser) -> {parser, Parse} = Parser, Parse(State). -spec run(list(chomp@lexer:token(GVM)), parser(GVP, GVQ, GVM, GVR)) -> {ok, GVP} | {error, {error(GVQ, GVM), chomp@span:span(), list({chomp@span:span(), GVR})}}. 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(GWP, GWQ)) -> {gleam@option:option(GWP), state(GWP, GWQ)}. 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(GWW) -> parser(GWW, any(), any(), any()). return(Value) -> {parser, fun(State) -> {cont, {committed, false}, Value, State} end}. -spec succeed(GXE) -> parser(GXE, any(), any(), any()). succeed(Value) -> return(Value). -spec throw(GXM) -> parser(any(), GXM, 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(GXU) -> parser(any(), GXU, any(), any()). fail(Error) -> throw(Error). -spec lazy(fun(() -> parser(GYC, GYD, GYE, GYF))) -> parser(GYC, GYD, GYE, GYF). lazy(Parser) -> {parser, fun(State) -> runwrap(State, Parser()) end}. -spec backtrackable(parser(GYO, GYP, GYQ, GYR)) -> parser(GYO, GYP, GYQ, GYR). 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(GZA, GZB, GZC, GZD), fun((GZA) -> parser(GZI, GZB, GZC, GZD))) -> parser(GZI, GZB, GZC, GZD). 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(HAI, HAJ, HAK, HAL), fun((HAI) -> parser(HAQ, HAJ, HAK, HAL))) -> parser(HAQ, HAJ, HAK, HAL). then(Parser, F) -> do(Parser, F). -spec map(parser(HAZ, HBA, HBB, HBC), fun((HAZ) -> HBH)) -> parser(HBH, HBA, HBB, HBC). map(Parser, F) -> do(Parser, fun(A) -> return(F(A)) end). -spec replace(parser(any(), HBN, HBO, HBP), HBU) -> parser(HBU, HBN, HBO, HBP). replace(Parser, B) -> map(Parser, fun(_) -> B end). -spec or_error(parser(HBZ, HCA, HCB, HCC), HCA) -> parser(HBZ, HCA, HCB, HCC). 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(HCL, HCM, HCN, HCO), HCM) -> parser(HCL, HCM, HCN, HCO). 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(HDS) -> parser(chomp@span:span(), any(), HDS, 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(HEG, HEH, HEI, HEJ))) -> parser(HEG, HEH, HEI, HEJ). 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(HET, HEU, HEV, HEW), HET) -> parser(HET, HEU, HEV, HEW). 'or'(Parser, Default) -> one_of([Parser, return(Default)]). -spec optional(parser(HFF, HFG, HFH, HFI)) -> parser(gleam@option:option(HFF), HFG, HFH, HFI). optional(Parser) -> one_of([map(Parser, fun(Field@0) -> {some, Field@0} end), return(none)]). -spec loop_help( fun((ICN) -> parser(loop(ICZ, ICN), ICS, ICT, ICU)), committed(), ICN, state(ICT, ICU) ) -> step(ICZ, ICS, ICT, ICU). 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(HJC, fun((HJC) -> parser(loop(HJD, HJC), HJG, HJH, HJI))) -> parser(HJD, HJG, HJH, HJI). loop(Init, Step) -> {parser, fun(State) -> loop_help(Step, {committed, false}, Init, State) end}. -spec until_end(parser(HHK, HHL, HHM, HHN)) -> parser(list(HHK), HHL, HHM, HHN). 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(HIK, parser(HIK, HIL, HIM, HIN), parser(any(), HIL, HIM, HIN)) -> parser(list(HIK), HIL, HIM, HIN). 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(HFS, HFT, HFU, HFV), parser(any(), HFT, HFU, HFV)) -> parser(list(HFS), HFT, HFU, HFV). sequence(Parser, Sep) -> one_of( [begin _pipe = Parser, then(_pipe, fun(_capture) -> more(_capture, Parser, Sep) end) end, return([])] ). -spec many(parser(HGK, HGL, HGM, HGN)) -> parser(list(HGK), HGL, HGM, HGN). many(Parser) -> sequence(Parser, return(nil)). -spec many1(parser(HGX, HGY, HGZ, HHA)) -> parser(list(HGX), HGY, HGZ, HHA). many1(Parser) -> do(Parser, fun(X) -> do(many(Parser), fun(Xs) -> return([X | Xs]) end) end). -spec take_if(fun((HJW) -> boolean())) -> parser(HJW, any(), HJW, 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(HDL, any(), HDL, any()). any() -> take_if(fun(_) -> true end). -spec take_map(fun((HKD) -> gleam@option:option(HKE))) -> parser(HKE, any(), HKD, 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(HHX, HHY, HHZ, HIA), HHZ) -> parser(list(HHX), HHY, HHZ, HIA). 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(HKY, HKZ), HKZ) -> state(HKY, HKZ). push_context(State, Context) -> erlang:setelement( 5, State, [{erlang:element(4, State), Context} | erlang:element(5, State)] ). -spec pop_context(state(HLE, HLF)) -> state(HLE, HLF). pop_context(State) -> case erlang:element(5, State) of [] -> State; [_ | Context] -> erlang:setelement(5, State, Context) end. -spec in(parser(HKM, HKN, HKO, HKP), HKP) -> parser(HKM, HKN, HKO, HKP). 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( GZR, parser(GZS, GZT, GZU, GZR), fun((GZS) -> parser(GZZ, GZT, GZU, GZR)) ) -> parser(GZZ, GZT, GZU, GZR). do_in(Context, Parser, F) -> _pipe = do(Parser, F), in(_pipe, Context). -spec inspect(parser(HLK, HLL, HLM, HLN), binary()) -> parser(HLK, HLL, HLM, HLN). inspect(Parser, Message) -> {parser, fun(State) -> gleam@io:println(<>), _pipe = runwrap(State, Parser), gleam@io:debug(_pipe) end}.