Dashboard > StringTemplate > ... > StringTemplate Documentation > StringTemplate Grammars
  StringTemplate Log In | Sign Up   View a printable version of the current page.  
  StringTemplate Grammars
Added by Terence Parr, last edited by Kunle Odutola on Oct 23, 2006  (view change)
Labels: 
(None)

StringTemplate Grammars

StringTemplate has multiple grammars that describe templates at varying degrees of detail. At the grossest level of granularity, the group.g grammar accepts a list of templates with formal template arguments. Each of these templates is broken up into chunks of literal text and attribute expressions via template.g. The default lexer uses $...$ delimiters, but the angle.bracket.template.g lexer provides <...> delimiters. Each of the attribute expression chunks is processed by action.g. It builds trees (ASTs) representing the operation indicated in the expression. These ASTs
represent the "precompiled" templates, which are evaluated by the tree grammar eval.g each time a StringTemplate is rendered to string with ToString().

The grammar files are:

  • group.g: read a group file full of templates
  • template.g: break an individual template into chunks
  • angle.bracket.template.g: <...> template lexer
  • action.g: parse attribute expressions into ASTs
  • eval.g: evaluate expression ASTs during ToString()

Anything outside of the StringTemplate start/stop delimiters is ignored.

A word about Strings. Strings are double-quoted with optional embedded escaped characters that are translated (escapes are not translated outside of strings; for example, text outside of attribute expressions do not get escape chars translated except \$, \< and \>).

<<
STRING
    :   '"' (ESC_CHAR | ~'"')* '"'
    ;
>>

The translated escapes are:

<<
ESC_CHAR
    :   '\\'
        (   'n'
        |   'r'
        |   't'
        |   'b'
        |   'f'
        |   '"'
        |   '\\'
        )
     ;
>>

but other escapes are allowed and ignored.

Please see the actual grammar files for the formal language specification of StringTemplate's various components.

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.1 Build:#806 May 06, 2007) - Bug/feature request - Contact Administrators