snor v0.2.0 Snor.Parser

Convert a string into an intermediate representation - a list of nodes.

A node could be one of (mainly) -

  • A plaintext node
  • A function node
  • A block node

Link to this section Summary

Types

An argument pair

A block node

A function node

An interpolation node

The result of a parse_* operation

A parsed node

A plaintext node

Represents remaining tokens after a node was parsed and extracted

A grapheme from the template that was passed in

Functions

Parse a string into a list of nodes

Parse an argument pair

Parse an interpolation node

Parse an interpolation/block node

Link to this section Types

Link to this type

argument_pair()

argument_pair() :: %{key: String.t(), value: [parsed_node()]}

An argument pair

Link to this type

block_node()

block_node() :: %{
  with_scope: String.t(),
  children: [parsed_node()],
  negative: boolean()
}

A block node

Link to this type

function_node()

function_node() :: %{function: String.t(), arguments: [argument_pair()]}

A function node

Link to this type

interpolation_node()

interpolation_node() :: %{interpolation: String.t()}

An interpolation node

Link to this type

parse_result()

parse_result() ::
  {:error, String.t()} | {:ok, parsed_node(), remaining_tokens()}

The result of a parse_* operation

A parsed node

Link to this type

plaintext_node()

plaintext_node() :: %{plaintext: String.t()}

A plaintext node

Link to this type

remaining_tokens()

remaining_tokens() :: [token()]

Represents remaining tokens after a node was parsed and extracted

A grapheme from the template that was passed in

Link to this section Functions

Link to this function

parse(template)

parse(String.t()) :: [parsed_node()]

Parse a string into a list of nodes

Examples

iex> Snor.Parser.parse("Hello")
[%{plaintext: "Hello"}]

iex> Snor.Parser.parse("{{name}}")
[%{interpolation: "name"}]

iex> Snor.Parser.parse("{{upcase item='Jane'}}")
[%{arguments: [%{key: "item", value: [%{plaintext: "Jane"}]}], function: "upcase"}]

iex> Snor.Parser.parse("{{#person}}{{name}}{{/person}}")
[%{children: [%{interpolation: "name"}, %{plaintext: ""}], negative: false, with_scope: "person"}]
Link to this function

parse_argument(tokens, nodes)

parse_argument([token()], [parsed_node()]) ::
  {:ok, argument_pair(), remaining_tokens()}

Parse an argument pair

Link to this function

parse_block(tokens, nodes)

parse_block([token()], [parsed_node()]) :: parse_result()

Parse a block

Link to this function

parse_interpolation(tokens, nodes)

parse_interpolation([token()], [parsed_node()]) :: parse_result()

Parse an interpolation node

Link to this function

parse_node(tokens, nodes)

parse_node([token()], [parsed_node()]) ::
  {:error, String.t()} | {:ok, parsed_node(), remaining_tokens()}

Parse an interpolation/block node