Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/Template/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ sub tokenise_directive {
(-?\d+(?:\.\d+)?) # numbers
|
# filename matches in $5
( \/?\w+(?:(?:\/|::?)\w*)+ | \/\w+)
( \/?\w+(?:(?:\/|::?)[a-zA-Z_]\w*)+ | \/[a-zA-Z_]\w*)
|
# an identifier matches in $6
(\w+) # variable identifier
Expand Down Expand Up @@ -671,6 +671,19 @@ sub tokenise_directive {
}
# number
elsif (defined ($token = $4)) {
# A negative number like -2 that immediately follows a value-producing
# token (IDENT, NUMBER, LITERAL, ')' or ']') should be split into a
# BINOP '-' and a positive number. Without this, "x-2" tokenises as
# IDENT 'x', NUMBER '-2' instead of IDENT 'x', BINOP '-', NUMBER '2'.
if ($token =~ /^-/ && @tokens >= 2) {
my $prev_type = $tokens[-2];
if ($prev_type eq 'IDENT' || $prev_type eq 'NUMBER'
|| $prev_type eq 'LITERAL' || $prev_type eq ')'
|| $prev_type eq ']') {
push(@tokens, 'BINOP', '-');
$token = substr($token, 1);
}
}
$type = 'NUMBER';
}
elsif (defined($token = $5)) {
Expand Down
23 changes: 23 additions & 0 deletions t/macro.t
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,26 @@ two: 2[The Title] -> one: 2[The Title]
[% triple(10) %]
-- expect --
30

-- test --
-- name expressions in macro args (GH #315) --
[% MACRO show(n) GET n -%]
[% x = 5 -%]
[% show(x) %],[% show(x+2) %],[% show(x-2) %],[% show(x*2) %],[% show(x/2) %]
-- expect --
5,7,3,10,2.5

-- test --
-- name negative number in macro arg --
[% MACRO show(n) GET n -%]
[% show(-3) %]
-- expect --
-3

-- test --
-- name multiple args with expressions --
[% MACRO add(a, b) GET a + b -%]
[% x = 10 -%]
[% add(x-3, 2) %],[% add(x/2, x*3) %]
-- expect --
9,35