fork download
  1. defmodule Parser do
  2. def run(str), do: IO.inspect do_parse(str, [])
  3.  
  4. defp do_parse("", agg), do: Enum.reverse(agg)
  5. defp do_parse("<" <> rest, agg), do: in_tag(rest, agg)
  6. defp do_parse(<<_, rest::binary>>, agg), do: do_parse(rest, agg)
  7.  
  8. defp in_tag(data, curr \\ "", agg)
  9. defp in_tag(">" <> rest, curr, agg), do: do_parse(rest, [curr | agg])
  10. defp in_tag(<<c, rest::binary>>, curr, agg), do: in_tag(rest, curr <> <<c>>, agg)
  11. end
  12.  
  13. Parser.run("ala ma <kota> i <psa> hey") # ["kota", "psa"]
  14. Parser.run("<ala>ma</kota>") # ["ala", "/kota"]
  15. Parser.run("<a<b>c>") # ["a<b"]
  16. Parser.run(">a>b<c>d<e<f>") # ["c", "e<f"]
  17.  
Success #stdin #stdout 0.29s 35732KB
stdin
Standard input is empty
stdout
["kota", "psa"]
["ala", "/kota"]
["a<b"]
["c", "e<f"]