NAME

Text::MustacheTemplate::Parser - Simple mustache template parser

SYNOPSIS

use Text::MustacheTemplate::Lexer;
use Text::MustacheTemplate::Parser;

# change delimiters
# local $Text::MustacheTemplate::Lexer::OPEN_DELIMITER = '<%';
# local $Text::MustacheTemplate::Lexer::CLOSE_DELIMITER = '%>';

my $source = '* {{variable}}';
my @tokens = Text::MustacheTemplate::Lexer->tokenize($source);
local $Text::MustacheTemplate::Parser::SOURCE = $source; # optional for syntax error reporting
my $ast = Text::MustacheTemplate::Parser->parse(@tokens);

DESCRIPTION

Text::MustacheTemplate::Parser is a simple parser for Mustache template.

This is low-level interface for Text::MustacheTemplate. The APIs may be change without notice.

METHODS

parse(\@tokens)

Parses the token array from Text::MustacheTemplate::Lexer and converts it into an abstract syntax tree (AST). Returns a nested ArrayRef structure representing the template's parsed form.

The AST consists of nodes, where each node is an array reference with the following structure:

  • First element: Node type constant (see "SYNTAXES")

  • Remaining elements: Node-specific data

SYNTAXES

The following constants define the various syntax node types that make up the abstract syntax tree (AST) after parsing.

SYNTAX_RAW_TEXT

Represents literal text content in the template.

Format: [SYNTAX_RAW_TEXT, $text]

SYNTAX_VARIABLE

Represents a variable interpolation tag (e.g., {{name}} or {{{name}}}).

Format: [SYNTAX_VARIABLE, $escape_type, $variable_name]

Where $escape_type is one of:

VARIABLE_HTML_ESCAPE

The variable should be HTML-escaped before output (used with {{name}})

VARIABLE_RAW

The variable should not be escaped (used with {{{name}}} or {{&name}})

SYNTAX_BOX

Represents block-like structures that may contain child content.

Format: [SYNTAX_BOX, $box_type, ...] where the remaining parameters depend on the box type.

Box types include:

BOX_SECTION

A regular Mustache section ({{#section}}...{{/section}}).

Format: [SYNTAX_BOX, BOX_SECTION, $name, $inner_template, \@children]

$inner_template contains the raw template source of the section for lambda support.

BOX_INVERTED_SECTION

An inverted section ({{^section}}...{{/section}}) which renders when the value is falsy.

Format: [SYNTAX_BOX, BOX_INVERTED_SECTION, $name, \@children]

BOX_BLOCK

A block definition ({{$block}}...{{/block}}) for template inheritance.

Format: [SYNTAX_BOX, BOX_BLOCK, $name, \@children]

BOX_PARENT

A parent template reference ({{<parent}}...{{/parent}}) for template inheritance.

Format: [SYNTAX_BOX, BOX_PARENT, $reference_type, $name, \@children]

Where $reference_type is one of:

REFERENCE_STATIC

A static parent template name

REFERENCE_DYNAMIC

A dynamic parent template name (e.g., {{<*dynamic}})

SYNTAX_COMMENT

Represents a comment ({{! comment }}) which doesn't appear in the output.

Format: [SYNTAX_COMMENT, $comment_text]

SYNTAX_PARTIAL

Represents including a partial template ({{> partial}}).

Format: [SYNTAX_PARTIAL, $reference_type, $name, $padding]

Where $reference_type is one of:

REFERENCE_STATIC

A static partial template name

REFERENCE_DYNAMIC

A dynamic partial template name (e.g., {{>*dynamic}})

$padding contains any whitespace that preceded the tag, used for indenting partial content.

SYNTAX_DELIMITER

Represents a delimiter change ({{=<% %>=}}).

Format: [SYNTAX_DELIMITER, $open_delimiter, $close_delimiter]

LICENSE

Copyright (C) karupanerura.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

karupanerura <karupa@cpan.org>