File: hierarchy.ex

package info (click to toggle)
elixir-makeup 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 384 kB
  • sloc: javascript: 24; makefile: 9
file content (45 lines) | stat: -rw-r--r-- 1,156 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
defmodule Makeup.Token.Utils.Hierarchy do
  @moduledoc false

  def hierarchy_to_precedence(hierarchy) do
    hierarchy
    |> Enum.map(&dependencies/1)
    |> List.flatten
    |> Enum.reverse
  end

  def node_tag({tag, _, _}), do: tag
  def node_tag({tag, _}), do: tag

  defp descendants({_, _, children}) do
    first_degree = Enum.map(children, &node_tag/1)
    higher_degree = children |> Enum.map(&descendants/1)
    (first_degree ++ higher_degree) |> List.flatten
  end
  defp descendants(_terminal), do: []

  defp dependencies({tag, _, children} = node) do
    node_dependencies = {tag, descendants(node)}
    children_dependencies = children
      |> Enum.map(&dependencies/1)
      |> List.flatten

    [node_dependencies | children_dependencies]
  end
  defp dependencies(_terminal), do: []

  def to_nested_list_of_pairs({tag, class, children}) do
    [{tag, class} | Enum.map(children, &to_nested_list_of_pairs/1)]
  end
  def to_nested_list_of_pairs({tag, class}) do
    {tag, class}
  end

  def style_to_class_map(hierarchy) do
    hierarchy
    |> Enum.map(&to_nested_list_of_pairs/1)
    |> List.flatten
    |> Enum.into(%{})
  end

end