Package PyFoam :: Package ThirdParty :: Module pyratemp :: Class Parser
[hide private]
[frames] | no frames]

Class Parser

source code


Parse a template into a parse-tree.

Includes a syntax-check, an optional expression-check and verbose error-messages.

See documentation for a description of the parse-tree.

Instance Methods [hide private]
 
__init__(self, loadfunc=None, testexpr=None, escape=1)
Init the parser.
source code
 
_errpos(self, fpos)
Convert `fpos` to ``(filename,row,column)`` for error-messages.
source code
 
_parse(self, template, fpos=0)
Recursive part of `parse()`.
source code
 
_parse_sub(self, parsetree, text, fpos=0)
Parse substitutions, and append them to the parse-tree.
source code
 
_testexpr(self, expr, fpos=0)
Test a template-expression to detect errors.
source code
 
parse(self, template)
Parse a template.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]
  _block_end = ')-->'
  _block_start = '<!--('
  _comment_end = '!#'
  _comment_start = '#!'
  _e = '\\)\\-\\-\\>'
  _reBlock = re.compile(r'(?mx)^(?P<mEnd>[ \t]*)<!--\(end\)-->(?...
  _reComment = re.compile(r'(?m)#!(?P<content>.*?)(?P<end>!#|\n|...
  _reForParam = re.compile(r'^(?P<names>\w+(?:\s*,\s*\w+)*)\s+in...
  _reMacroParam = re.compile(r'^\w+$')
  _reSubstitution = re.compile(r'(?mx)(\$!\s*(?P<sub>.*?)\s*(?P<...
  _s = '\\<\\!\\-\\-\\('
  _strBlock = '\n ^(?P<mEnd>[ \\t]*)\\<\\!\\-...
  _strComment = '\\#\\!(?P<content>.*?)(?P<end>\\!\\#|\\n|$)'
  _strForParam = '^(?P<names>\\w+(?:\\s*,\\s*\\w+)*)\\s+in\\s+(?...
  _strSubstitution = '\n (\n ...
  _sub_end = '!$'
  _sub_start = '$!'
  _subesc_end = '!@'
  _subesc_start = '@!'
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, loadfunc=None, testexpr=None, escape=1)
(Constructor)

source code 

Init the parser.

:Parameters:

  • `loadfunc`: function to load included templates (i.e. ``LoaderFile(...).load``)
  • `testexpr`: function to test if a template-expressions is valid (i.e. ``EvalPseudoSandbox().compile``)
  • `escape`: default-escaping (may be modified by the template)

:Exceptions:

  • `ValueError`: if `testexpr` or `escape` is invalid.
Overrides: object.__init__

_parse(self, template, fpos=0)

source code 

Recursive part of `parse()`.

:Parameters:

  • template
  • fpos: position of ``template`` in the complete template (for error-messages)

_parse_sub(self, parsetree, text, fpos=0)

source code 

Parse substitutions, and append them to the parse-tree.

Additionally, remove comments.

parse(self, template)

source code 

Parse a template.

:Parameters:

  • `template`: template-unicode-string

:Returns: the resulting parse-tree :Exceptions:

  • `TemplateSyntaxError`: for template-syntax-errors
  • `TemplateIncludeError`: if template-inclusion failed
  • `TemplateException`

Class Variable Details [hide private]

_reBlock

Value:
re.compile(r'(?mx)^(?P<mEnd>[ \t]*)<!--\(end\)-->(?P<meIgnored>.*)\r?\\
n?|(?P<sEnd>)<!--\(end\)-->|(?P<sSpace>[ \t]*)<!--\((?P<sKeyw>\w+)[ \t\
]*(?P<sParam>.*?)\)-->(?P<sContent>.*?)(?=(?:<!--\(.*?\)-->.*?)??<!--\\
(end\)-->)|^(?P<indent>[ \t]*)<!--\((?P<mKeyw>\w+)\s*(?P<mParam>.*?)\)\
-->(?P<mIgnored>.*)\r?\n(?P<mContent>(?:.*\n)*?)(?=\8<!--\((?:.|\s)*?\\
)-->)')

_reComment

Value:
re.compile(r'(?m)#!(?P<content>.*?)(?P<end>!#|\n|$)')

_reForParam

Value:
re.compile(r'^(?P<names>\w+(?:\s*,\s*\w+)*)\s+in\s+(?P<iter>.+)$')

_reSubstitution

Value:
re.compile(r'(?mx)(\$!\s*(?P<sub>.*?)\s*(?P<end>!\$|$)|@!\s*(?P<escsub\
>.*?)\s*(?P<escend>!@|$))')

_strBlock

Value:
'''
                    ^(?P<mEnd>[ \\t]*)\\<\\!\\-\\-\\(end\\)\\-\\-\\>(?\
P<meIgnored>.*)\\r?\\n?   # multi-line end  (^   <!--(end)-->IGNORED_T\
EXT\\n)
                    |
                    (?P<sEnd>)\\<\\!\\-\\-\\(end\\)\\-\\-\\>          \
                     # single-line end (<!--(end)-->)
                    |
...

_strForParam

Value:
'^(?P<names>\\w+(?:\\s*,\\s*\\w+)*)\\s+in\\s+(?P<iter>.+)$'

_strSubstitution

Value:
'''
                    (
                    \\$\\!\\s*(?P<sub>.*?)\\s*(?P<end>\\!\\$|$)       \
#substitution
                    |
                    \\@\\!\\s*(?P<escsub>.*?)\\s*(?P<escend>\\!\\@|$) \
#escaped substitution
                    )
...