public class SoyFileParser extends Object
Important: Do not use outside of Soy code (treat as superpackage-private).
This parser parses the following Soy file structure:
1. Delegate package (delpackage):
+ Optional.
+ The file must contain 0 or 1 delpackage declaration.
+ It must appear before the namespace declaration.
+ It must appear on its own line and start at the start of a line.
Example: {delpackage MySecretFeature}
2. Namespace:
+ The file must contain exactly one namespace declaration.
+ It must appear before any templates.
+ It must appear on its own line and start at the start of a line.
Examples:
{namespace boo.foo}
{namespace boo.foo autoescape="..."}
3. Alias:
+ Alias declarations must appear after the namespace declaration.
+ They must appear before any templates.
+ Each must appear on its own line and start at the start of a line.
Examples:
{alias boo.foo.goo.moo}
{alias boo.foo.goo.moo as zoo}
4. SoyDoc:
+ Starts with slash-star-star (/**) and ends with star-slash (*/) like JavaDoc.
+ SoyDoc must appear on its own line(s) and start at the start of a line.
+ Currently recognizes two tags: "@param keyName" and "@param? optionalKeyName".
Example:
/**
* @param boo Something scary.
* @param? goo Something slimy (optional).
*/
5. Template:
+ Each template must be immediately preceded by a SoyDoc block.
+ The 'template' tag and the '/template' tag much each appear on its own line(s) and start
at the start of a line.
+ The template content is parsed by TemplateParser.jj.
Examples:
/**
* New style.
* @param boo Something scary.
* @param? goo Something slimy (optional).
*/
{template .foo autoescape="..."}
{msg desc=""}
{$boo} has a friend named {$goo.firstName}.
{/msg}
{/template}
6. Misc:
+ Other than the items specified above, everything else is ignored.
+ SoyDoc blocks not immediately followed by a template are ignored.
+ The file must end with a newline.
Template contents are parsed as follows:
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. Soy tag format:
+ Can be delimited by single braces "{...}".
+ } characters are only allowed in tags if they're inside string literals.
+ Some Soy tags are allowed to end in "/}" to denote immediate ending of a block.
+ It is an error to use "/}" 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
2. 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}
3. 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}
4. 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}
5. Misc:
+ The following commands are not allowed to appear in a template:
{namespace ...} {template ...} {/template}
TODO(lukes): This parser has a lot of issues: Too much parsing logic is handled by the AST nodes with regular expressions. This is probably slower than handling it in the parser and it leads to redundant error handling code.
| Modifier and Type | Field and Description |
|---|---|
static int |
ALIAS_OPEN
RegularExpression Id.
|
static int |
ANY_CHAR
RegularExpression Id.
|
static int |
AS
RegularExpression Id.
|
static int |
ATTRIBUTE_VALUE
RegularExpression Id.
|
static int |
AUTOESCAPE
RegularExpression Id.
|
static int |
BLOCK_COMMENT_CHAR
RegularExpression Id.
|
static int |
BLOCK_DOC_COMMENT
RegularExpression Id.
|
static int |
BLOCK_DOC_COMMENT_START
RegularExpression Id.
|
static int |
BLOCK_NONDOC_COMMENT
RegularExpression Id.
|
static int |
BLOCK_NONDOC_COMMENT_START
RegularExpression Id.
|
static int |
BRACE
RegularExpression Id.
|
static int |
CMD_BEGIN_CALL
RegularExpression Id.
|
static int |
CMD_BEGIN_CASE
RegularExpression Id.
|
static int |
CMD_BEGIN_CSS
RegularExpression Id.
|
static int |
CMD_BEGIN_ELSEIF
RegularExpression Id.
|
static int |
CMD_BEGIN_FALLBACK_MSG
RegularExpression Id.
|
static int |
CMD_BEGIN_FOR
RegularExpression Id.
|
static int |
CMD_BEGIN_FOREACH
RegularExpression Id.
|
static int |
CMD_BEGIN_IF
RegularExpression Id.
|
static int |
CMD_BEGIN_IMPLICIT_PRINT
RegularExpression Id.
|
static int |
CMD_BEGIN_LET
RegularExpression Id.
|
static int |
CMD_BEGIN_MSG
RegularExpression Id.
|
static int |
CMD_BEGIN_PARAM
RegularExpression Id.
|
static int |
CMD_BEGIN_PLURAL
RegularExpression Id.
|
static int |
CMD_BEGIN_PRINT
RegularExpression Id.
|
static int |
CMD_BEGIN_SELECT
RegularExpression Id.
|
static int |
CMD_BEGIN_SWITCH
RegularExpression Id.
|
static int |
CMD_BEGIN_XID
RegularExpression Id.
|
static int |
CMD_CLOSE_CALL
RegularExpression Id.
|
static int |
CMD_CLOSE_DELTEMPLATE
RegularExpression Id.
|
static int |
CMD_CLOSE_FOR
RegularExpression Id.
|
static int |
CMD_CLOSE_FOREACH
RegularExpression Id.
|
static int |
CMD_CLOSE_IF
RegularExpression Id.
|
static int |
CMD_CLOSE_LET
RegularExpression Id.
|
static int |
CMD_CLOSE_LOG
RegularExpression Id.
|
static int |
CMD_CLOSE_MSG
RegularExpression Id.
|
static int |
CMD_CLOSE_PARAM
RegularExpression Id.
|
static int |
CMD_CLOSE_PLURAL
RegularExpression Id.
|
static int |
CMD_CLOSE_SELECT
RegularExpression Id.
|
static int |
CMD_CLOSE_SWITCH
RegularExpression Id.
|
static int |
CMD_CLOSE_TEMPLATE
RegularExpression Id.
|
static int |
CMD_END
RegularExpression Id.
|
static int |
CMD_FULL_CR
RegularExpression Id.
|
static int |
CMD_FULL_DEBUGGER
RegularExpression Id.
|
static int |
CMD_FULL_DEFAULT
RegularExpression Id.
|
static int |
CMD_FULL_ELSE
RegularExpression Id.
|
static int |
CMD_FULL_IFEMPTY
RegularExpression Id.
|
static int |
CMD_FULL_LB
RegularExpression Id.
|
static int |
CMD_FULL_LF
RegularExpression Id.
|
static int |
CMD_FULL_NIL
RegularExpression Id.
|
static int |
CMD_FULL_RB
RegularExpression Id.
|
static int |
CMD_FULL_SP
RegularExpression Id.
|
static int |
CMD_FULL_TAB
RegularExpression Id.
|
static int |
CMD_OPEN_LITERAL
RegularExpression Id.
|
static int |
CMD_OPEN_LOG
RegularExpression Id.
|
static int |
CMD_SELF_CLOSE
RegularExpression Id.
|
static int |
CMD_TEXT_ARBITRARY_TOKEN
RegularExpression Id.
|
static int |
CMD_TEXT_DIRECTIVE_NAME
RegularExpression Id.
|
static int |
CMD_TEXT_PHNAME_ATTR
RegularExpression Id.
|
static int |
CMD_TYPE_LITERAL
RegularExpression Id.
|
static int |
CMD_TYPE_PREFIX
RegularExpression Id.
|
static int |
CSSBASE
RegularExpression Id.
|
static int |
DECL_BEGIN_INJECT_PARAM
RegularExpression Id.
|
static int |
DECL_BEGIN_PARAM
RegularExpression Id.
|
static int |
DEFAULT
Lexical state.
|
static int |
DELPACKAGE_OPEN
RegularExpression Id.
|
static int |
DELTEMPLATE_OPEN
RegularExpression Id.
|
static int |
DOTTED_IDENT
RegularExpression Id.
|
static int |
EOF
End of File.
|
static int |
EQ
RegularExpression Id.
|
static int |
IDENT
RegularExpression Id.
|
static int |
IN_BLOCK_DOC_COMMENT
Lexical state.
|
static int |
IN_BLOCK_NONDOC_COMMENT
Lexical state.
|
static int |
IN_CMD_TAG_STRUCTURED
Lexical state.
|
static int |
IN_CMD_TAG_UNSTRUCTURED
Lexical state.
|
static int |
IN_LITERAL_BLOCK
Lexical state.
|
static int |
IN_MULTILINE_COMMENT
Lexical state.
|
static int |
IN_SOYDOC
Lexical state.
|
static int |
IN_TYPE_LITERAL
Lexical state.
|
com.google.template.soy.soyparse.Token |
jj_nt
Next token.
|
static int |
LINE_COMMENT_AT_SOL
RegularExpression Id.
|
static int |
LINE_COMMENT_NOT_SOL
RegularExpression Id.
|
static int |
LITERAL_RAW_TEXT_CONTENT
RegularExpression Id.
|
static int |
MSG_HTML_TAG_CLOSE
RegularExpression Id.
|
static int |
MSG_HTML_TAG_OPEN
RegularExpression Id.
|
static int |
NAME
RegularExpression Id.
|
static int |
NAMESPACE_OPEN
RegularExpression Id.
|
static int |
NL
RegularExpression Id.
|
static int |
NOT_NL
RegularExpression Id.
|
static int |
NOT_WS
RegularExpression Id.
|
static int |
QMARK
RegularExpression Id.
|
static int |
RBRACE
RegularExpression Id.
|
static int |
REQUIRECSS
RegularExpression Id.
|
static int |
SOYDOC
RegularExpression Id.
|
static int |
TEMPLATE_DEFAULT_AT_SOL
Lexical state.
|
static int |
TEMPLATE_DEFAULT_IN_MSG_BLOCK_AT_SOL
Lexical state.
|
static int |
TEMPLATE_DEFAULT_IN_MSG_BLOCK_NOT_SOL
Lexical state.
|
static int |
TEMPLATE_DEFAULT_NOT_SOL
Lexical state.
|
static int |
TEMPLATE_OPEN
RegularExpression Id.
|
static int |
TEMPLATE_OPEN_SUFFIX
RegularExpression Id.
|
com.google.template.soy.soyparse.Token |
token
Current token.
|
static int |
TOKEN_NL
RegularExpression Id.
|
static int |
TOKEN_NOT_WS
RegularExpression Id.
|
com.google.template.soy.soyparse.SoyFileParserTokenManager |
token_source
Generated Token Manager.
|
static int |
TOKEN_WS_NOT_NL
RegularExpression Id.
|
static String[] |
tokenImage
Literal token values.
|
static int |
UNEXPECTED_TOKEN
RegularExpression Id.
|
static int |
WS
RegularExpression Id.
|
static int |
WS_CHAR
RegularExpression Id.
|
static int |
WS_NOT_NL
RegularExpression Id.
|
static int |
XXX_BRACE_INVALID
RegularExpression Id.
|
static int |
XXX_CMD_TEXT_PHNAME_NOT_IDENT
RegularExpression Id.
|
static int |
XXX_INVALID_STRING_LITERAL
RegularExpression Id.
|
| Constructor and Description |
|---|
SoyFileParser(InputStream stream)
Constructor with InputStream.
|
SoyFileParser(InputStream stream,
String encoding)
Constructor with InputStream and supplied encoding
|
SoyFileParser(Reader stream)
Constructor.
|
SoyFileParser(com.google.template.soy.soyparse.SoyFileParserTokenManager tm)
Constructor with generated Token Manager.
|
SoyFileParser(SoyTypeRegistry typeRegistry,
IdGenerator nodeIdGen,
Reader input,
SoyFileKind soyFileKind,
String filePath,
ErrorReporter errorReporter)
Constructor that takes a reader object providing the input.
|
| Modifier and Type | Method and Description |
|---|---|
void |
disable_tracing()
Disable tracing.
|
void |
enable_tracing()
Enable tracing.
|
ParseException |
generateParseException()
Generate ParseException.
|
com.google.template.soy.soyparse.Token |
getNextToken()
Get the next Token.
|
com.google.template.soy.soyparse.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.
|
SoyFileNode |
parseSoyFile()
Attempts to parse the given input as a Soy file, returns null if parsing fails.
|
TemplateParseResult |
parseTemplateContent()
TODO(lukes): Delete after porting tests
|
void |
ReInit(InputStream stream)
Reinitialise.
|
void |
ReInit(InputStream stream,
String encoding)
Reinitialise.
|
void |
ReInit(Reader stream)
Reinitialise.
|
void |
ReInit(com.google.template.soy.soyparse.SoyFileParserTokenManager tm)
Reinitialise.
|
public com.google.template.soy.soyparse.SoyFileParserTokenManager token_source
public com.google.template.soy.soyparse.Token token
public com.google.template.soy.soyparse.Token jj_nt
public static final int EOF
public static final int RBRACE
public static final int EQ
public static final int AS
public static final int ALIAS_OPEN
public static final int CSSBASE
public static final int NAMESPACE_OPEN
public static final int AUTOESCAPE
public static final int REQUIRECSS
public static final int DELPACKAGE_OPEN
public static final int ATTRIBUTE_VALUE
public static final int DOTTED_IDENT
public static final int TEMPLATE_OPEN_SUFFIX
public static final int DELTEMPLATE_OPEN
public static final int TEMPLATE_OPEN
public static final int SOYDOC
public static final int UNEXPECTED_TOKEN
public static final int LINE_COMMENT_AT_SOL
public static final int LINE_COMMENT_NOT_SOL
public static final int BLOCK_DOC_COMMENT_START
public static final int BLOCK_NONDOC_COMMENT_START
public static final int BLOCK_COMMENT_CHAR
public static final int BLOCK_DOC_COMMENT
public static final int BLOCK_NONDOC_COMMENT
public static final int CMD_CLOSE_TEMPLATE
public static final int CMD_CLOSE_DELTEMPLATE
public static final int DECL_BEGIN_PARAM
public static final int DECL_BEGIN_INJECT_PARAM
public static final int XXX_BRACE_INVALID
public static final int CMD_FULL_SP
public static final int CMD_FULL_NIL
public static final int CMD_FULL_LF
public static final int CMD_FULL_CR
public static final int CMD_FULL_TAB
public static final int CMD_FULL_LB
public static final int CMD_FULL_RB
public static final int CMD_OPEN_LITERAL
public static final int CMD_BEGIN_CALL
public static final int CMD_CLOSE_CALL
public static final int CMD_BEGIN_PARAM
public static final int CMD_CLOSE_PARAM
public static final int CMD_BEGIN_MSG
public static final int CMD_BEGIN_FALLBACK_MSG
public static final int CMD_CLOSE_MSG
public static final int CMD_BEGIN_PRINT
public static final int CMD_BEGIN_XID
public static final int CMD_BEGIN_CSS
public static final int CMD_BEGIN_IF
public static final int CMD_BEGIN_ELSEIF
public static final int CMD_FULL_ELSE
public static final int CMD_CLOSE_IF
public static final int CMD_BEGIN_LET
public static final int CMD_CLOSE_LET
public static final int CMD_BEGIN_FOR
public static final int CMD_CLOSE_FOR
public static final int CMD_BEGIN_PLURAL
public static final int CMD_CLOSE_PLURAL
public static final int CMD_BEGIN_SELECT
public static final int CMD_CLOSE_SELECT
public static final int CMD_BEGIN_SWITCH
public static final int CMD_CLOSE_SWITCH
public static final int CMD_FULL_DEFAULT
public static final int CMD_BEGIN_CASE
public static final int CMD_BEGIN_FOREACH
public static final int CMD_FULL_IFEMPTY
public static final int CMD_CLOSE_FOREACH
public static final int CMD_OPEN_LOG
public static final int CMD_CLOSE_LOG
public static final int CMD_FULL_DEBUGGER
public static final int CMD_BEGIN_IMPLICIT_PRINT
public static final int CMD_END
public static final int CMD_SELF_CLOSE
public static final int CMD_TEXT_DIRECTIVE_NAME
public static final int CMD_TEXT_PHNAME_ATTR
public static final int XXX_CMD_TEXT_PHNAME_NOT_IDENT
public static final int NAME
public static final int CMD_TYPE_PREFIX
public static final int CMD_TYPE_LITERAL
public static final int CMD_TEXT_ARBITRARY_TOKEN
public static final int XXX_INVALID_STRING_LITERAL
public static final int MSG_HTML_TAG_OPEN
public static final int MSG_HTML_TAG_CLOSE
public static final int LITERAL_RAW_TEXT_CONTENT
public static final int TOKEN_NL
public static final int TOKEN_WS_NOT_NL
public static final int TOKEN_NOT_WS
public static final int ANY_CHAR
public static final int WS
public static final int WS_CHAR
public static final int NOT_WS
public static final int NL
public static final int NOT_NL
public static final int WS_NOT_NL
public static final int BRACE
public static final int QMARK
public static final int IDENT
public static final int DEFAULT
public static final int IN_MULTILINE_COMMENT
public static final int IN_SOYDOC
public static final int TEMPLATE_DEFAULT_AT_SOL
public static final int TEMPLATE_DEFAULT_IN_MSG_BLOCK_AT_SOL
public static final int TEMPLATE_DEFAULT_NOT_SOL
public static final int TEMPLATE_DEFAULT_IN_MSG_BLOCK_NOT_SOL
public static final int IN_BLOCK_DOC_COMMENT
public static final int IN_BLOCK_NONDOC_COMMENT
public static final int IN_CMD_TAG_UNSTRUCTURED
public static final int IN_CMD_TAG_STRUCTURED
public static final int IN_TYPE_LITERAL
public static final int IN_LITERAL_BLOCK
public static final String[] tokenImage
public SoyFileParser(SoyTypeRegistry typeRegistry, IdGenerator nodeIdGen, Reader input, SoyFileKind soyFileKind, String filePath, ErrorReporter errorReporter)
typeRegistry - The type registry for resolving type names.nodeIdGen - The node id generator for the tree being built.input - The input to parse.soyFileKind - The kind of this Soy file.filePath - The path of the source being parsed. Used for reporting.errorReporter - For reporting parse errors.public SoyFileParser(InputStream stream)
public SoyFileParser(InputStream stream, String encoding)
public SoyFileParser(Reader stream)
public SoyFileParser(com.google.template.soy.soyparse.SoyFileParserTokenManager tm)
public SoyFileNode parseSoyFile()
public final void MaybeWhitespace(String errorMessage) throws ParseException
ParseExceptionpublic final TemplateParseResult parseTemplateContent() throws ParseException
ParseExceptionpublic void ReInit(InputStream stream)
public void ReInit(InputStream stream, String encoding)
public void ReInit(Reader stream)
public void ReInit(com.google.template.soy.soyparse.SoyFileParserTokenManager tm)
public final com.google.template.soy.soyparse.Token getNextToken()
public final com.google.template.soy.soyparse.Token getToken(int index)
public ParseException generateParseException()
public final void enable_tracing()
public final void disable_tracing()