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
Expug.Tokenizer
is used to build the tokens used by this compiler.Expug.Builder
uses the AST made by this compiler.
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
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}]}
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.
On failure, it returns {:error, [type: type, position: {line, col}]}
.
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:
: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 (-#
).