Skip to content

Instantly share code, notes, and snippets.

@jswanner
Created December 1, 2023 06:35
Show Gist options
  • Save jswanner/02387f5cc3a748985a9beb4d862cbb0d to your computer and use it in GitHub Desktop.
Save jswanner/02387f5cc3a748985a9beb4d862cbb0d to your computer and use it in GitHub Desktop.
defmodule Replacer do
import NimbleParsec
one = string("o") |> lookahead(string("ne")) |> replace("1")
two = string("t") |> lookahead(string("wo")) |> replace("2")
three = string("t") |> lookahead(string("hree")) |> replace("3")
four = string("f") |> lookahead(string("our")) |> replace("4")
five = string("f") |> lookahead(string("ive")) |> replace("5")
six = string("s") |> lookahead(string("ix")) |> replace("6")
seven = string("s") |> lookahead(string("even")) |> replace("7")
eight = string("e") |> lookahead(string("ight")) |> replace("8")
nine = string("n") |> lookahead(string("ine")) |> replace("9")
defparsec(
:replace,
repeat(
choice([
one,
two,
three,
four,
five,
six,
seven,
eight,
nine,
utf8_string([], 1)
])
),
debug: true
)
end
defp replace__0(rest, acc, stack, context, line, offset) do
replace__2(rest, [], [{rest, acc, context, line, offset} | stack], context, line, offset)
end
defp replace__2(rest, acc, stack, context, line, offset) do
replace__55(rest, [], [{rest, context, line, offset}, acc | stack], context, line, offset)
end
defp replace__4(<<x0::utf8, rest::binary>>, acc, stack, context, comb__line, comb__offset) do
replace__5(
rest,
[<<x0::utf8>>] ++ acc,
stack,
context,
(
line = comb__line
case x0 do
10 -> {elem(line, 0) + 1, comb__offset + byte_size(<<x0::utf8>>)}
_ -> line
end
),
comb__offset + byte_size(<<x0::utf8>>)
)
end
defp replace__4(rest, _acc, stack, context, line, offset) do
[_, acc | stack] = stack
replace__1(rest, acc, stack, context, line, offset)
end
defp replace__5(rest, acc, [_, previous_acc | stack], context, line, offset) do
replace__3(rest, acc ++ previous_acc, stack, context, line, offset)
end
defp replace__6(_, _, [{rest, context, line, offset} | _] = stack, _, _, _) do
replace__4(rest, [], stack, context, line, offset)
end
defp replace__7(rest, acc, stack, context, line, offset) do
replace__8(rest, [], [acc | stack], context, line, offset)
end
defp replace__8(<<"n", rest::binary>>, acc, stack, context, comb__line, comb__offset) do
replace__9(rest, acc, stack, context, comb__line, comb__offset + 1)
end
defp replace__8(rest, _acc, stack, context, line, offset) do
[acc | stack] = stack
replace__6(rest, acc, stack, context, line, offset)
end
defp replace__9(<<"ine", _::binary>> = rest, acc, stack, context, line, offset) do
replace__10(rest, acc, stack, context, line, offset)
end
defp replace__9(rest, acc, stack, context, line, offset) do
[acc | stack] = stack
replace__6(rest, acc, stack, context, line, offset)
end
defp replace__10(rest, user_acc, [acc | stack], context, line, offset) do
_ = user_acc
replace__11(rest, ["9"] ++ acc, stack, context, line, offset)
end
defp replace__11(rest, acc, [_, previous_acc | stack], context, line, offset) do
replace__3(rest, acc ++ previous_acc, stack, context, line, offset)
end
defp replace__12(_, _, [{rest, context, line, offset} | _] = stack, _, _, _) do
replace__7(rest, [], stack, context, line, offset)
end
defp replace__13(rest, acc, stack, context, line, offset) do
replace__14(rest, [], [acc | stack], context, line, offset)
end
defp replace__14(<<"e", rest::binary>>, acc, stack, context, comb__line, comb__offset) do
replace__15(rest, acc, stack, context, comb__line, comb__offset + 1)
end
defp replace__14(rest, _acc, stack, context, line, offset) do
[acc | stack] = stack
replace__12(rest, acc, stack, context, line, offset)
end
defp replace__15(<<"ight", _::binary>> = rest, acc, stack, context, line, offset) do
replace__16(rest, acc, stack, context, line, offset)
end
defp replace__15(rest, acc, stack, context, line, offset) do
[acc | stack] = stack
replace__12(rest, acc, stack, context, line, offset)
end
defp replace__16(rest, user_acc, [acc | stack], context, line, offset) do
_ = user_acc
replace__17(rest, ["8"] ++ acc, stack, context, line, offset)
end
defp replace__17(rest, acc, [_, previous_acc | stack], context, line, offset) do
replace__3(rest, acc ++ previous_acc, stack, context, line, offset)
end
defp replace__18(_, _, [{rest, context, line, offset} | _] = stack, _, _, _) do
replace__13(rest, [], stack, context, line, offset)
end
defp replace__19(rest, acc, stack, context, line, offset) do
replace__20(rest, [], [acc | stack], context, line, offset)
end
defp replace__20(<<"s", rest::binary>>, acc, stack, context, comb__line, comb__offset) do
replace__21(rest, acc, stack, context, comb__line, comb__offset + 1)
end
defp replace__20(rest, _acc, stack, context, line, offset) do
[acc | stack] = stack
replace__18(rest, acc, stack, context, line, offset)
end
defp replace__21(<<"even", _::binary>> = rest, acc, stack, context, line, offset) do
replace__22(rest, acc, stack, context, line, offset)
end
defp replace__21(rest, acc, stack, context, line, offset) do
[acc | stack] = stack
replace__18(rest, acc, stack, context, line, offset)
end
defp replace__22(rest, user_acc, [acc | stack], context, line, offset) do
_ = user_acc
replace__23(rest, ["7"] ++ acc, stack, context, line, offset)
end
defp replace__23(rest, acc, [_, previous_acc | stack], context, line, offset) do
replace__3(rest, acc ++ previous_acc, stack, context, line, offset)
end
defp replace__24(_, _, [{rest, context, line, offset} | _] = stack, _, _, _) do
replace__19(rest, [], stack, context, line, offset)
end
defp replace__25(rest, acc, stack, context, line, offset) do
replace__26(rest, [], [acc | stack], context, line, offset)
end
defp replace__26(<<"s", rest::binary>>, acc, stack, context, comb__line, comb__offset) do
replace__27(rest, acc, stack, context, comb__line, comb__offset + 1)
end
defp replace__26(rest, _acc, stack, context, line, offset) do
[acc | stack] = stack
replace__24(rest, acc, stack, context, line, offset)
end
defp replace__27(<<"ix", _::binary>> = rest, acc, stack, context, line, offset) do
replace__28(rest, acc, stack, context, line, offset)
end
defp replace__27(rest, acc, stack, context, line, offset) do
[acc | stack] = stack
replace__24(rest, acc, stack, context, line, offset)
end
defp replace__28(rest, user_acc, [acc | stack], context, line, offset) do
_ = user_acc
replace__29(rest, ["6"] ++ acc, stack, context, line, offset)
end
defp replace__29(rest, acc, [_, previous_acc | stack], context, line, offset) do
replace__3(rest, acc ++ previous_acc, stack, context, line, offset)
end
defp replace__30(_, _, [{rest, context, line, offset} | _] = stack, _, _, _) do
replace__25(rest, [], stack, context, line, offset)
end
defp replace__31(rest, acc, stack, context, line, offset) do
replace__32(rest, [], [acc | stack], context, line, offset)
end
defp replace__32(<<"f", rest::binary>>, acc, stack, context, comb__line, comb__offset) do
replace__33(rest, acc, stack, context, comb__line, comb__offset + 1)
end
defp replace__32(rest, _acc, stack, context, line, offset) do
[acc | stack] = stack
replace__30(rest, acc, stack, context, line, offset)
end
defp replace__33(<<"ive", _::binary>> = rest, acc, stack, context, line, offset) do
replace__34(rest, acc, stack, context, line, offset)
end
defp replace__33(rest, acc, stack, context, line, offset) do
[acc | stack] = stack
replace__30(rest, acc, stack, context, line, offset)
end
defp replace__34(rest, user_acc, [acc | stack], context, line, offset) do
_ = user_acc
replace__35(rest, ["5"] ++ acc, stack, context, line, offset)
end
defp replace__35(rest, acc, [_, previous_acc | stack], context, line, offset) do
replace__3(rest, acc ++ previous_acc, stack, context, line, offset)
end
defp replace__36(_, _, [{rest, context, line, offset} | _] = stack, _, _, _) do
replace__31(rest, [], stack, context, line, offset)
end
defp replace__37(rest, acc, stack, context, line, offset) do
replace__38(rest, [], [acc | stack], context, line, offset)
end
defp replace__38(<<"f", rest::binary>>, acc, stack, context, comb__line, comb__offset) do
replace__39(rest, acc, stack, context, comb__line, comb__offset + 1)
end
defp replace__38(rest, _acc, stack, context, line, offset) do
[acc | stack] = stack
replace__36(rest, acc, stack, context, line, offset)
end
defp replace__39(<<"our", _::binary>> = rest, acc, stack, context, line, offset) do
replace__40(rest, acc, stack, context, line, offset)
end
defp replace__39(rest, acc, stack, context, line, offset) do
[acc | stack] = stack
replace__36(rest, acc, stack, context, line, offset)
end
defp replace__40(rest, user_acc, [acc | stack], context, line, offset) do
_ = user_acc
replace__41(rest, ["4"] ++ acc, stack, context, line, offset)
end
defp replace__41(rest, acc, [_, previous_acc | stack], context, line, offset) do
replace__3(rest, acc ++ previous_acc, stack, context, line, offset)
end
defp replace__42(_, _, [{rest, context, line, offset} | _] = stack, _, _, _) do
replace__37(rest, [], stack, context, line, offset)
end
defp replace__43(rest, acc, stack, context, line, offset) do
replace__44(rest, [], [acc | stack], context, line, offset)
end
defp replace__44(<<"t", rest::binary>>, acc, stack, context, comb__line, comb__offset) do
replace__45(rest, acc, stack, context, comb__line, comb__offset + 1)
end
defp replace__44(rest, _acc, stack, context, line, offset) do
[acc | stack] = stack
replace__42(rest, acc, stack, context, line, offset)
end
defp replace__45(<<"hree", _::binary>> = rest, acc, stack, context, line, offset) do
replace__46(rest, acc, stack, context, line, offset)
end
defp replace__45(rest, acc, stack, context, line, offset) do
[acc | stack] = stack
replace__42(rest, acc, stack, context, line, offset)
end
defp replace__46(rest, user_acc, [acc | stack], context, line, offset) do
_ = user_acc
replace__47(rest, ["3"] ++ acc, stack, context, line, offset)
end
defp replace__47(rest, acc, [_, previous_acc | stack], context, line, offset) do
replace__3(rest, acc ++ previous_acc, stack, context, line, offset)
end
defp replace__48(_, _, [{rest, context, line, offset} | _] = stack, _, _, _) do
replace__43(rest, [], stack, context, line, offset)
end
defp replace__49(rest, acc, stack, context, line, offset) do
replace__50(rest, [], [acc | stack], context, line, offset)
end
defp replace__50(<<"t", rest::binary>>, acc, stack, context, comb__line, comb__offset) do
replace__51(rest, acc, stack, context, comb__line, comb__offset + 1)
end
defp replace__50(rest, _acc, stack, context, line, offset) do
[acc | stack] = stack
replace__48(rest, acc, stack, context, line, offset)
end
defp replace__51(<<"wo", _::binary>> = rest, acc, stack, context, line, offset) do
replace__52(rest, acc, stack, context, line, offset)
end
defp replace__51(rest, acc, stack, context, line, offset) do
[acc | stack] = stack
replace__48(rest, acc, stack, context, line, offset)
end
defp replace__52(rest, user_acc, [acc | stack], context, line, offset) do
_ = user_acc
replace__53(rest, ["2"] ++ acc, stack, context, line, offset)
end
defp replace__53(rest, acc, [_, previous_acc | stack], context, line, offset) do
replace__3(rest, acc ++ previous_acc, stack, context, line, offset)
end
defp replace__54(_, _, [{rest, context, line, offset} | _] = stack, _, _, _) do
replace__49(rest, [], stack, context, line, offset)
end
defp replace__55(rest, acc, stack, context, line, offset) do
replace__56(rest, [], [acc | stack], context, line, offset)
end
defp replace__56(<<"o", rest::binary>>, acc, stack, context, comb__line, comb__offset) do
replace__57(rest, acc, stack, context, comb__line, comb__offset + 1)
end
defp replace__56(rest, _acc, stack, context, line, offset) do
[acc | stack] = stack
replace__54(rest, acc, stack, context, line, offset)
end
defp replace__57(<<"ne", _::binary>> = rest, acc, stack, context, line, offset) do
replace__58(rest, acc, stack, context, line, offset)
end
defp replace__57(rest, acc, stack, context, line, offset) do
[acc | stack] = stack
replace__54(rest, acc, stack, context, line, offset)
end
defp replace__58(rest, user_acc, [acc | stack], context, line, offset) do
_ = user_acc
replace__59(rest, ["1"] ++ acc, stack, context, line, offset)
end
defp replace__59(rest, acc, [_, previous_acc | stack], context, line, offset) do
replace__3(rest, acc ++ previous_acc, stack, context, line, offset)
end
defp replace__1(_, _, [{rest, acc, context, line, offset} | stack], _, _, _) do
replace__60(rest, acc, stack, context, line, offset)
end
defp replace__3(
inner_rest,
inner_acc,
[{rest, acc, context, line, offset} | stack],
inner_context,
inner_line,
inner_offset
) do
_ = {rest, acc, context, line, offset}
replace__2(
inner_rest,
[],
[{inner_rest, inner_acc ++ acc, inner_context, inner_line, inner_offset} | stack],
inner_context,
inner_line,
inner_offset
)
end
defp replace__60(rest, acc, _stack, context, line, offset) do
{:ok, acc, rest, context, line, offset}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment