defmodule Zig.Parser.Collected do @literals ~w(integer char float string)a def literals, do: @literals def post_traverse(rest, [string | args_rest], context, _, _, :line_string) do collated = string |> String.split("\n") |> Enum.map_join("\n", fn line -> line |> String.trim() |> String.trim_leading("\\\\") end) {rest, [collated | args_rest], context} end def post_traverse(rest, ["@" <> special_identifier | args_rest], context, _, _, :IDENTIFIER) do id_atom = special_identifier |> String.trim(~S(")) |> String.to_atom() {rest, [id_atom | args_rest], context} end def post_traverse(rest, [collected | args_rest], context, _, _, :IDENTIFIER) do {rest, [String.to_atom(collected) | args_rest], context} end def post_traverse(rest, ["0x" <> hex | args_rest], context, _, _, :INTEGER) do {rest, [{:integer, String.to_integer(remove_underscore(hex), 16)} | args_rest], context} end def post_traverse(rest, ["0o" <> oct | args_rest], context, _, _, :INTEGER) do {rest, [{:integer, String.to_integer(remove_underscore(oct), 8)} | args_rest], context} end def post_traverse(rest, ["0b" <> bin | args_rest], context, _, _, :INTEGER) do {rest, [{:integer, String.to_integer(remove_underscore(bin), 16)} | args_rest], context} end def post_traverse(rest, [integer | args_rest], context, _, _, :INTEGER) do {rest, [{:integer, String.to_integer(remove_underscore(integer))} | args_rest], context} end def post_traverse(rest, [<> | args_rest], context, _, _, :CHAR_LITERAL) do {rest, [{:char, char} | args_rest], context} end def post_traverse(rest, [float_str | args_rest], context, _, _, :FLOAT) do # the zig parser itself should know that this is inherently a float. token = float_str |> remove_underscore() |> Float.parse |> case do {float, ""} -> {:float, float} _ -> {:extended_float, float_str} end {rest, [token | args_rest], context} rescue ArgumentError -> {rest, [{:extended_float, float_str} | args_rest], context} end def post_traverse(rest, [string | args_rest], context, _, _, :STRINGLITERAL) do {rest, [{:string, String.trim(string, ~S("))} | args_rest], context} end def post_traverse(rest, [string | args_rest], context, _, _, :BUILTINIDENTIFIER) do builtin = string |> String.trim_leading("@") |> String.to_atom() {rest, [{:builtin, builtin} | args_rest], context} end defp remove_underscore(string), do: String.replace(string, "_", "") end