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

Class Parser

source code

object --+
         |
        Parser

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=HTML)
Init the parser.
source code
 
parse(self, template)
Parse a template.
source code
 
_errpos(self, fpos)
Convert `fpos` to ``(filename,row,column)`` for error-messages.
source code
 
_testexpr(self, expr, fpos=0)
Test a template-expression to detect errors.
source code
 
_parse_sub(self, parsetree, text, fpos=0)
Parse substitutions, and append them to the parse-tree.
source code
 
_parse(self, template, fpos=0)
Recursive part of `parse()`.
source code

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

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

Inherited from object: __class__

Method Details [hide private]

__init__(self, loadfunc=None, testexpr=None, escape=HTML)
(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)

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`

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

source code 

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

Additionally, remove comments.

_parse(self, template, fpos=0)

source code 

Recursive part of `parse()`.

:Parameters:

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

Class Variable Details [hide private]

_strComment

Value:
r"""%s(?P<content>.*?)(?P<end>%s|\n|$)""" %(re.escape(_comment_start),\
 re.escape(_comment_end))

_strSubstitution

Value:
r"""
                    (
                    %s\s*(?P<sub>.*?)\s*(?P<end>%s|$)       #substitut\
ion
                    |
                    %s\s*(?P<escsub>.*?)\s*(?P<escend>%s|$) #escaped s\
ubstitution
                    )
...

_strBlock

Value:
r"""
                    ^(?P<mEnd>[ \t]*)%send%s(?P<meIgnored>.*)\r?\n?   \
# multi-line end  (^   <!--(end)-->IGNORED_TEXT\n)
                    |
                    (?P<sEnd>)%send%s                               # \
single-line end (<!--(end)-->)
                    |
                    (?P<sSpace>[ \t]*)                              # \
...

_strForParam

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