public class Interpreter
extends java.lang.Object
STGroup
. To execute the byte codes, we need an output stream and a
reference to an ST
instance. That instance's ST.impl
field
points at a CompiledST
, which contains all of the byte codes and
other information relevant to execution.
This interpreter is a stack-based bytecode interpreter. All operands go onto
an operand stack.
If debug
set, we track interpreter events. For now, I am only
tracking instance creation events. These are used by STViz
to pair up
output chunks with the template expressions that generate them.
We create a new interpreter for each invocation of
ST.render()
, ST.inspect()
, or ST.getEvents()
.Modifier and Type | Class and Description |
---|---|
static class |
Interpreter.Option |
Modifier and Type | Field and Description |
---|---|
boolean |
debug
When
true , track events inside templates and in events . |
static int |
DEFAULT_OPERAND_STACK_SIZE |
static java.util.Set<java.lang.String> |
predefinedAnonSubtemplateAttributes |
static boolean |
trace
Dump bytecode instructions as they are executed.
|
Constructor and Description |
---|
Interpreter(STGroup group,
boolean debug) |
Interpreter(STGroup group,
ErrorManager errMgr,
boolean debug) |
Interpreter(STGroup group,
java.util.Locale locale,
boolean debug) |
Interpreter(STGroup group,
java.util.Locale locale,
ErrorManager errMgr,
boolean debug) |
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
convertAnythingIteratableToIterator(InstanceScope scope,
java.lang.Object o) |
java.util.Iterator<?> |
convertAnythingToIterator(InstanceScope scope,
java.lang.Object o) |
int |
exec(STWriter out,
InstanceScope scope)
Execute template
self and return how many characters it wrote to out . |
java.lang.Object |
first(InstanceScope scope,
java.lang.Object v)
Return the first attribute if multi-valued, or the attribute itself if
single-valued.
|
java.lang.Object |
getAttribute(InstanceScope scope,
java.lang.String name)
Find an attribute via dynamic scoping up enclosing scope chain.
|
java.lang.Object |
getDictionary(STGroup g,
java.lang.String name) |
static java.util.List<ST> |
getEnclosingInstanceStack(InstanceScope scope,
boolean topdown) |
static java.lang.String |
getEnclosingInstanceStackString(InstanceScope scope)
If an instance of x is enclosed in a y which is in a
z, return a
String of these instance names in order from
topmost to lowest; here that would be [z y x] . |
static java.util.List<EvalTemplateEvent> |
getEvalTemplateEventStack(InstanceScope scope,
boolean topdown) |
java.util.List<InterpEvent> |
getEvents() |
java.util.List<java.lang.String> |
getExecutionTrace() |
static java.util.List<InstanceScope> |
getScopeStack(InstanceScope scope,
boolean topdown) |
static int |
getShort(byte[] memory,
int index) |
java.lang.Object |
last(InstanceScope scope,
java.lang.Object v)
Return the last attribute if multi-valued, or the attribute itself if
single-valued.
|
java.lang.Object |
length(java.lang.Object v)
Return the length of a multi-valued attribute or 1 if it is a single
attribute.
|
java.lang.Object |
rest(InstanceScope scope,
java.lang.Object v)
Return everything but the first attribute if multi-valued, or
null if single-valued. |
java.lang.Object |
reverse(InstanceScope scope,
java.lang.Object v)
Return a list with the same elements as
v but in reverse order. |
void |
setDefaultArguments(STWriter out,
InstanceScope scope)
Set any default argument values that were not set by the invoking
template or by
ST.add(java.lang.String, java.lang.Object) directly. |
java.lang.Object |
strip(InstanceScope scope,
java.lang.Object v)
Return a new list without
null values. |
java.lang.Object |
trunc(InstanceScope scope,
java.lang.Object v)
Return all but the last element.
|
public static final int DEFAULT_OPERAND_STACK_SIZE
public static final java.util.Set<java.lang.String> predefinedAnonSubtemplateAttributes
public static boolean trace
public boolean debug
true
, track events inside templates and in events
.public Interpreter(STGroup group, boolean debug)
public Interpreter(STGroup group, java.util.Locale locale, boolean debug)
public Interpreter(STGroup group, ErrorManager errMgr, boolean debug)
public Interpreter(STGroup group, java.util.Locale locale, ErrorManager errMgr, boolean debug)
public int exec(STWriter out, InstanceScope scope)
self
and return how many characters it wrote to out
.out
public java.lang.Object first(InstanceScope scope, java.lang.Object v)
<names:first()>
.public java.lang.Object last(InstanceScope scope, java.lang.Object v)
List
or array, this is pretty slow
as it iterates until the last element.
This method is used for rendering expressions of the form
<names:last()>
.public java.lang.Object rest(InstanceScope scope, java.lang.Object v)
null
if single-valued.public java.lang.Object trunc(InstanceScope scope, java.lang.Object v)
trunc(x)==null
if x
is single-valued.public java.lang.Object strip(InstanceScope scope, java.lang.Object v)
null
values.public java.lang.Object reverse(InstanceScope scope, java.lang.Object v)
v
but in reverse order.
Note that null
values are not stripped out; use
reverse(strip(v))
to do that.public java.lang.Object length(java.lang.Object v)
v
is null
return 0.
The implementation treats several common collections and arrays as
special cases for speed.public java.lang.Object convertAnythingIteratableToIterator(InstanceScope scope, java.lang.Object o)
public java.util.Iterator<?> convertAnythingToIterator(InstanceScope scope, java.lang.Object o)
public java.lang.Object getAttribute(InstanceScope scope, java.lang.String name)
ST.EMPTY_ATTR
if found definition but no value.public java.lang.Object getDictionary(STGroup g, java.lang.String name)
public void setDefaultArguments(STWriter out, InstanceScope scope)
ST.add(java.lang.String, java.lang.Object)
directly. Note that the default values may
be templates.
The evaluation context is the invokedST
template itself so
template default arguments can see other arguments.public static java.lang.String getEnclosingInstanceStackString(InstanceScope scope)
String
of these instance names in order from
topmost to lowest; here that would be [z y x]
.public static java.util.List<ST> getEnclosingInstanceStack(InstanceScope scope, boolean topdown)
public static java.util.List<InstanceScope> getScopeStack(InstanceScope scope, boolean topdown)
public static java.util.List<EvalTemplateEvent> getEvalTemplateEventStack(InstanceScope scope, boolean topdown)
public java.util.List<InterpEvent> getEvents()
public java.util.List<java.lang.String> getExecutionTrace()
public static int getShort(byte[] memory, int index)