Squirrel::Template::Expr - expression handling for Squirrel::Template
# code that uses it my $parser = Squirrel::Template::Expr::Parser->new;
my $expr = $parser->parse($expr_text);
my $tokens = Squirrel::Template::Expr::Tokenizer->new($expr_text);
my $expr = $parser->parse_tokens($tokenizer); # and possibly process more tokens here
my $eval = Squirrel::Template::Expr::Parser->new($templater);
my $value = $eval->process($expr); my $value = $eval->process($expr, "LIST");
my $arrayref = $eval->process(\@exprs);
# Expressions
<:= somevalue + 10 :> <:.if somevalue == 10 :>
Squirrel::Template::Expr provides expression parsing and evaluation for newer style tags for the Squirrel::Template manpage.
Listed highest precedence first.
<[ list ]
>, <{ key:value, ... }
>, literals
<[ list ]
> allows you to build lists objects. Within [ ... ]
you can use the ..
operator to produce a list of numerically or
alphabetically ascending values per Perl's magic increment.
eg.
[ "a", "c" .. "z" ] [ 1 .. 10 ]
Method calls within <[ ... ]
> are done in perl's list context.
<{ ... }
> allows you to build hash objects.
eg.
{ "somekey":somevariable, somekeyinvar:"somevalue" }
See Literals for literals
method calls - methods are called as:
object.method;
or
object.method(arguments)
and may be chained.
Virtual methods are defined for hashes, arrays and scalars, see the Squirrel::Template::Expr::WrapHash manpage, the Squirrel::Template::Expr::WrapArray manpage, the Squirrel::Template::Expr::WrapScalar manpage, the Squirrel::Template::Expr::WrapCode manpage and the Squirrel::Template::Expr::WrapClass manpage.
function calls - functions are called as:
somevar();
or
somevar(arguments);
or any other expression that doesn't look like a method call:
somehash.get["foo"]();
unary -, unary +, unary !, unary not
* / div mod - simple arithmetic operators. div
returns the integer
portion of dividing the first operand by the second. mod
returns
the remainder of integer division.
+ - _ - arithmetic addition and subtraction. _
does string
concatenation.
eq ne le lt ge gt == != > < >= <= =~ !~ - relational operators as per Perl.
and - boolean and, with shortcut.
or - boolean or, with shortcut.
Conditional (cond ? true : false
) - return the value
of true or false depending on cond.
Numbers can be represented in several formats:
simple decimal - 100
, 3.14159
, 1e10
.
hex - 0x64
octal - 0o144
binary - 0b1100100
Strings can be either " or ' delimited.
Simple quote delimited strings allow no escaping, and may not contain single quotes. The contents are treated literally.
Double quoted strings allow escaping as follows:
Any of \"
, \n
, \\
, \t
are treated as in C or perl,
replaced with double quote, newline, backslash or tab respectively.
<\x{hex-digits}
> is replaced with the unicode code-point
indicated by the hex number.
\xhex-digithex-digit
is replaced by the unicode
code-point indicated by the 2-digit hex number.
\N{ unicode-character-name }
is replaced by the unicode
character named.
Squirrel::Template::Expr::Parser provides parsing for expressions.
new()
Create a new parser object.
parse($text)
Parse $text
as an expression. Parsing must reach the end of the
text or an exception will be thrown.
parse_tokens($tokenizer)
Process tokens from $tokenizer
, a
Squirrel::Template::Expr::Tokenizer object. The caller can call
these method several times with the same $tokenizer
to parse
components of a statement, and should ensure the eof token is visible
after the final component.
Split text into tokens. Token parsing is occasionally context sensitive.
new($text)
Create a new tokenizer for parsing $text
.
get()
get($context)
Retrieve a token from the stream, consuming it. If a term is expected
$context should be set to 'TERM'
.
unget()
Push a token back into the stream.
peek()
peek($context)
Retrieve the next token from the stream without consuming it.
peektype()
peektype($context)
Retrieve the type of the next token from the stream without consuming it.
Used to evaluate an expression returned by Squirrel::Template::Expr::parse().
new($templater)
Create a new evaluator. $templater
should be a
the Squirrel::Template manpage object.
the Squirrel::Template manpage
Tony Cook <tony@develop-help.com>