| 1 | package Text::Nunjucks::Parser; | ||||||||||||||
| 2 | # ABSTRACT: 1:1 port of Nunjucks src/parser.js -- the recursive-descent parser | ||||||||||||||
| 3 | |||||||||||||||
| 4 | 19 | our $VERSION = '0.001'; | |||||||||||||
| 5 | |||||||||||||||
| 6 | 19 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 19 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 19 | ▶ | use Text::Nunjucks::Lexer; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 19 | ▶ | use Text::Nunjucks::Parser::Parser; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | # 1:1 port of upstream src/parser.js. parser.js holds one `class Parser extends | ||||||||||||||
| 12 | # Obj` and a `module.exports = { parse, Parser }`. This file mirrors that shape | ||||||||||||||
| 13 | # the way Lexer.pm mirrors lexer.js: the module-level exports live here on | ||||||||||||||
| 14 | # Text::Nunjucks::Parser (parse(), and parser_class() for the exported Parser | ||||||||||||||
| 15 | # class that jinja-compat reaches through `this.parser.Parser`), while the | ||||||||||||||
| 16 | # Parser class itself is the Moo class Text::Nunjucks::Parser::Parser in its own | ||||||||||||||
| 17 | # file (ADR 0002, ADR 0020) -- just as lexer's Tokenizer is | ||||||||||||||
| 18 | # Text::Nunjucks::Lexer::Tokenizer. Keeping them in separate packages resolves | ||||||||||||||
| 19 | # the one name clash parser.js has in a single Perl namespace: the instance | ||||||||||||||
| 20 | # method Parser.prototype.parse() and the module function | ||||||||||||||
| 21 | # module.exports.parse() are both named `parse`. | ||||||||||||||
| 22 | # | ||||||||||||||
| 23 | # lexer/nodes/lib are used through their real interfaces (lexer.TOKEN_X -> | ||||||||||||||
| 24 | # Text::Nunjucks::Lexer->TOKEN_X, new nodes.X -> $self->node('X')->new, | ||||||||||||||
| 25 | # lib.indexOf -> Text::Nunjucks::Lib->index_of), never re-derived. `throw new | ||||||||||||||
| 26 | # Error` becomes a thrown Text::Nunjucks::Lib::Error and `new lib.TemplateError` | ||||||||||||||
| 27 | # a Text::Nunjucks::Lib::TemplateError, the same mappings the lexer uses. | ||||||||||||||
| 28 | |||||||||||||||
| 29 | #### module.exports.parse -- parse(src, extensions, opts) | ||||||||||||||
| 30 | |||||||||||||||
| 31 | 9618 | ▶ | sub parse { | ||||||||||||
|
Subroutine
parse: called CC=2 SCAR=9.2
Pod
undocumented
| |||||||||||||||
| 32 | 0 | ▶ | my ( $self, $src, $extensions, $opts ) = @_; | ||||||||||||
|
Subroutine
parse: not called CC=2 SCAR=9.2
Pod
undocumented
| |||||||||||||||
| 33 | 0 | my $p = $self->parser_class->new( $self->lexer->lex( $src, $opts ) ); | |||||||||||||
| 34 | # JS: if (extensions !== undefined) p.extensions = extensions; | ||||||||||||||
| 35 | 0 | ▶ | $p->extensions($extensions) if defined $extensions; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 36 | 0 | return $p->parse_as_root; | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | # Collaborators named once, reached through the invocant (house rule). | ||||||||||||||
| 40 | 9618 | ▶ | sub lexer { 'Text::Nunjucks::Lexer' } | ||||||||||||
|
Subroutine
lexer: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 41 | 9618 | ▶ | sub parser_class { 'Text::Nunjucks::Parser::Parser' } | ||||||||||||
|
Subroutine
parser_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 42 | |||||||||||||||
| 43 | 19 | 1; | |||||||||||||
| 44 | |||||||||||||||
| 45 | __END__ | ||||||||||||||