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.render(), ST.inspect(), or ST.getEvents().| Modifier and Type | Class and Description |
|---|---|
protected static class |
Interpreter.ArgumentsMap |
protected static class |
Interpreter.ObjectList |
static class |
Interpreter.Option |
| Modifier and Type | Field and Description |
|---|---|
InstanceScope |
currentScope
Stack of enclosing instances (scopes).
|
boolean |
debug
When
true, track events inside templates and in events. |
static int |
DEFAULT_OPERAND_STACK_SIZE |
protected List<InterpEvent> |
events
Track everything happening in interpreter across all templates if
debug. |
protected List<String> |
executeTrace
If
trace is true, track trace here. |
static Set<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,
Locale locale,
boolean debug) |
Interpreter(STGroup group,
Locale locale,
ErrorManager errMgr,
boolean debug) |
| Modifier and Type | Method and Description |
|---|---|
protected int |
_exec(STWriter out,
ST self) |
protected void |
addToList(List<Object> list,
Object o) |
Object |
convertAnythingIteratableToIterator(Object o) |
Iterator<?> |
convertAnythingToIterator(Object o) |
int |
exec(STWriter out,
ST self)
Execute template
self and return how many characters it wrote to out. |
Object |
first(Object v)
Return the first attribute if multi-valued, or the attribute itself if
single-valued.
|
Object |
getAttribute(ST self,
String name)
Find an attribute via dynamic scoping up enclosing scope chain.
|
Object |
getDictionary(STGroup g,
String name) |
static List<ST> |
getEnclosingInstanceStack(InstanceScope scope,
boolean topdown) |
static 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 List<EvalTemplateEvent> |
getEvalTemplateEventStack(InstanceScope scope,
boolean topdown) |
List<InterpEvent> |
getEvents() |
List<String> |
getExecutionTrace() |
protected int |
getExprStartChar(ST self) |
protected int |
getExprStopChar(ST self) |
protected Object |
getObjectProperty(STWriter out,
ST self,
Object o,
Object property) |
static List<InstanceScope> |
getScopeStack(InstanceScope scope,
boolean topdown) |
static int |
getShort(byte[] memory,
int index) |
protected void |
indent(STWriter out,
ST self,
int strIndex) |
Object |
last(Object v)
Return the last attribute if multi-valued, or the attribute itself if
single-valued.
|
Object |
length(Object v)
Return the length of a multi-valued attribute or 1 if it is a single
attribute.
|
protected void |
map(ST self,
Object attr,
ST st) |
protected void |
printForTrace(StringBuilder tr,
Object o) |
Object |
rest(Object v)
Return everything but the first attribute if multi-valued, or
null if single-valued. |
Object |
reverse(Object v)
Return a list with the same elements as
v but in reverse order. |
protected List<ST> |
rot_map_iterator(ST self,
Iterator<?> attr,
List<ST> prototypes) |
protected void |
rot_map(ST self,
Object attr,
List<ST> prototypes)
Renders expressions of the form
<names:a()> or
<names:a(),b()>. |
void |
setDefaultArguments(STWriter out,
ST invokedST)
Set any default argument values that were not set by the invoking
template or by
ST.add(java.lang.String, java.lang.Object) directly. |
protected void |
setFirstArgument(ST self,
ST st,
Object attr) |
Object |
strip(Object v)
Return a new list without
null values. |
protected boolean |
testAttributeTrue(Object a) |
protected String |
toString(STWriter out,
ST self,
Object value) |
protected void |
trace(ST self,
int ip) |
protected void |
trackDebugEvent(ST self,
InterpEvent e)
For every event, we track in overall
events list and in
self's InstanceScope.events list so that each template
has a list of events used to create it. |
Object |
trunc(Object v)
Return all but the last element.
|
protected int |
writeIterator(STWriter out,
ST self,
Object o,
String[] options) |
protected int |
writeObject(STWriter out,
ST self,
Object o,
String[] options)
Generic method to emit text for an object.
|
protected int |
writeObjectNoOptions(STWriter out,
ST self,
Object o)
Write out an expression result that doesn't use expression options.
|
protected int |
writeObjectWithOptions(STWriter out,
ST self,
Object o,
Object[] options)
Write out an expression result that uses expression options.
|
protected int |
writePOJO(STWriter out,
Object o,
String[] options) |
protected ST.AttributeList |
zip_map(ST self,
List<Object> exprs,
ST prototype)
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 InstanceScope currentScope
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(STGroup group, boolean debug)
public Interpreter(STGroup group, ErrorManager errMgr, boolean debug)
public Interpreter(STGroup group, Locale locale, ErrorManager errMgr, boolean debug)
public int exec(STWriter out, ST self)
self and return how many characters it wrote to out.outprotected int writeObjectNoOptions(STWriter out, ST self, Object o)
<name>protected int writeObjectWithOptions(STWriter out, ST self, Object o, Object[] options)
<names; separator=", ">protected int writeObject(STWriter out, ST self, Object o, String[] options)
protected int writeIterator(STWriter out, ST self, Object o, String[] options) throws IOException
IOExceptionprotected int writePOJO(STWriter out, Object o, String[] options) throws IOException
IOExceptionprotected int getExprStartChar(ST self)
protected int getExprStopChar(ST self)
protected void rot_map(ST self, Object attr, List<ST> prototypes)
<names:a()> or
<names:a(),b()>.protected List<ST> rot_map_iterator(ST self, Iterator<?> attr, List<ST> prototypes)
protected ST.AttributeList zip_map(ST self, List<Object> exprs, ST prototype)
<names,phones:{n,p | ...}> or
<a,b:t()>.public Object first(Object v)
<names:first()>.public Object last(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 Object rest(Object v)
null if single-valued.public Object trunc(Object v)
trunc(x)==null if x is single-valued.public Object reverse(Object v)
v but in reverse order.
Note that null values are not stripped out; use
reverse(strip(v)) to do that.public Object length(Object v)
v is null return 0.
The implementation treats several common collections and arrays as
special cases for speed.protected boolean testAttributeTrue(Object a)
protected Object getObjectProperty(STWriter out, ST self, Object o, Object property)
public Object getAttribute(ST self, String name)
ST.EMPTY_ATTR if found definition but no value.public void setDefaultArguments(STWriter out, ST invokedST)
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 String getEnclosingInstanceStackString(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(ST self, int ip)
protected void printForTrace(StringBuilder tr, Object o)
public List<InterpEvent> getEvents()
protected void trackDebugEvent(ST self, 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)
Copyright © 2012. All Rights Reserved.