expug v0.9.2 Expug.Compiler View Source

Compiles tokens into an AST.

How it works

Nodes are maps with a :type key. They are then filled up using a function with the same name as the type:

node = %{type: :document}
document({node, tokens})

This function returns another {node, tokens} tuple, where node is the updated node, and tokens are the rest of the tokens to parse.

The functions (document/1) here can do 1 of these things:

  • Spawn a child, say, %{type: :element}, then delegate to its function (eg, element()).
  • Simply return a {node, tokens} - no transformation here.

The functions indent() and statement() are a little different. It can give you an element, or a text node, or whatever.

Also see

Link to this section Summary

Functions

Adds a child to a Node

Consumes :subindent tokens and adds them to the value of node

Returns a list of [type: :attribute] items

Compiles tokens. Returns {:ok, ast} on success

A document

Parses an element. Returns a %{type: :element} node

Indentation. Called with depth which is the current level its at

A statement after an :indent. Can consume these

Matches :subindent tokens and discards them. Used for line comments (-#)

Link to this section Functions

Link to this function add_attribute(list, key, value) View Source

Adds a child to a Node.

iex> Expug.Compiler.add_child(%{}, %{type: :a})
%{children: [%{type: :a}]}

iex> src = %{children: [%{type: :a}]}
...> Expug.Compiler.add_child(src, %{type: :b})
%{children: [%{type: :a}, %{type: :b}]}
Link to this function add_element(node, t, tokens, depth) View Source

Consumes :subindent tokens and adds them to the value of node.

Returns a list of [type: :attribute] items.

Link to this function compile(tokens, opts \\ []) View Source

Compiles tokens. Returns {:ok, ast} on success.

On failure, it returns {:error, [type: type, position: {line, col}]}.

A document.

Link to this function element(arg, parent, depths) View Source

Parses an element. Returns a %{type: :element} node.

Indentation. Called with depth which is the current level its at.

A statement after an :indent. Can consume these:

:element_name
:element_class
:element_id
[:attribute_open [...] :attribute_close]
[:buffered_text | :unescaped_text | :raw_text | :block_text]

Matches :subindent tokens and discards them. Used for line comments (-#).

Link to this function subindent_capture(tokens, lines \\ []) View Source