Top Description Inners Fields Constructors Methods
org.stringtemplate.v4

public Class Interpreter

extends Object
Class Inheritance
Imports
org.stringtemplate.v4.compiler.*, .Compiler, org.stringtemplate.v4.debug.*, org.stringtemplate.v4.gui.STViz, org.stringtemplate.v4.misc.*, java.io.*, java.lang.reflect.Array, .Constructor, java.util.*

This class knows how to execute template bytecodes relative to a particular 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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected static class
protected static class
public static enum

Field Summary

Modifier and TypeField and Description
public boolean
debug

When true, track events inside templates and in events.

public static final int
pack-priv ErrorManager
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.

pack-priv STGroup
group

Render template with respect to this group.

pack-priv Locale
locale

For renderers, we have to pass in the locale.

pack-priv int
nwline

The number of characters written on this template line so far.

pack-priv Object[]
operands

Operand stack, grows upwards.

public static final Set<String>
pack-priv int
sp

Stack pointer register.

public static boolean
trace

Dump bytecode instructions as they are executed.

Constructor Summary

AccessConstructor and Description
public
Interpreter(STGroup group, boolean debug)

public
Interpreter(STGroup group, Locale locale, boolean debug)

public
Interpreter(STGroup group, ErrorManager errMgr, boolean debug)

public
Interpreter(STGroup group, Locale locale, ErrorManager errMgr, boolean debug)

Method Summary

Modifier and TypeMethod and Description
protected int
protected void
public Object
public Iterator<?>
public int

Returns:

the number of characters written to out
exec
(STWriter out, InstanceScope scope)

Execute template self and return how many characters it wrote to out.

public Object
first(InstanceScope scope, Object v)

Return the first attribute if multi-valued, or the attribute itself if single-valued.

public Object
getAttribute(InstanceScope scope, String name)

Find an attribute via dynamic scoping up enclosing scope chain.

public Object
public static List<ST>
getEnclosingInstanceStack(InstanceScope scope, boolean topdown)

public 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].

public static List<EvalTemplateEvent>
getEvalTemplateEventStack(InstanceScope scope, boolean topdown)

public List<InterpEvent>
public List<String>
protected int
protected int
protected Object
public static List<InstanceScope>
getScopeStack(InstanceScope scope, boolean topdown)

public static int
getShort(byte[] memory, int index)

protected void
indent(STWriter out, InstanceScope scope, int strIndex)

public Object
last(InstanceScope scope, Object v)

Return the last attribute if multi-valued, or the attribute itself if single-valued.

public Object
length(Object v)

Return the length of a multi-valued attribute or 1 if it is a single attribute.

pack-priv void
load_str(ST self, int ip)

protected void
map(InstanceScope scope, Object attr, final ST st)

pack-priv void
passthru(InstanceScope scope, String templateName, Map<String, Object> attrs)

protected void
private <T> String
renderObject(InstanceScope scope, String formatString, Object o, Class<T> attributeType)

public Object
rest(InstanceScope scope, Object v)

Return everything but the first attribute if multi-valued, or null if single-valued.

public Object
reverse(InstanceScope scope, Object v)

Return a list with the same elements as v but in reverse order.

protected void
rot_map(InstanceScope scope, Object attr, List<ST> prototypes)

Renders expressions of the form <names:a()> or <names:a(),b()>.

protected List<ST>
rot_map_iterator(InstanceScope scope, Iterator<?> attr, List<ST> prototypes)

public void
setDefaultArguments(STWriter out, InstanceScope scope)

Set any default argument values that were not set by the invoking template or by ST#add directly.

protected void
pack-priv void
storeArgs(InstanceScope scope, Map<String, Object> attrs, ST st)

pack-priv void
storeArgs(InstanceScope scope, int nargs, ST st)

public Object
strip(InstanceScope scope, Object v)

Return a new list without null values.

pack-priv void
super_new(InstanceScope scope, String name, int nargs)

pack-priv void
protected boolean
protected String
protected void
trace(InstanceScope scope, int ip)

protected void
trackDebugEvent(InstanceScope scope, 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.

public Object
trunc(InstanceScope scope, Object v)

Return all but the last element.

protected int
writeIterator(STWriter out, InstanceScope scope, Object o, String[] options)

protected int
writeObject(STWriter out, InstanceScope scope, Object o, String[] options)

Generic method to emit text for an object.

protected int
writeObjectNoOptions(STWriter out, InstanceScope scope, Object o)

Write out an expression result that doesn't use expression options.

protected int
writeObjectWithOptions(STWriter out, InstanceScope scope, Object o, Object[] options)

Write out an expression result that uses expression options.

protected int
writePOJO(STWriter out, InstanceScope scope, Object o, String[] options)

protected ST.AttributeList
zip_map(InstanceScope scope, List<Object> exprs, ST prototype)

Renders expressions of the form <names,phones:{n,p | ...}> or <a,b:t()>.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

debugback to summary
public boolean debug

When true, track events inside templates and in events.

DEFAULT_OPERAND_STACK_SIZEback to summary
public static final int DEFAULT_OPERAND_STACK_SIZE
errMgrback to summary
pack-priv ErrorManager errMgr
eventsback to summary
protected List<InterpEvent> events

Track everything happening in interpreter across all templates if debug. The last event in this field is the EvalTemplateEvent for the root template.

executeTraceback to summary
protected List<String> executeTrace

If trace is true, track trace here.

groupback to summary
pack-priv STGroup group

Render template with respect to this group.

See Also
ST#groupThatCreatedThisInstance, CompiledST#nativeGroup
localeback to summary
pack-priv Locale locale

For renderers, we have to pass in the locale.

nwlineback to summary
pack-priv int nwline

The number of characters written on this template line so far.

operandsback to summary
pack-priv Object[] operands

Operand stack, grows upwards.

predefinedAnonSubtemplateAttributesback to summary
public static final Set<String> predefinedAnonSubtemplateAttributes
spback to summary
pack-priv int sp

Stack pointer register.

traceback to summary
public static boolean trace

Dump bytecode instructions as they are executed. This field is mostly for StringTemplate development.

Constructor Detail

Interpreterback to summary
public Interpreter(STGroup group, boolean debug)
Interpreterback to summary
public Interpreter(STGroup group, Locale locale, boolean debug)
Interpreterback to summary
public Interpreter(STGroup group, ErrorManager errMgr, boolean debug)
Interpreterback to summary
public Interpreter(STGroup group, Locale locale, ErrorManager errMgr, boolean debug)

Method Detail

_execback to summary
protected int _exec(STWriter out, InstanceScope scope)
addToListback to summary
protected void addToList(InstanceScope scope, List<Object> list, Object o)
convertAnythingIteratableToIteratorback to summary
public Object convertAnythingIteratableToIterator(InstanceScope scope, Object o)
convertAnythingToIteratorback to summary
public Iterator<?> convertAnythingToIterator(InstanceScope scope, Object o)
execback to summary
public int exec(STWriter out, InstanceScope scope)

Execute template self and return how many characters it wrote to out.

Returns:int

the number of characters written to out

firstback to summary
public Object first(InstanceScope scope, Object v)

Return the first attribute if multi-valued, or the attribute itself if single-valued.

This method is used for rendering expressions of the form <names:first()>.

getAttributeback to summary
public Object getAttribute(InstanceScope scope, String name)

Find an attribute via dynamic scoping up enclosing scope chain. Only look for a dictionary definition if the attribute is not found, so attributes sent in to a template override dictionary names.

Return ST#EMPTY_ATTR if found definition but no value.

getDictionaryback to summary
public Object getDictionary(STGroup g, String name)
getEnclosingInstanceStackback to summary
public static List<ST> getEnclosingInstanceStack(InstanceScope scope, boolean topdown)
getEnclosingInstanceStackStringback to summary
public 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].

getEvalTemplateEventStackback to summary
public static List<EvalTemplateEvent> getEvalTemplateEventStack(InstanceScope scope, boolean topdown)
getEventsback to summary
public List<InterpEvent> getEvents()
getExecutionTraceback to summary
public List<String> getExecutionTrace()
getExprStartCharback to summary
protected int getExprStartChar(InstanceScope scope)
getExprStopCharback to summary
protected int getExprStopChar(InstanceScope scope)
getObjectPropertyback to summary
protected Object getObjectProperty(STWriter out, InstanceScope scope, Object o, Object property)
getScopeStackback to summary
public static List<InstanceScope> getScopeStack(InstanceScope scope, boolean topdown)
getShortback to summary
public static int getShort(byte[] memory, int index)
indentback to summary
protected void indent(STWriter out, InstanceScope scope, int strIndex)
lastback to summary
public Object last(InstanceScope scope, Object v)

Return the last attribute if multi-valued, or the attribute itself if single-valued. Unless it's a 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()>.

lengthback to summary
public Object length(Object v)

Return the length of a multi-valued attribute or 1 if it is a single attribute. If v is null return 0.

The implementation treats several common collections and arrays as special cases for speed.

load_strback to summary
pack-priv void load_str(ST self, int ip)
mapback to summary
protected void map(InstanceScope scope, Object attr, final ST st)
passthruback to summary
pack-priv void passthru(InstanceScope scope, String templateName, Map<String, Object> attrs)
printForTraceback to summary
protected void printForTrace(StringBuilder tr, InstanceScope scope, Object o)
renderObjectback to summary
private <T> String renderObject(InstanceScope scope, String formatString, Object o, Class<T> attributeType)
restback to summary
public Object rest(InstanceScope scope, Object v)

Return everything but the first attribute if multi-valued, or null if single-valued.

reverseback to summary
public Object reverse(InstanceScope scope, Object v)

Return a list with the same elements as v but in reverse order.

Note that null values are not stripped out; use reverse(strip(v)) to do that.

rot_mapback to summary
protected void rot_map(InstanceScope scope, Object attr, List<ST> prototypes)

Renders expressions of the form <names:a()> or <names:a(),b()>.

rot_map_iteratorback to summary
protected List<ST> rot_map_iterator(InstanceScope scope, Iterator<?> attr, List<ST> prototypes)
setDefaultArgumentsback to summary
public void setDefaultArguments(STWriter out, InstanceScope scope)

Set any default argument values that were not set by the invoking template or by ST#add 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.

setFirstArgumentback to summary
protected void setFirstArgument(InstanceScope scope, ST st, Object attr)
storeArgsback to summary
pack-priv void storeArgs(InstanceScope scope, Map<String, Object> attrs, ST st)
storeArgsback to summary
pack-priv void storeArgs(InstanceScope scope, int nargs, ST st)
stripback to summary
public Object strip(InstanceScope scope, Object v)

Return a new list without null values.

super_newback to summary
pack-priv void super_new(InstanceScope scope, String name, int nargs)
super_newback to summary
pack-priv void super_new(InstanceScope scope, String name, Map<String, Object> attrs)
testAttributeTrueback to summary
protected boolean testAttributeTrue(Object a)
toStringback to summary
protected String toString(STWriter out, InstanceScope scope, Object value)
traceback to summary
protected void trace(InstanceScope scope, int ip)
trackDebugEventback to summary
protected void trackDebugEvent(InstanceScope scope, 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. If e is an EvalTemplateEvent, store in parent's InstanceScope#childEvalTemplateEvents list for STViz tree view.

truncback to summary
public Object trunc(InstanceScope scope, Object v)

Return all but the last element. trunc(x)==null if x is single-valued.

writeIteratorback to summary
protected int writeIterator(STWriter out, InstanceScope scope, Object o, String[] options) throws IOException
writeObjectback to summary
protected int writeObject(STWriter out, InstanceScope scope, Object o, String[] options)

Generic method to emit text for an object. It differentiates between templates, iterable objects, and plain old Java objects (POJOs)

writeObjectNoOptionsback to summary
protected int writeObjectNoOptions(STWriter out, InstanceScope scope, Object o)

Write out an expression result that doesn't use expression options. E.g., <name>

writeObjectWithOptionsback to summary
protected int writeObjectWithOptions(STWriter out, InstanceScope scope, Object o, Object[] options)

Write out an expression result that uses expression options. E.g., <names; separator=", ">

writePOJOback to summary
protected int writePOJO(STWriter out, InstanceScope scope, Object o, String[] options) throws IOException
zip_mapback to summary
protected ST.AttributeList zip_map(InstanceScope scope, List<Object> exprs, ST prototype)

Renders expressions of the form <names,phones:{n,p | ...}> or <a,b:t()>.

org.stringtemplate.v4 back to summary

protected Class Interpreter.ArgumentsMap

extends HashMap<String, Object>
Class Inheritance

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Inherited from java.util.HashMap:
clearclonecomputecomputeIfAbsentcomputeIfPresentcontainsKeycontainsValueentrySetforEachgetgetOrDefaultisEmptykeySetmergenewHashMapputputAllputIfAbsentremoveremovereplacereplacereplaceAllsizevalues

Constructor Detail

ArgumentsMapback to summary
protected ArgumentsMap()
org.stringtemplate.v4 back to summary

protected Class Interpreter.ObjectList

extends ArrayList<Object>
Class Inheritance

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Inherited from java.util.ArrayList:
addaddaddAlladdAlladdFirstaddLastclearclonecontainsensureCapacityequalsforEachgetgetFirstgetLasthashCodeindexOfisEmptyiteratorlastIndexOflistIteratorlistIteratorremoveremoveremoveAllremoveFirstremoveIfremoveLastremoveRangereplaceAllretainAllsetsizesortspliteratorsubListtoArraytoArraytrimToSize

Constructor Detail

ObjectListback to summary
protected ObjectList()
org.stringtemplate.v4 back to summary

public final Enum Interpreter.Option

extends Enum<Interpreter.Option>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final Interpreter.Option
public static final Interpreter.Option
public static final Interpreter.Option
public static final Interpreter.Option
public static final Interpreter.Option

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Interpreter.Option
public static Interpreter.Option[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

ANCHORback to summary
public static final Interpreter.Option ANCHOR
FORMATback to summary
public static final Interpreter.Option FORMAT
NULLback to summary
public static final Interpreter.Option NULL
SEPARATORback to summary
public static final Interpreter.Option SEPARATOR
WRAPback to summary
public static final Interpreter.Option WRAP

Constructor Detail

Optionback to summary
private Option()

Method Detail

valueOfback to summary
public static Interpreter.Option valueOf(String name)
valuesback to summary
public static Interpreter.Option[] values()