Skip to content

Commit

Permalink
Merged :elem_slots and :eval_args into :elements in T::TT3::Constants
Browse files Browse the repository at this point in the history
  • Loading branch information
abw committed Nov 19, 2009
1 parent 0de6e9f commit 4b20e89
Show file tree
Hide file tree
Showing 55 changed files with 322 additions and 260 deletions.
3 changes: 2 additions & 1 deletion bin/tt3-html
Expand Up @@ -16,7 +16,8 @@ use constant TEMPLATE => 'Template::TT3::Template';
my $file = shift || die usage();
my $template = TEMPLATE->new( file => $file );
my $tokens = $template->tokens->view_HTML;
my $tree = $template->tree->view_HTML;
my $tree = $template->try->tree;
$tree = $tree ? $tree->view_HTML : "ERROR: $@";
my $wrapper = TEMPLATE->new( file => \*DATA );

print $wrapper->fill(
Expand Down
8 changes: 8 additions & 0 deletions examples/list_generator.tt3
@@ -0,0 +1,8 @@
[%
[
for [1, 2, 3, 4, 5, 6]
if item < 5
item * 2
].join

%]
69 changes: 69 additions & 0 deletions html/list_generator.html
@@ -0,0 +1,69 @@
<html>
<head>
<title>TT3 Template Tree: examples/list_generator.tt3</title>
<link rel="stylesheet" type="text/css" href="tt3.css">
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="tt3.js"></script>

</head>
<body>
<h1>TT3: examples/list_generator.tt3</h1>

<div class="tabset" id="tabset">
<ul class="tabs">
<li class="warm"><a href="#tokens">Tokens</a></li>
<li><a href="#tree">Tree</a></li>
</ul>
<div class="panels">
<div class="panel warm" id="tokens">
<h2>Source Tokens</h1>
<div class="content">
<span class="tag"><span class="tag_start">[%</span><span class="whitespace">
<span class="nl"></span> </span><span class="element">[</span><span class="whitespace">
<span class="nl"></span> </span><span class="keyword">for</span><span class="whitespace"> </span><span class="element">[</span><span class="number">1</span><span class="element">,</span><span class="whitespace"> </span><span class="number">2</span><span class="element">,</span><span class="whitespace"> </span><span class="number">3</span><span class="element">,</span><span class="whitespace"> </span><span class="number">4</span><span class="element">,</span><span class="whitespace"> </span><span class="number">5</span><span class="element">,</span><span class="whitespace"> </span><span class="number">6</span><span class="terminator">]</span><span class="whitespace">
<span class="nl"></span> </span><span class="if keyword">if</span><span class="whitespace"> </span><span class="word">item</span><span class="whitespace"> </span><span class="binary"><</span><span class="whitespace"> </span><span class="number">5</span><span class="whitespace">
<span class="nl"></span> </span><span class="word">item</span><span class="whitespace"> </span><span class="binary">*</span><span class="whitespace"> </span><span class="number">2</span><span class="whitespace">
<span class="nl"></span> </span><span class="terminator">]</span><span class="binary">.</span><span class="word">join</span><span class="whitespace">
<span class="nl"></span>
<span class="nl"></span></span><span class="tag_end">%]</span></span><span class="eof">--EOF--</span>
</div>
</div>
<div class="panel" id="tree">
<h2>Parse Tree</h1>
<div class="content">

<div class="block">
<div class="binary expr element">
<div class="head"><span class="info type">binary expr</span><span class="info posn">@83</span><span class="source">[ for ] . join</span></div>

<div class="body">
<div class="lhs ">
<div class="element">Template::TT3::Element::List=ARRAY(0x40f10)</div>
</div>

<div class="operator element">.</div>

<div class="rhs ">
<div class="word element element">
<div class="head"><span class="info type">word element</span><span class="info posn">@84</span><span class="source">join</span></div>
</div>
</div>
</div>
</div>
</div>

</div>
</div>
</div>
</div>
</body>
<script type="text/javascript">
$(document).ready(
function() {
$('#tabset').tt3_tabset();
$('#tree').tt3_tree();
}
);
</script>
</html>

4 changes: 2 additions & 2 deletions lib/Template/TT3/Class.pm
@@ -1,6 +1,6 @@
package Template::TT3::Class;

use Template::TT3::Constants ':eval_args';
#use Template::TT3::Constants ':elements';
use Badger::Class
version => 3.00,
debug => 0,
Expand Down Expand Up @@ -60,7 +60,7 @@ sub view {

$self->method(
view => sub {
$_[VIEW]->$method($_[SELF]);
$_[1]->$method($_[0]);
}
);
}
Expand Down
9 changes: 6 additions & 3 deletions lib/Template/TT3/Constants.pm
Expand Up @@ -43,8 +43,11 @@ use Badger::Class
CONFIG => '=0',
VARS => '=1',
METHODS => '=2',

# evaluation parameters
SELF => '=0', # zeroth argument is always $self
},
elem_slots => {
elements => {
# element slots - the first 4 are common to all elements
META => '=0',
NEXT => '=1',
Expand All @@ -68,8 +71,8 @@ use Badger::Class
RPREC => '=3', # rightward precedence
LEFT => '=-1', # binds left # NOT USED?
RIGHT => '=1', # binds right # NOT USED?
},
eval_args => {

# evaluation parameters
SELF => '=0', # zeroth argument is always $self
CONTEXT => '=1', # first argument is content/visitor/genr
VISITOR => '=1',
Expand Down
16 changes: 13 additions & 3 deletions lib/Template/TT3/Element.pm
Expand Up @@ -7,7 +7,7 @@ use Template::TT3::Class
utils => 'self_params numlike refaddr',
slots => 'meta _next token pos',
import => 'class',
constants => ':elem_slots :eval_args CODE ARRAY HASH BLANK',
constants => ':elements CODE ARRAY HASH BLANK CMD_PRECEDENCE FORCE',
constant => {
# define a new base_type for the T::Base type() method to strip off
# when generate a short type name for each subclass op
Expand Down Expand Up @@ -243,8 +243,18 @@ sub as_postop {


sub as_block {
# Any expression can be a single expression block
return shift->as_expr(@_);
# my ($self, $token, $scope, $parent, $follow) = @_;

# Any expression can be a single expression block. We specify the
# precedence level for commands so that the expression will consume
# higher precedence operators, but stop at the next keyword, e.g.
# "if x a + 10 for y" is parsed as "(if x (a + 10)) for y". Note that
# the block (denoted by "(...)") ends at the 'for' keyword rather than
# consuming it rightwards as "(if x ((a + 10) for y)). We use the FORCE
# flag to indicate that the precedence may be ignored for the first
# and only the first token (i.e. this one, $self). That allows a
# command to follow as the single expression: if x fill y
return $_[0]->as_expr($_[1], $_[2], CMD_PRECEDENCE, FORCE);
}


Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Command.pm
Expand Up @@ -4,7 +4,7 @@ use Template::TT3::Elements::Literal;
use Template::TT3::Class
version => 3.00,
base => 'Template::TT3::Element::Keyword',
constants => ':elem_slots :eval_args';
constants => ':elements';

sub generate {
$_[CONTEXT]->generate_keyword(
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Command/As.pm
Expand Up @@ -3,7 +3,7 @@ package Template::TT3::Element::Command::As;
use Template::TT3::Class
version => 3.00,
base => 'Template::TT3::Element::Command',
constants => ':elem_slots :eval_args',
constants => ':elements',
as => 'expr_block_expr',
alias => {
# value => \&text,
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Command/Block.pm
Expand Up @@ -5,7 +5,7 @@ use Template::TT3::Class
debug => 0,
base => 'Template::TT3::Element::Command',
utils => 'tt_params',
constants => ':elem_slots :eval_args BLANK',
constants => ':elements BLANK',
alias => {
value => \&text,
values => \&text,
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Command/Decode.pm
Expand Up @@ -3,7 +3,7 @@ package Template::TT3::Element::Command::Decode;
use Template::TT3::Class
version => 3.00,
base => 'Template::TT3::Element::Command::Encode',
constants => ':elem_slots :eval_args',
constants => ':elements',
constant => {
ARG_NAME => 'decoder',
},
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Command/Do.pm
Expand Up @@ -5,7 +5,7 @@ use Template::TT3::Class
debug => 0,
base => 'Template::TT3::Element::Command',
as => 'block_expr',
constants => ':elem_slots :eval_args',
constants => ':elements',
alias => {
text => \&value,
values => \&value,
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Command/Dot.pm
Expand Up @@ -4,7 +4,7 @@ use Template::TT3::Class
version => 3.00,
debug => 0,
base => 'Template::TT3::Element::Command::Block',
constants => ':elem_slots :eval_args',
constants => ':elements',
constant => {
ARG_NAME => 'decoder',
SOURCE_FORMAT => '%s %s %s',
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Command/Encode.pm
Expand Up @@ -4,7 +4,7 @@ use Template::TT3::Class
version => 3.00,
base => 'Template::TT3::Element::Command::Block',
as => 'name_block_expr',
constants => ':elem_slots :eval_args',
constants => ':elements',
constant => {
ARG_NAME => 'encoder', # changed in ...::Decode subclass
},
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Command/Fill.pm
Expand Up @@ -5,7 +5,7 @@ use Template::TT3::Class
debug => 0,
base => 'Template::TT3::Element::Command',
as => 'name_expr',
constants => ':elem_slots :eval_args ARRAY',
constants => ':elements ARRAY',
constant => {
SEXPR_FORMAT => "<fill:%s>",
},
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Command/For.pm
Expand Up @@ -4,7 +4,7 @@ use Template::TT3::Class
version => 3.00,
debug => 0,
base => 'Template::TT3::Element::Command',
constants => ':elem_slots :eval_args ARRAY',
constants => ':elements ARRAY',
constant => {
SEXPR_FORMAT => "<for:\n <expr:\n %s\n >\n <body:\n %s\n >\n>",
},
Expand Down
11 changes: 8 additions & 3 deletions lib/Template/TT3/Element/Command/If.pm
Expand Up @@ -4,9 +4,14 @@ use Template::TT3::Class
version => 3.00,
debug => 0,
base => 'Template::TT3::Element::Command',
constants => ':elem_slots :eval_args',
view => 'if',
constants => ':elements',
constant => {
SEXPR_FORMAT => "<if:\n <expr:\n %s\n >\n <body:\n %s\n >\n>",
FOLLOW => {
map { $_ => 1 }
qw( elsif else )
},
},
alias => {
# value => \&text,
Expand Down Expand Up @@ -45,7 +50,7 @@ sub as_expr {
|| return $self->missing( expression => $token );

# parse block following the expression
$self->[RHS] = $$token->as_block($token, $scope)
$self->[RHS] = $$token->as_block($token, $scope, $self, $self->FOLLOW)
|| return $self->missing( block => $token );

# TODO: look for elsif/else
Expand Down Expand Up @@ -99,7 +104,7 @@ sub source {
my $self = shift;
my $expr = $self->[LHS]->source;
my $body = $self->[RHS]->source;
return "if $expr; $body; end";
return "if $expr { $body }";
}

1;
15 changes: 14 additions & 1 deletion lib/Template/TT3/Element/Command/Is.pm
Expand Up @@ -3,7 +3,11 @@ package Template::TT3::Element::Command::Is;
use Template::TT3::Class
version => 3.00,
base => 'Template::TT3::Element::Command',
constants => ':elem_slots :eval_args',
view => 'is',
constants => ':elements',
constant => {
SOURCE_FORMAT => '%s %s { %s }',
},
as => 'block_expr',
alias => {
value => \&text,
Expand Down Expand Up @@ -62,4 +66,13 @@ sub text {
# }


sub source {
sprintf(
$_[SELF]->SOURCE_FORMAT,
$_[SELF]->[LHS]->source,
$_[SELF]->[TOKEN],
$_[SELF]->[RHS]->source,
)
}

1;
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Command/Raw.pm
Expand Up @@ -5,7 +5,7 @@ use Template::TT3::Class
base => 'Template::TT3::Element::Command',
as => 'block_expr',
view => 'raw',
constants => ':elem_slots :eval_args',
constants => ':elements',
alias => {
value => \&text,
values => \&text,
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Control/Commands.pm
Expand Up @@ -4,7 +4,7 @@ use Template::TT3::Class
version => 3.00,
debug => 0,
base => 'Template::TT3::Element::Command',
constants => ':elem_slots :eval_args',
constants => ':elements',
as => 'name_expr',
alias => {
text => \&value,
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Control/Html.pm
Expand Up @@ -5,7 +5,7 @@ use Template::TT3::Class
version => 3.00,
debug => 0,
base => 'Template::TT3::Element::Command',
constants => ':elem_slots :eval_args DOT',
constants => ':elements DOT',
constant => {
SKIP_WORDS => {
map { $_ => 1 }
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Control/Tags.pm
Expand Up @@ -4,7 +4,7 @@ use Template::TT3::Class
version => 3.00,
debug => 0,
base => 'Template::TT3::Element::Command',
constants => ':elem_slots :eval_args DOT',
constants => ':elements DOT',
constant => {
SKIP_WORDS => {
map { $_ => 1 }
Expand Down
10 changes: 7 additions & 3 deletions lib/Template/TT3/Element/Role/Block.pm
Expand Up @@ -3,12 +3,12 @@ package Template::TT3::Element::Role::Block;
use Template::TT3::Class
debug => 0,
version => 2.718,
constants => ':elem_slots :eval_args',
constants => ':elements',
mixins => 'as_block';


sub as_block {
my ($self, $token, $scope, $parent) = @_;
my ($self, $token, $scope, $parent, $follow) = @_;
my (@exprs, $expr);

# Optional 4th argument is a reference to the parent but default to $self.
Expand All @@ -23,10 +23,14 @@ sub as_block {
# parse expressions
my $block = $$token->as_exprs($token, $scope)
|| return $parent->missing( $self->ARG_BLOCK, $token );

# check next token matches our FINISH token
return $parent->missing( $self->FINISH, $token)
unless $$token->is( $self->FINISH, $token );
# unless $$token->is( $self->FINISH );

# $block->[FOLLOW] = $$token->follow($block, $token, $scope


# return $block, not $self
return $block;
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Role/BlockExpr.pm
Expand Up @@ -3,7 +3,7 @@ package Template::TT3::Element::Role::BlockExpr;
use Template::TT3::Class
version => 2.718,
mixins => 'as_expr',
constants => ':elem_slots';
constants => ':elements';


sub as_expr {
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Role/ExprBlockExpr.pm
Expand Up @@ -3,7 +3,7 @@ package Template::TT3::Element::Role::ExprBlockExpr;
use Template::TT3::Class
version => 2.718,
mixins => 'as_expr',
constants => ':elem_slots';
constants => ':elements';


sub as_expr {
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Element/Role/Filename.pm
Expand Up @@ -2,7 +2,7 @@ package Template::TT3::Element::Role::Filename;

use Template::TT3::Class
version => 2.718,
constants => ':elem_slots :eval_args',
constants => ':elements',
mixins => 'as_filename filename';


Expand Down

0 comments on commit 4b20e89

Please sign in to comment.