|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object freemarker.template.compiler.StandardTemplateParser
public class StandardTemplateParser
Parses standard template language and generates
Instruction
s. Uses
ExpressionBuilder
to build expressions.
Field Summary | |
---|---|
protected static java.lang.String |
ASSIGN_END_TAG
|
protected static java.lang.String |
ASSIGN_TAG
|
protected static char |
BOOLEAN_ESCAPE_CHAR
|
protected static java.lang.String |
BREAK_TAG
|
protected static java.lang.String |
CALL_TAG
|
protected static java.lang.String |
CASE_TAG
|
protected static java.lang.String |
COMMENT_END_TAG
|
protected static java.lang.String |
COMMENT_TAG
|
protected static java.lang.String |
DEFAULT_TAG
|
protected static char |
DOUBLE_QUOTE_CHAR
|
protected static java.lang.String |
ELSE_IF_TAG
|
protected static java.lang.String |
ELSE_TAG
|
protected static java.lang.String |
EMPTY_LITERAL
|
protected static char |
END_TAG_START_CHAR
|
protected static char |
ESCAPE_CHAR
|
protected static java.lang.String |
EXIT_TAG
|
protected static java.lang.String |
FALSE_LITERAL
|
protected static java.lang.String |
FOREACH_END_TAG
|
protected static java.lang.String |
FOREACH_INDEX_KEYWORD
|
protected static java.lang.String |
FOREACH_TAG
|
protected int |
foundPos
The position at which the current instruction was found. |
protected static java.lang.String |
FUNCTION_END_TAG
|
protected static java.lang.String |
FUNCTION_TAG
|
protected static java.lang.String |
GLOBAL_KEYWORD
|
protected static char |
HASH_LITERAL_END_CHAR
|
protected static char |
HASH_LITERAL_START_CHAR
|
protected static java.lang.String |
IF_END_TAG
|
protected static java.lang.String |
IF_TAG
|
protected static java.lang.String |
INCLUDE_TAG
|
protected static java.lang.String |
LAMBDA_KEYWORD
|
protected static java.lang.String |
LIST_END_TAG
|
protected static java.lang.String |
LIST_INDEX_KEYWORD
|
protected static char |
LIST_LITERAL_END_CHAR
|
protected static java.lang.String |
LIST_LITERAL_RANGE
|
protected static char |
LIST_LITERAL_START_CHAR
|
protected static java.lang.String |
LIST_TAG
|
protected static java.lang.String |
LOCAL_END_TAG
|
protected static java.lang.String |
LOCAL_KEYWORD
|
protected static java.lang.String |
LOCAL_TAG
|
protected static int |
LONG_OPERATOR_LENGTH
Length of operators that are more than one character long. |
protected static int |
MAX_TAG_NAME_LENGTH
|
protected Instruction |
nextFMInstruction
The next non-text instruction found by the parser. |
protected static java.lang.String |
NOPARSE_TAG
|
protected static java.lang.String |
NOPARSE_TAG_END
|
protected int |
parsePos
The current parse position. |
protected int |
previousParsePos
The parse position before the current instruction was found. |
protected static char |
QUOTE_CHAR
|
protected static java.lang.String |
SWITCH_END_TAG
|
protected static java.lang.String |
SWITCH_TAG
|
protected static char |
TAG_END_CHAR
|
protected static char |
TAG_START_CHAR
|
protected FunctionTemplateProcessor |
template
The Template being parsed. |
protected java.lang.String |
text
The text to be parsed. |
protected int |
textLen
The number of characters in the text. |
protected static java.lang.String |
TRANSFORM_END_TAG
|
protected static java.lang.String |
TRANSFORM_TAG
|
protected static java.lang.String |
TRUE_LITERAL
|
protected static char |
VAR_INSTR_END_CHAR
|
protected static char |
VAR_INSTR_START_CHAR
|
protected static java.lang.String |
VAR_INSTR_START_CHARS
|
Constructor Summary | |
---|---|
StandardTemplateParser()
Default constructor. |
|
StandardTemplateParser(FunctionTemplateProcessor template,
java.lang.String text)
Constructs a new StandardTemplateParser with the given
template and text to be parsed. |
Method Summary | |
---|---|
java.lang.String |
atChar()
Adds text to an error message indicating the line number where the error was found. |
protected java.lang.String |
atChar(int pos)
Adds text to an error message indicating the line number where the error was found. |
protected boolean |
findTagEnd()
Requires a TAG_END_CHAR , optionally preceded by whitespace,
and advances parsePos after the TAG_END_CHAR . |
protected void |
findTagNameEnd()
Advances parsePos through any remaining alphanumeric
characters. |
Instruction |
getNextInstruction()
Searches the text for an instruction, starting at the current parse position. |
protected Instruction |
getNextInstructionTag()
Searches the text for a tagged instruction, starting at the current parse position. |
protected static boolean |
isIdentifierStartChar(char c)
Determines whether a character is legal at the start of an identifier. |
boolean |
isMoreInstructions()
Are there any more instructions left to be parsed? |
protected Instruction |
parseAssign()
Parses an AssignInstruction or
AssignBlockInstruction 's tag. |
protected boolean |
parseBinaryElement(java.util.List<Expression> elements)
Parse an optional binary element. |
protected Expression |
parseBooleanLiteral()
Parses a BooleanLiteral or an
EmptyLiteral . |
protected CallInstruction |
parseCall()
Parses a CallInstruction 's tag. |
protected CaseInstruction |
parseCase()
Parses a CaseInstruction 's tag. |
protected Dot |
parseDot()
Parses the Dot operator. |
protected DynamicKeyName |
parseDynamicKeyName()
Parses a DynamicKeyName . |
protected java.util.List<Expression> |
parseElements()
Retrieve the next Expression (s)
following parsePos . |
protected IfInstruction |
parseElseIf()
Parses an IfInstruction
<elseif> tag. |
protected Expression |
parseExpression()
Parses and builds an Expression ,
which may also be a sub-expression. |
protected ListInstruction |
parseForeachStart()
Parses a ListInstruction 's
start tag with the "foreach" keyword. |
protected FunctionInstruction |
parseFunction()
Parses a FunctionInstruction 's tag. |
protected Expression |
parseHashLiteral()
Parses a HashLiteral . |
protected Identifier |
parseIdentifier()
Tries to parse an Identifier . |
protected IfElseInstruction |
parseIfStart()
Parses an IfElseInstruction 's start tag. |
protected IncludeInstruction |
parseInclude()
Parses an IncludeInstruction 's tag. |
protected Expression |
parseListLiteral()
Parses a ListLiteral . |
protected ListInstruction |
parseListStart()
Parses a ListInstruction 's start tag. |
protected LocalInstruction |
parseLocalStart()
|
protected MethodCall |
parseMethodCall()
Parses a MethodCall . |
protected Expression |
parseNumberLiteral()
Parses a NumberLiteral . |
protected Expression |
parseStringLiteral()
Parses a StringLiteral . |
protected SwitchInstruction |
parseSwitch()
Parses a SwitchInstruction 's tag. |
protected TransformInstruction |
parseTransformStart()
Parses a TransformInstruction 's tag. |
protected Expression |
parseUnaryBinaryExpression()
Parses and builds a unary or binary Expression ,
which may also be a sub-expression. |
protected Variable |
parseVariable()
Parses an Expression and ensures
that it's a Variable . |
protected Variable |
parseVariableElement()
Retrieve the next Variable
following parsePos , and ensure its a
Variable . |
protected VariableInstruction |
parseVariableInstruction()
Parses a VariableInstruction . |
protected Expression |
parseVariableOrList()
Parses either a variable name or a list literal. |
protected void |
requireChar(char c)
Requires a given character, optionally preceded by by whitespace. |
protected void |
requireWhitespace()
Advances parsePos beyond required whitespace. |
void |
setTemplate(FunctionTemplateProcessor template)
Sets the template to receive the parsed instructions. |
void |
setText(java.lang.String text)
Sets the text to be parsed. |
protected boolean |
skipChar(char c)
Advances beyond any whitespace; then, if the next character matches a given character, advances beyond it and returns true ,
otherwise returns false . |
protected boolean |
skipKeyword(java.lang.String keyword)
Skip over a given keyword. |
java.lang.String |
skipToEndInstruction(ContainerInstruction beginInstruction)
Searches the text for a matching end instruction, starting at the current parse position. |
protected boolean |
skipToTagEnd()
Requires a TAG_END_CHAR , optionally preceded by whitespace,
and advances parsePos to the TAG_END_CHAR . |
protected void |
skipWhitespace()
Advances parsePos beyond any whitespace. |
java.lang.String |
toString()
Returns a string representation of the object. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected java.lang.String text
protected int textLen
protected FunctionTemplateProcessor template
protected int parsePos
protected int previousParsePos
protected int foundPos
protected Instruction nextFMInstruction
protected static final int MAX_TAG_NAME_LENGTH
protected static final java.lang.String VAR_INSTR_START_CHARS
protected static final char VAR_INSTR_START_CHAR
protected static final char VAR_INSTR_END_CHAR
protected static final java.lang.String LIST_TAG
protected static final java.lang.String LIST_INDEX_KEYWORD
protected static final java.lang.String LIST_END_TAG
protected static final java.lang.String IF_TAG
protected static final java.lang.String ELSE_TAG
protected static final java.lang.String ELSE_IF_TAG
protected static final java.lang.String IF_END_TAG
protected static final java.lang.String SWITCH_TAG
protected static final java.lang.String SWITCH_END_TAG
protected static final java.lang.String CASE_TAG
protected static final java.lang.String BREAK_TAG
protected static final java.lang.String DEFAULT_TAG
protected static final java.lang.String ASSIGN_TAG
protected static final java.lang.String ASSIGN_END_TAG
protected static final java.lang.String INCLUDE_TAG
protected static final java.lang.String FUNCTION_TAG
protected static final java.lang.String FUNCTION_END_TAG
protected static final java.lang.String CALL_TAG
protected static final java.lang.String EXIT_TAG
protected static final java.lang.String LOCAL_TAG
protected static final java.lang.String LOCAL_END_TAG
protected static final char TAG_START_CHAR
protected static final char TAG_END_CHAR
protected static final char END_TAG_START_CHAR
protected static final char QUOTE_CHAR
protected static final char DOUBLE_QUOTE_CHAR
protected static final char ESCAPE_CHAR
protected static final char BOOLEAN_ESCAPE_CHAR
protected static final char LIST_LITERAL_START_CHAR
protected static final char LIST_LITERAL_END_CHAR
protected static final char HASH_LITERAL_START_CHAR
protected static final char HASH_LITERAL_END_CHAR
protected static final java.lang.String LIST_LITERAL_RANGE
protected static final java.lang.String COMMENT_TAG
protected static final java.lang.String COMMENT_END_TAG
protected static final java.lang.String NOPARSE_TAG
protected static final java.lang.String NOPARSE_TAG_END
protected static final java.lang.String FOREACH_TAG
protected static final java.lang.String FOREACH_INDEX_KEYWORD
protected static final java.lang.String FOREACH_END_TAG
protected static final java.lang.String TRANSFORM_TAG
protected static final java.lang.String TRANSFORM_END_TAG
protected static final java.lang.String TRUE_LITERAL
protected static final java.lang.String FALSE_LITERAL
protected static final java.lang.String EMPTY_LITERAL
protected static final java.lang.String LAMBDA_KEYWORD
protected static final java.lang.String LOCAL_KEYWORD
protected static final java.lang.String GLOBAL_KEYWORD
protected static final int LONG_OPERATOR_LENGTH
Constructor Detail |
---|
public StandardTemplateParser()
public StandardTemplateParser(FunctionTemplateProcessor template, java.lang.String text)
StandardTemplateParser
with the given
template and text to be parsed.
template
- a new template that will received the parsed instructionstext
- the text to be parsedMethod Detail |
---|
public void setText(java.lang.String text)
text
- the text to be parsed.public void setTemplate(FunctionTemplateProcessor template)
template
- the template being parsed.public Instruction getNextInstruction() throws ParseException
Searches the text for an instruction, starting at the current
parse position. If one is found, parses it into an
Instruction
. Before changing
parsePos
, sets previousParsePos = parsePos
.
If no instruction is found, leaves parsePos
unchanged.
getNextInstruction
in interface TemplateParser
Instruction
representing the
next instruction following parsePos
, or null
if none is found.
ParseException
- the next instruction couldn't be parsedprotected Instruction getNextInstructionTag() throws ParseException
Searches the text for a tagged instruction, starting at the current
parse position. If one is found, parses it into a
Instruction
. Before changing
parsePos
, sets previousParsePos = parsePos
.
If no instruction is found, parsePos
will be equal
to textLen
, which will be equal to foundPos
.
Instruction
representing the
next instruction following parsePos
, or null
if none is found.
ParseException
- the next instruction couldn't be parsedpublic boolean isMoreInstructions()
isMoreInstructions
in interface TemplateParser
true
if there is more text to parse, otherwise
false
public java.lang.String skipToEndInstruction(ContainerInstruction beginInstruction)
Searches the text for a matching end instruction, starting at the
current parse position. If we find it, parse it and return. Before
changing parsePos
, should set previousParsePos =
parsePos
.
If no instruction is found, set parsePos
to
textLen
which equals foundPos
.
skipToEndInstruction
in interface TemplateParser
String
containing the intermediate text if we
find the end instruction we're after, otherwise null
public java.lang.String atChar()
atChar
in interface TemplateParser
String
containing the message.protected void findTagNameEnd()
parsePos
through any remaining alphanumeric
characters. Leaves parsePos
unchanged if not found.
We check for the maximum length that a FM-Classic tag name could be.
This is a simple optimization to ensure that we don't end up with
massively long tag names being returned. Note that we intentionally
go one past the maximum length, to ensure we have a complete name.
protected boolean findTagEnd() throws ParseException
TAG_END_CHAR
, optionally preceded by whitespace,
and advances parsePos
after the TAG_END_CHAR
.
true
if we found the end of the tag, otherwise false
ParseException
- an error occurred while scanning for the end tagprotected boolean skipToTagEnd() throws ParseException
TAG_END_CHAR
, optionally preceded by whitespace,
and advances parsePos
to the TAG_END_CHAR
.
true
if we found the end of the tag, otherwise false
ParseException
- an error occurred while scanning for the end tagprotected java.lang.String atChar(int pos)
String
containing the message.protected Expression parseExpression() throws ParseException
Expression
,
which may also be a sub-expression. This method deals explicitly with
ternary expressions.
Expression
ParseException
protected Expression parseUnaryBinaryExpression() throws ParseException
Expression
,
which may also be a sub-expression. An expression in parenthesis is
considered a sub-expression.
Expression
.
ParseException
protected java.util.List<Expression> parseElements() throws ParseException
Expression
(s)
following parsePos
.
Expression
s containing any
elements encountered
ParseException
- something went wrong during parsingprotected boolean parseBinaryElement(java.util.List<Expression> elements) throws ParseException
elements
- a list of elements to which we add any elements parsed
true
if a binary operator was found, otherwise
false
ParseException
- something went wrong during parsingprotected DynamicKeyName parseDynamicKeyName() throws ParseException
DynamicKeyName
.
Expects parsePos
to be on the open bracket.
DynamicKeyName
.
ParseException
protected Dot parseDot() throws ParseException
Dot
operator.
Expects parsePos
to be one character beyond the dot itself.
Dot
.
ParseException
protected Expression parseStringLiteral() throws ParseException
StringLiteral
.
Expects parsePos
to be on the open quote. This is so
we can determine whether a single- or double-quote was used to open
the String, and thus what close quote we should look for.
StringLiteral
.
ParseException
protected Expression parseListLiteral() throws ParseException
ListLiteral
.
Expects parsePos
to be just beyond open square bracket.
ListLiteral
.
ParseException
protected Expression parseHashLiteral() throws ParseException
HashLiteral
.
Expects parsePos
to be just beyond the open brace.
HashLiteral
.
ParseException
protected Expression parseNumberLiteral() throws ParseException
NumberLiteral
.
Expects parsePos
to be on the first digit or optional
minus sign.
NumberLiteral
.
ParseException
protected Expression parseBooleanLiteral() throws ParseException
BooleanLiteral
or an
EmptyLiteral
.
Expects parsePos
to be immediately following the '#' symbol.
BooleanLiteral
.
ParseException
protected Variable parseVariable() throws ParseException
Expression
and ensures
that it's a Variable
.
Variable
.
ParseException
protected Variable parseVariableElement() throws ParseException
Variable
following parsePos
, and ensure its a
Variable
.
Variable
, or null
if none is found.
ParseException
protected Identifier parseIdentifier() throws ParseException
Identifier
.
Skips any whitespace prior to the identifier.
Identifier
.
ParseException
protected static boolean isIdentifierStartChar(char c)
Determines whether a character is legal at the start of an identifier. An identifier is either something like a tag name, such as "foreach", or a variable name, such as "myHash".
In this implementation, an identifier can only start with either a letter or an underscore.
Note that this method does not affect identifiers inside a dynamic key name.
protected MethodCall parseMethodCall() throws ParseException
MethodCall
.
MethodCall
object initialized with the arguments.
ParseException
protected Expression parseVariableOrList() throws ParseException
ParseException
- the next expression element couldn't be parsedprotected VariableInstruction parseVariableInstruction() throws ParseException
VariableInstruction
.
Expects parsePos
to be at the beginning of the
VAR_INSTR_START_CHARS
.
VariableInstruction
.
ParseException
protected ListInstruction parseListStart() throws ParseException
ListInstruction
's start tag.
ListInstruction
initialized with the values
from the tag.
ParseException
protected ListInstruction parseForeachStart() throws ParseException
ListInstruction
's
start tag with the "foreach" keyword.
ListInstruction
initialized with the values
from the tag.
ParseException
protected IfElseInstruction parseIfStart() throws ParseException
IfElseInstruction
's start tag.
IfElseInstruction
initialized with the expression
in the tag.
ParseException
protected IfInstruction parseElseIf() throws ParseException
IfInstruction
<elseif> tag.
IfInstruction
initialised with the expression
in the tag.
ParseException
protected Instruction parseAssign() throws ParseException
AssignInstruction
or
AssignBlockInstruction
's tag.
Determines whether the assign is an expression assignment or a block
assignment and returns the appropriate instruction.
AssignInstruction
or
AssignBlockInstruction
initialized with the values from
the tag.
ParseException
protected IncludeInstruction parseInclude() throws ParseException
IncludeInstruction
's tag.
IncludeInstruction
initialized with the name
in the tag.
ParseException
protected SwitchInstruction parseSwitch() throws ParseException
SwitchInstruction
's tag.
SwitchInstruction
initialized with the expression
in the tag.
ParseException
protected CaseInstruction parseCase() throws ParseException
CaseInstruction
's tag.
CaseInstruction
initialized with the expression
in the tag.
ParseException
protected FunctionInstruction parseFunction() throws ParseException
FunctionInstruction
's tag.
FunctionInstruction
intialized with the
argument names in the tag.
ParseException
protected CallInstruction parseCall() throws ParseException
CallInstruction
's tag.
This essentially looks like a method call, so we parse it the same way.
CallInstruction
initialized with the arguments
in the tag.
ParseException
protected TransformInstruction parseTransformStart() throws ParseException
TransformInstruction
's tag.
This tag consists of one parameter: the variable representing the
TemplateTransformModel
to be used for the
transformation.
TransformInstruction
ParseException
protected LocalInstruction parseLocalStart() throws ParseException
ParseException
protected boolean skipChar(char c) throws ParseException
true
,
otherwise returns false
.
true
if the character was found, otherwise false
ParseException
protected void requireChar(char c) throws ParseException
ParseException
- the required character couldn't be foundprotected boolean skipKeyword(java.lang.String keyword) throws ParseException
keyword
- the keyword to skip over
ParseException
- there are no more characters
before the keyword is expected.protected void skipWhitespace() throws ParseException
parsePos
beyond any whitespace.
ParseException
- there are no more characters
after whitespace has been skipped.protected void requireWhitespace() throws ParseException
parsePos
beyond required whitespace.
ParseException
- there are no more characters
after whitespace has been skipped, or if no whitespace
could be found.public java.lang.String toString()
toString
in class java.lang.Object
String
representation of the object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |