hierarchy.ex (1156B)
1 defmodule Makeup.Token.Utils.Hierarchy do 2 @moduledoc false 3 4 def hierarchy_to_precedence(hierarchy) do 5 hierarchy 6 |> Enum.map(&dependencies/1) 7 |> List.flatten 8 |> Enum.reverse 9 end 10 11 def node_tag({tag, _, _}), do: tag 12 def node_tag({tag, _}), do: tag 13 14 defp descendants({_, _, children}) do 15 first_degree = Enum.map(children, &node_tag/1) 16 higher_degree = children |> Enum.map(&descendants/1) 17 (first_degree ++ higher_degree) |> List.flatten 18 end 19 defp descendants(_terminal), do: [] 20 21 defp dependencies({tag, _, children} = node) do 22 node_dependencies = {tag, descendants(node)} 23 children_dependencies = children 24 |> Enum.map(&dependencies/1) 25 |> List.flatten 26 27 [node_dependencies | children_dependencies] 28 end 29 defp dependencies(_terminal), do: [] 30 31 def to_nested_list_of_pairs({tag, class, children}) do 32 [{tag, class} | Enum.map(children, &to_nested_list_of_pairs/1)] 33 end 34 def to_nested_list_of_pairs({tag, class}) do 35 {tag, class} 36 end 37 38 def style_to_class_map(hierarchy) do 39 hierarchy 40 |> Enum.map(&to_nested_list_of_pairs/1) 41 |> List.flatten 42 |> Enum.into(%{}) 43 end 44 45 end