Mau.Parser (mau v0.8.0)
View SourceParser for the Mau template engine using NimbleParsec.
Handles parsing of template strings into AST nodes. Supports plain text parsing and string literals with escape sequences.
Summary
Functions
Parses the given binary as additive_expression.
Parses the given binary as logical_or_expression.
Parses a template string into an AST.
Parses a boolean literal and returns a clean result.
Parses the given binary as parse_boolean_literal_raw.
Parses an expression block and returns a clean result.
Parses the given binary as parse_expression_block_raw.
Parses an identifier and returns a clean result.
Parses the given binary as parse_identifier_raw.
Parses a null literal and returns a clean result.
Parses the given binary as parse_null_literal_raw.
Parses a number literal and returns a clean result.
Parses the given binary as parse_number_literal_raw.
Parses a string literal and returns a clean result.
Parses the given binary as parse_string_literal_raw.
Parses the given binary as parse_template.
Parses a variable path and returns a clean result.
Parses the given binary as parse_variable_path_raw.
Parses the given binary as unary_expression.
Functions
@spec additive_expression(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as additive_expression.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the additive_expression (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
@spec logical_or_expression(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as logical_or_expression.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the logical_or_expression (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
Parses a template string into an AST.
Now handles mixed content: text and expression blocks.
Examples
iex> Mau.Parser.parse("Hello world")
{:ok, [{:text, ["Hello world"], []}]}
iex> Mau.Parser.parse("")
{:ok, []}
iex> Mau.Parser.parse(~s(Hello {{ "world" }}!))
{:ok, [{:text, ["Hello "], []}, {:expression, [{:literal, ["world"], []}], []}, {:text, ["!"], []}]}
Parses a boolean literal and returns a clean result.
Examples
iex> Mau.Parser.parse_boolean_literal("true")
{:ok, {:literal, [true], []}}
iex> Mau.Parser.parse_boolean_literal("false")
{:ok, {:literal, [false], []}}
@spec parse_boolean_literal_raw(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as parse_boolean_literal_raw.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the parse_boolean_literal_raw (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
Parses an expression block and returns a clean result.
Examples
iex> Mau.Parser.parse_expression_block(~s({{ "hello" }}))
{:ok, {:expression, [{:literal, ["hello"], []}], []}}
iex> Mau.Parser.parse_expression_block("{{42}}")
{:ok, {:expression, [{:literal, [42], []}], []}}
iex> Mau.Parser.parse_expression_block("{{ true }}")
{:ok, {:expression, [{:literal, [true], []}], []}}
@spec parse_expression_block_raw(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as parse_expression_block_raw.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the parse_expression_block_raw (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
Parses an identifier and returns a clean result.
Examples
iex> Mau.Parser.parse_identifier("user")
{:ok, "user"}
iex> Mau.Parser.parse_identifier("$input")
{:ok, "$input"}
iex> Mau.Parser.parse_identifier("user_name")
{:ok, "user_name"}
@spec parse_identifier_raw(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as parse_identifier_raw.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the parse_identifier_raw (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
Parses a null literal and returns a clean result.
Examples
iex> Mau.Parser.parse_null_literal("null")
{:ok, {:literal, [nil], []}}
@spec parse_null_literal_raw(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as parse_null_literal_raw.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the parse_null_literal_raw (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
Parses a number literal and returns a clean result.
Examples
iex> Mau.Parser.parse_number_literal("42")
{:ok, {:literal, [42], []}}
iex> Mau.Parser.parse_number_literal("3.14")
{:ok, {:literal, [3.14], []}}
iex> Mau.Parser.parse_number_literal("-123")
{:ok, {:literal, [-123], []}}
@spec parse_number_literal_raw(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as parse_number_literal_raw.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the parse_number_literal_raw (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
Parses a string literal and returns a clean result.
Examples
iex> Mau.Parser.parse_string_literal(~s("hello"))
{:ok, {:literal, ["hello"], []}}
iex> Mau.Parser.parse_string_literal("'world'")
{:ok, {:literal, ["world"], []}}
@spec parse_string_literal_raw(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as parse_string_literal_raw.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the parse_string_literal_raw (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
@spec parse_template(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as parse_template.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the parse_template (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
Parses a variable path and returns a clean result.
Examples
iex> Mau.Parser.parse_variable_path("user")
{:ok, {:variable, ["user"], []}}
iex> Mau.Parser.parse_variable_path("user.name")
{:ok, {:variable, ["user", {:property, "name"}], []}}
iex> Mau.Parser.parse_variable_path("$input.data.field")
{:ok, {:variable, ["$input", {:property, "data"}, {:property, "field"}], []}}
@spec parse_variable_path_raw(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as parse_variable_path_raw.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the parse_variable_path_raw (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
@spec unary_expression(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as unary_expression.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the unary_expression (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map