public class TemplateParser extends Object implements TemplateParserConstants
Important: Do not use outside of Soy code (treat as superpackage-private).
This parser parses the content (header and body) of a Soy template.
Header:
1. Comments:
+ Comments are only allowed outside of Soy tags.
+ Standard "//" for a rest-of-line comment. Must appear at start of line or after a space.
+ Standard slash-star (/*) ... star-slash (*/) for a block comment.
+ Doc comments are not allowed, except when attached to a valid declaration.
2. Param declaration:
+ Soy tag with command name "@param" and command text "key: type".
+ Optional desc string is written as a block doc comment, which either must
precede the param tag, or must start on the same line as the end of the '@param' tag.
+ Examples:
/** A list of numbers. */
/**
A list of numbers. */
3. Injected param declaration:
+ Works exactly like @param except that parameter values are taken from the
implicit $ij scope.
+ Soy tag with command name "@inject" and command text "key: type".
+ Optional desc string is written as a block doc comment, which either must
precede the param tag, or must start on the same line as the end of the '@inject' tag.
+ Examples:
/** A list of numbers. */
/**
A list of numbers. */
Body:
1. Comments:
+ Comments are only allowed outside of Soy tags.
+ Standard "//" for a rest-of-line comment. Must appear at start of line or after a space.
+ Standard slash-star (/*) ... star-slash (*/) for a block comment.
2. Soy tag format:
+ Can be delimited by single braces "{...}" or double braces "{{...}}".
+ Soy tags delimited by double braces are allowed to contain single braces within.
+ Some Soy tags are allowed to end in "/}" or "/}}" to denote immediate ending of a block.
+ It is an error to use "/}" or "/}}" when it's not applicable to the command.
+ If there is a command name, it must come immediately after the opening delimiter.
+ The command name must be followed by either the closing delimiter (if the command does not
take any command text) or a whitespace (if the command takes command text).
+ It is an error to provide command text when it's not applicable, and vice versa.
+ This parser does not parse command text (that will be separate).
Examples:
{print $boo} // explicit 'print' command
{$boo.foo} // implicit 'print' command
{printer} // implicit 'print' command (the prefix 'print' here is not a command name)
{\n} // a command that doesn't take any command text
{call .gooMoo data="all" /} // self-ending block
{call .gooMoo data="all"}...{/call} // block with separate start and end tags
3. Raw text:
+ Raw text is fixed text that will be part of the template output. There are 3 types.
+ Any text outside of Soy tags is raw text.
+ There are 7 special character commands that produce raw text strings:
{sp} = space {nil} = empty string {\n} = newline (line feed) {\r} = carriage return
{\t} = tab {lb} = left brace {rb} = right brace
+ A section of raw text (may contain braces) can be enclosed within a 'literal' block:
{literal}...{/literal}
4. Msg blocks:
+ A block between 'msg' and '/msg' tags represents a message for translation.
+ It is an error to nest 'msg' blocks.
+ Within a 'msg' block, the parsing of Soy tags is the same. The only difference is that we
also recognize "<" and ">" as opening and closing an HTML tag. This is because each
HTML tag as a whole needs to be turned into a single placeholder in the message.
+ A 'msg' block may have a 'plural' or 'select' block as its only content.
+ A 'msg' block may be followed by optional additional 'fallbackmsg' blocks.
Example:
{msg desc="Event title."}
Join event <a href="{$event.url}">{$event.title}</a>.
{fallbackmsg desc="Event title."}
Join event {$event.title}.
{/msg}
5. Other Soy commands:
{print ...}
{...} // implied 'print' command
{xid ...}
{css ...}
{let ... /}
{let ...}...{/let}
{if ...}...{elseif ...}...{else ...}...{/if}
{switch ...}{case ...}...{default}...{/switch}
{foreach ...}...{ifempty}...{/foreach}
{for ...}...{/for}
{call ... /}
{delcall ... /}
{call ...}{param ... /}{param ...}...{/param}{/call}
{delcall ...}{param ... /}{param ...}...{/param}{/delcall}
{log}...{/log}
{debugger}
6. Misc:
+ The following commands are not allowed to appear in a template:
{namespace ...} {template ...} {/template}
| Modifier and Type | Field and Description |
|---|---|
Token |
jj_nt
Next token.
|
Token |
token
Current token.
|
TemplateParserTokenManager |
token_source
Generated Token Manager.
|
AFTER_CMD_NAME_1, AFTER_CMD_NAME_2, AFTER_SOY_TAG_OPEN, ANY_CHAR, BLOCK_COMMENT_CHAR, BLOCK_DOC_COMMENT, BLOCK_DOC_COMMENT_START, BLOCK_NONDOC_COMMENT, BLOCK_NONDOC_COMMENT_START, BRACE, CMD_NAME_ANY_CALL, CMD_NAME_CASE, CMD_NAME_CR, CMD_NAME_CSS, CMD_NAME_DEBUGGER, CMD_NAME_DEFAULT, CMD_NAME_ELSE, CMD_NAME_ELSEIF, CMD_NAME_END_ANY_CALL, CMD_NAME_END_FOR, CMD_NAME_END_FOREACH, CMD_NAME_END_IF, CMD_NAME_END_LET, CMD_NAME_END_LITERAL, CMD_NAME_END_LOG, CMD_NAME_END_MSG, CMD_NAME_END_PARAM, CMD_NAME_END_PLURAL, CMD_NAME_END_SELECT, CMD_NAME_END_SWITCH, CMD_NAME_END_TEMPLATE, CMD_NAME_FALLBACKMSG, CMD_NAME_FOR, CMD_NAME_FOREACH, CMD_NAME_IF, CMD_NAME_IFEMPTY, CMD_NAME_INJECTED_PARAM_DECL, CMD_NAME_LB, CMD_NAME_LET, CMD_NAME_LF, CMD_NAME_LITERAL, CMD_NAME_LOG, CMD_NAME_MSG, CMD_NAME_NAMESPACE, CMD_NAME_NIL, CMD_NAME_PARAM, CMD_NAME_PARAM_DECL, CMD_NAME_PLURAL, CMD_NAME_PRINT, CMD_NAME_RB, CMD_NAME_SELECT, CMD_NAME_SP, CMD_NAME_SWITCH, CMD_NAME_TAB, CMD_NAME_TEMPLATE, CMD_NAME_XID, CMD_TEXT_CHAR_1, CMD_TEXT_CHAR_2, CMD_TEXT_DIRECTIVE_NAME, CMD_TEXT_PHNAME_ATTR, DEFAULT, DEFAULT_IN_MSG_BLOCK_AT_SOL, DEFAULT_IN_MSG_BLOCK_NOT_SOL, DEFAULT_NOT_SOL, EOF, IDENT, IN_BLOCK_DOC_COMMENT, IN_BLOCK_NONDOC_COMMENT, IN_CMD_TEXT_1, IN_CMD_TEXT_2, IN_LITERAL_BLOCK, LINE_COMMENT_AT_SOL, LINE_COMMENT_NOT_SOL, LITERAL_RAW_TEXT_CONTENT, MSG_HTML_TAG_CLOSE, MSG_HTML_TAG_OPEN, NL, NOT_BRACE, NOT_CMD_NAME_BOUNDARY_AFTER_CMD_NAME, NOT_NL, NOT_WS, QMARK, REG_SOY_TAG_CLOSE_AFTER_CMD_NAME_1, REG_SOY_TAG_CLOSE_AFTER_CMD_NAME_2, REG_SOY_TAG_CLOSE_AFTER_CMD_TEXT_1, REG_SOY_TAG_CLOSE_AFTER_CMD_TEXT_2, SELF_ENDING_SOY_TAG_CLOSE_AFTER_CMD_NAME_1, SELF_ENDING_SOY_TAG_CLOSE_AFTER_CMD_NAME_2, SELF_ENDING_SOY_TAG_CLOSE_AFTER_CMD_TEXT_1, SELF_ENDING_SOY_TAG_CLOSE_AFTER_CMD_TEXT_2, SOY_TAG_OPEN_1, SOY_TAG_OPEN_2, TOKEN_NL, TOKEN_NOT_WS, TOKEN_WS_NOT_NL, tokenImage, WS, WS_AFTER_CMD_NAME, WS_CHAR, WS_NOT_NL, XXX_BRACE_AFTER_SOY_TAG_OPEN, XXX_BRACE_BEFORE_SOY_TAG_CLOSE, XXX_CMD_NAME_INVALID_DECL, XXX_CMD_TEXT_PHNAME_NOT_IDENT, XXX_DOUBLE_LEFT_BRACE_IN_SOY_TAG_2, XXX_DOUBLE_RIGHT_BRACE_IN_SOY_TAG_1, XXX_INVALID_END_LITERAL_TAG, XXX_LEFT_BRACE_IN_SOY_TAG_1, XXX_NESTED_LITERAL_TAG, XXX_NO_CMD_NAME, XXX_UNMATCHED_RIGHT_BRACE_1, XXX_UNMATCHED_RIGHT_BRACE_2| Constructor and Description |
|---|
TemplateParser(IdGenerator nodeIdGen,
Reader input,
String filePath,
int templateContentStartLine,
ErrorReporter errorReporter)
Constructor that takes a reader object providing the input.
|
TemplateParser(IdGenerator nodeIdGen,
String input,
String filePath,
int templateContentStartLine)
Constructor that takes a string input.
|
TemplateParser(IdGenerator nodeIdGen,
String input,
String filePath,
int templateContentStartLine,
ErrorReporter errorReporter)
Constructor that takes a string input.
|
TemplateParser(InputStream stream)
Constructor with InputStream.
|
TemplateParser(InputStream stream,
String encoding)
Constructor with InputStream and supplied encoding
|
TemplateParser(Reader stream)
Constructor.
|
TemplateParser(TemplateParserTokenManager tm)
Constructor with generated Token Manager.
|
| Modifier and Type | Method and Description |
|---|---|
void |
disable_tracing()
Disable tracing.
|
void |
enable_tracing()
Enable tracing.
|
ParseException |
generateParseException()
Generate ParseException.
|
Token |
getNextToken()
Get the next Token.
|
Token |
getToken(int index)
Get the specific Token.
|
void |
MaybeWhitespace(String errorMessage)
Matches empty string or BasicRawText, and if the latter, then ensures it's all whitespace.
|
TemplateParseResult |
parseTemplateContent()
Attempts to parse the input as the content of a Soy template (header and body).
|
void |
ReInit(InputStream stream)
Reinitialise.
|
void |
ReInit(InputStream stream,
String encoding)
Reinitialise.
|
void |
ReInit(Reader stream)
Reinitialise.
|
void |
ReInit(TemplateParserTokenManager tm)
Reinitialise.
|
public TemplateParserTokenManager token_source
public Token token
public Token jj_nt
public TemplateParser(IdGenerator nodeIdGen, Reader input, String filePath, int templateContentStartLine, ErrorReporter errorReporter)
nodeIdGen - The node id generator for the tree being built.input - The input to parse. This reader will not be closed by this parser.filePath - File path or uri describing where the input is from. Used for reporting.templateContentStartLine - The start line for input, 1-indexed.errorReporter - For reporting errors.public TemplateParser(IdGenerator nodeIdGen, String input, String filePath, int templateContentStartLine, ErrorReporter errorReporter)
nodeIdGen - The node id generator for the tree being built.input - The input to parse.filePath - File path or uri describing where the input is from. Used for reporting.templateContentStartLine - The start line for input, 1-indexed.errorReporter - For reporting errors.public TemplateParser(IdGenerator nodeIdGen, String input, String filePath, int templateContentStartLine)
nodeIdGen - The node id generator for the tree being built.input - The input to parse.filePath - File path or uri describing where the input is from. Used for reporting.templateContentStartLine - The start line for input, 1-indexed.public TemplateParser(InputStream stream)
public TemplateParser(InputStream stream, String encoding)
public TemplateParser(Reader stream)
public TemplateParser(TemplateParserTokenManager tm)
public TemplateParseResult parseTemplateContent() throws TokenMgrError, ParseException
TokenMgrError - When the input has a token error.ParseException - When the input has a parse error.public final void MaybeWhitespace(String errorMessage) throws ParseException
ParseExceptionpublic void ReInit(InputStream stream)
public void ReInit(InputStream stream, String encoding)
public void ReInit(Reader stream)
public void ReInit(TemplateParserTokenManager tm)
public final Token getNextToken()
public final Token getToken(int index)
public ParseException generateParseException()
public final void enable_tracing()
public final void disable_tracing()