Class TemplateParser
Template
that can be processed later on.
It is a shift-reduce parser, meaning that it builds up the parse tree incrementally, bottom-up and left-to-right. It joins/compose templates when it detects the end of a parsing phase (end of property for instance).
The parser state has a stack of so-called template builders themselves having a stack of templates that will be joined together depending on the template builder type.
The parser drive the state by sequentially reading TemplateTokenizer.Token
from the lexer, moving to the
appropriate next state depending on the processed token and the current state.
The shift operation is about placing a Template
on top of the current template builder's stack.
The reduction operation pops out the current template builder, produce a composed template based on stacked templates of the popped builder, and then shift the resulting template on top of the new current builder.
In case the input source is null, it directly returns an empty template.
In case the input source contains no token and when no escaping is desired, it directly returns a textual template with the unmodified source.
In case of malformed source (missing CLOSING token for instance), this parser fallbacks
into an error reduction mode. It basically reduces the builders (starting from top of stack)
as Template
, offer them to the new top of stack builder, until there is no builder
anymore in the stack, with a special treatment when error-ing a property builder: transforming
the erroneous property into a sequence (inserting the &{
textual template in first position).
That will ensure that well-defined properties could be evaluated later on, while erroneous
ones are just treated as text (this was the behaviour of the old SubstitutionService
implementation).
-
Constructor Summary
-
Method Summary
-
Constructor Details
-
TemplateParser
public TemplateParser()
-
-
Method Details
-
parse
- Parameters:
source
- input string (may benull
)escaping
- escape the input string or not- Returns:
- the parsed template
-