public class CompilationContext extends Object
The main responsibility is to collect all errors or warnings and to provide the stack context (the names of all arguments and local variables).
Constructor and Description |
---|
CompilationContext(Template template,
CompilationContext parent)
Creates a new context for the given template and parent context.
|
Modifier and Type | Method and Description |
---|---|
void |
error(parsii.tokenizer.Position pos,
String message,
Object... params)
Records an error for the given position.
|
Optional<sirius.kernel.commons.Tuple<Class<?>,Integer>> |
findGlobal(String variableName)
Tries to resolve a variable name into an environment index.
|
Optional<sirius.kernel.commons.Tuple<Class<?>,Integer>> |
findLocal(String variableName)
Tries to resolve a variable name into a stack location.
|
TagHandler |
findTagHandler(parsii.tokenizer.Char position,
String tagName)
Resolves a tag name like i:if into a
TagHandler . |
List<parsii.tokenizer.ParseError> |
getErrors()
Returns all errors and warnings collected during the compilation.
|
int |
getStackDepth()
Returns the maximum stack depth required when rendering this template.
|
Template |
getTemplate()
Returns the template being compiled.
|
Emitter |
inlineTemplate(parsii.tokenizer.Position startOfTag,
Template template,
Function<String,Expression> arguments,
Function<String,Emitter> blocks)
Inlines the requested template into the current one.
|
Emitter |
invokeTemplate(parsii.tokenizer.Position position,
Template template,
Function<String,Expression> arguments,
Map<String,Emitter> blocks)
Generates an emitter which invokes the given template at runtime.
|
void |
pop(parsii.tokenizer.Position position)
Pops a local off the stack.
|
int |
push(parsii.tokenizer.Position position,
String name,
Class<?> type)
Pushes a local variable on the stack, so that it can be resolved later.
|
Class<?> |
resolveClass(parsii.tokenizer.Position position,
String typeName)
Resolve a string (name) into a java class and reports an error if no matching class was found.
|
Optional<Template> |
resolveTemplate(parsii.tokenizer.Position position,
String name)
Resolves the given name into a template.
|
int |
stackAlloc()
Allocates a stack position for an argument or local variable.
|
String |
toString() |
Optional<Class<?>> |
tryResolveClass(String typeName)
Tries to resolve a string (name) into a java class.
|
void |
warning(parsii.tokenizer.Position pos,
String message,
Object... params)
Records a warning for the given position.
|
public CompilationContext(@Nonnull Template template, @Nullable CompilationContext parent)
template
- the template which is being compiledparent
- the parent context if the template is compiled because it is invoked by another template being
compiledpublic Template getTemplate()
public int stackAlloc()
For known and resolveable variables, this is automatically handled by push(Position, String, Class)
.
However, when a template is inlined into another, we also have to transfer locals to a new stack location and might also create new intermediate locals for now inlined arguments (because we must not evaluate non-constant expressions more thant once).
public int push(parsii.tokenizer.Position position, String name, Class<?> type)
position
- the position where the local is defined - mainly used for error reportingname
- the name of the variabletype
- the type of the variablepublic void pop(parsii.tokenizer.Position position)
Note that this will only reduce the visibility of the variable but not free up the technical stack location. We only used each stack location once, to greatly simplify inlining.
position
- the position which caused the pop - mainly used for error reportingpublic int getStackDepth()
public Optional<sirius.kernel.commons.Tuple<Class<?>,Integer>> findLocal(String variableName)
variableName
- the name of the variablepublic Optional<sirius.kernel.commons.Tuple<Class<?>,Integer>> findGlobal(String variableName)
variableName
- the variable to resolvepublic void error(parsii.tokenizer.Position pos, String message, Object... params)
pos
- the position where the error occuredmessage
- the message to showparams
- the formatting parameters applied to the messagepublic void warning(parsii.tokenizer.Position pos, String message, Object... params)
pos
- the position where the warning occuredmessage
- the message to showparams
- the formatting parameters applied to the messagepublic List<parsii.tokenizer.ParseError> getErrors()
public TagHandler findTagHandler(parsii.tokenizer.Char position, String tagName)
TagHandler
.
There are mainly two types of tags. Built in ones, which start with i: and user defined tag libraries which start with an arbitrary prefix.
position
- the position where the tag was detectedtagName
- the name of the tag to resolvepublic Optional<Template> resolveTemplate(parsii.tokenizer.Position position, String name) throws CompileException
If the template isn't compiled yet and directly or indirectly references the template currently being compiled, an appropriate exception is thrown to avoid infinite recursion.
position
- the position where the template was referencedname
- the name of the template to resolveCompileException
- if case of a compilation error when compiling the referenced templatepublic Emitter invokeTemplate(parsii.tokenizer.Position position, Template template, Function<String,Expression> arguments, Map<String,Emitter> blocks)
Note that only the template name, not the template itself is stored, so that modifications of the referenced template will be detected and applied.
position
- the position where the invokation took placetemplate
- the template to callarguments
- the arguments passed to the templateblocks
- the emitter blocks passed to the templatepublic Emitter inlineTemplate(parsii.tokenizer.Position startOfTag, Template template, Function<String,Expression> arguments, Function<String,Emitter> blocks)
Note that the current state and content of the given template is used and that no further modifications of the referenced template are reflected (until the current template is recompiled). So this should only be used for static templates, not for ones modified by the user.
Note however, that aggressive optimizations are enabled by this approach as all constant arguments are propagated to the referenced template. Also all emitter blocks passed to the template are resolved and propagated during compilation.
As constant expressions and constant conditional emitters are optimized during the last phase of the compilation, this might yield in a much faster and more condense final template, especially if many configuration flags, as e.g. in the wondergem components, are present.
startOfTag
- the position where the invokation took placetemplate
- the template to callarguments
- the arguments passed to the templateblocks
- the emitter blocks passed to the templatepublic Class<?> resolveClass(parsii.tokenizer.Position position, String typeName)
Next to the classic Class.forName this also supports aliases like String for java.lang.String.
position
- the position for error reportingtypeName
- the type name to resolveTagliatelle.getClassAliases()
public Optional<Class<?>> tryResolveClass(String typeName)
Next to the classic Class.forName this also supports aliases like String for java.lang.String.
typeName
- the type name to resolveTagliatelle.getClassAliases()
Copyright © 2018. All rights reserved.