public class Interpreter extends 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, ST, or ST.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
Interpreter
|
protected static class |
Interpreter
|
static class |
Interpreter
|
| Modifier and Type | Field and Description |
|---|---|
boolean |
debug
When
true, track events inside templates and in
events.
|
static int |
DEFAULT_OPERAND_STACK_SIZE
|
protected List |
events
Track everything happening in interpreter across all templates if
debug.
|
protected List |
executeTrace
If
trace is
true, track trace here.
|
static Set |
predefinedAnonSubtemplateAttributes
|
static boolean |
trace
Dump bytecode instructions as they are executed.
|
| Constructor and Description |
|---|
Interpreter(STGroup
|
Interpreter(STGroup
|
Interpreter(STGroup
|
Interpreter(STGroup
|
| Modifier and Type | Method and Description |
|---|---|
protected int |
_exec(STWriter
|
protected void |
addToList(InstanceScope
|
Object |
convertAnythingIteratableToIterator(InstanceScope
|
Iterator |
convertAnythingToIterator(InstanceScope
|
int |
exec(STWriter
Execute template
self and return how many characters it wrote to
out.
|
Object |
first(InstanceScope
Return the first attribute if multi-valued, or the attribute itself if single-valued.
|
Object |
getAttribute(InstanceScope
Find an attribute via dynamic scoping up enclosing scope chain.
|
Object |
getDictionary(STGroup
|
static List |
getEnclosingInstanceStack(InstanceScope
|
static String |
getEnclosingInstanceStackString(InstanceScope
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 List |
getEvalTemplateEventStack(InstanceScope
|
List |
getEvents()
|
List |
getExecutionTrace()
|
protected int |
getExprStartChar(InstanceScope
|
protected int |
getExprStopChar(InstanceScope
|
protected Object |
getObjectProperty(STWriter
|
static List |
getScopeStack(InstanceScope
|
static int |
getShort(byte[] memory, int index)
|
protected void |
indent(STWriter
|
Object |
last(InstanceScope
Return the last attribute if multi-valued, or the attribute itself if single-valued.
|
Object |
length(Object
Return the length of a multi-valued attribute or 1 if it is a single attribute.
|
protected void |
map(InstanceScope
|
protected void |
printForTrace(StringBuilder
|
Object |
rest(InstanceScope
Return everything but the first attribute if multi-valued, or
null if single-valued.
|
Object |
reverse(InstanceScope
Return a list with the same elements as
v but in reverse order.
|
protected List |
rot_map_iterator(InstanceScope
|
protected void |
rot_map(InstanceScope
Renders expressions of the form
<names:a()> or
<names:a(),b()>.
|
void |
setDefaultArguments(STWriter
Set any default argument values that were not set by the invoking template or by
ST directly.
|
protected void |
setFirstArgument(InstanceScope
|
Object |
strip(InstanceScope
Return a new list without
null values.
|
protected boolean |
testAttributeTrue(Object
|
protected String |
toString(STWriter
|
protected void |
trace(InstanceScope
|
protected void |
trackDebugEvent(InstanceScope
For every event, we track in overall
events list and in
self's
InstanceScope list so that each template has a list of events used to create it.
|
Object |
trunc(InstanceScope
Return all but the last element.
|
protected int |
writeIterator(STWriter
|
protected int |
writeObject(STWriter
Generic method to emit text for an object.
|
protected int |
writeObjectNoOptions(STWriter
Write out an expression result that doesn't use expression options.
|
protected int |
writeObjectWithOptions(STWriter
Write out an expression result that uses expression options.
|
protected int |
writePOJO(STWriter
|
protected ST |
zip_map(InstanceScope
Renders expressions of the form
<names,phones:{n,p | ...}> or
<a,b:t()>.
|
public static final int DEFAULT_OPERAND_STACK_SIZE
public static final Set<String > predefinedAnonSubtemplateAttributes
public static boolean trace
public boolean debug
true, track events inside templates and in
events.
protected List<InterpEvent > events
debug. The last event in this field is the
EvalTemplateEvent for the root template.
public Interpreter(STGroupgroup, boolean debug)
public Interpreter(STGroupgroup, ErrorManager errMgr, boolean debug)
public Interpreter(STGroupgroup, Locale locale, ErrorManager errMgr, boolean debug)
public int exec(STWriterout, InstanceScope scope)
self and return how many characters it wrote to
out.
out
protected int _exec(STWriterout, InstanceScope scope)
protected void indent(STWriterout, InstanceScope scope, int strIndex)
protected int writeObjectNoOptions(STWriterout, InstanceScope scope, Object o)
<name>
protected int writeObjectWithOptions(STWriterout, InstanceScope scope, Object o, Object [] options)
<names; separator=", ">
protected int writeObject(STWriterout, InstanceScope scope, Object o, String [] options)
protected int writeIterator(STWriterout, InstanceScope scope, Object o, String [] options) throws IOException
IOException
protected int writePOJO(STWriterout, InstanceScope scope, Object o, String [] options) throws IOException
IOException
protected int getExprStartChar(InstanceScopescope)
protected int getExprStopChar(InstanceScopescope)
protected void map(InstanceScopescope, Object attr, ST st)
protected void rot_map(InstanceScopescope, Object attr, List <ST > prototypes)
<names:a()> or
<names:a(),b()>.
protected List<ST > rot_map_iterator(InstanceScope scope, Iterator <?> attr, List <ST > prototypes)
protected ST.AttributeList zip_map(InstanceScope scope, List <Object > exprs, ST prototype)
<names,phones:{n,p | ...}> or
<a,b:t()>.
protected void setFirstArgument(InstanceScopescope, ST st, Object attr)
protected void addToList(InstanceScopescope, List <Object > list, Object o)
public Objectfirst(InstanceScope scope, Object v)
This method is used for rendering expressions of the form <names:first()>.
public Objectlast(InstanceScope scope, 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 Objectrest(InstanceScope scope, Object v)
null if single-valued.
public Objecttrunc(InstanceScope scope, Object v)
trunc(x)==null if
x is single-valued.
public Objectstrip(InstanceScope scope, Object v)
null values.
public Objectreverse(InstanceScope scope, Object v)
v but in reverse order.
Note that null values are not stripped out; use reverse(strip(v)) to do that.
public Objectlength(Object v)
v is
null return 0.
The implementation treats several common collections and arrays as special cases for speed.
protected StringtoString(STWriter out, InstanceScope scope, Object value)
public ObjectconvertAnythingIteratableToIterator(InstanceScope scope, Object o)
public Iterator<?> convertAnythingToIterator(InstanceScope scope, Object o)
protected boolean testAttributeTrue(Objecta)
protected ObjectgetObjectProperty(STWriter out, InstanceScope scope, Object o, Object property)
public ObjectgetAttribute(InstanceScope scope, String name)
Return ST if found definition but no value.
public void setDefaultArguments(STWriterout, 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 StringgetEnclosingInstanceStackString(InstanceScope scope)
String of these instance names in order from topmost to lowest; here that would be
[z y x].
public static List<ST > getEnclosingInstanceStack(InstanceScope scope, boolean topdown)
public static List<InstanceScope > getScopeStack(InstanceScope scope, boolean topdown)
public static List<EvalTemplateEvent > getEvalTemplateEventStack(InstanceScope scope, boolean topdown)
protected void trace(InstanceScopescope, int ip)
protected void printForTrace(StringBuildertr, InstanceScope scope, Object o)
public List<InterpEvent > getEvents()
protected void trackDebugEvent(InstanceScopescope, InterpEvent e)
events list and in
self's
InstanceScope.events list so that each template has a list of events used to create it. If
e is an
EvalTemplateEvent, store in parent's
InstanceScope.childEvalTemplateEvents list for
STViz tree view.
public static int getShort(byte[] memory,
int index)