Package web :: Package web :: Module template :: Class Parser
[hide private]
[frames] | no frames]

Class Parser

source code

Parser Base.

Instance Methods [hide private]
 
__init__(self) source code
 
parse(self, text, name='<template>') source code
 
read_defwith(self, text) source code
 
read_section(self, text)
Reads one section from the given text.
source code
 
read_var(self, text)
Reads a var statement.
source code
 
read_suite(self, text)
Reads section by section till end of text.
source code
 
readline(self, text)
Reads one line from the text.
source code
 
read_node(self, text)
Reads a node from the given text and returns the node and remaining text.
source code
 
read_text(self, text)
Reads a text node from the given text.
source code
 
read_keyword(self, text) source code
 
read_expr(self, text, escape=True)
Reads a python expression from the text and returns the expression and remaining text.
source code
 
read_assignment(self, text)
Reads assignment statement from text.
source code
 
python_lookahead(self, text)
Returns the first python token from the given text.
source code
 
python_tokens(self, text) source code
 
read_indented_block(self, text, indent)
Read a block of text.
source code
 
read_statement(self, text)
Reads a python statement.
source code
 
read_block_section(self, text, begin_indent='') source code
 
create_block_node(self, keyword, stmt, block, begin_indent) source code
Method Details [hide private]

read_section(self, text)

source code 

Reads one section from the given text.

section -> block | assignment | line

>>> read_section = Parser().read_section
>>> read_section('foo\nbar\n')
(<line: [t'foo\n']>, 'bar\n')
>>> read_section('$ a = b + 1\nfoo\n')
(<assignment: 'a = b + 1'>, 'foo\n')

read_section('$for in range(10):\n hello $i\nfoo)

read_var(self, text)

source code 

Reads a var statement.

>>> read_var = Parser().read_var
>>> read_var('var x=10\nfoo')
(<var: x = 10>, 'foo')
>>> read_var('var x: hello $name\nfoo')
(<var: x = join_(u'hello ', escape_(name, True))>, 'foo')

read_suite(self, text)

source code 

Reads section by section till end of text.

>>> read_suite = Parser().read_suite
>>> read_suite('hello $name\nfoo\n')
[<line: [t'hello ', $name, t'\n']>, <line: [t'foo\n']>]

readline(self, text)

source code 

Reads one line from the text. Newline is supressed if the line ends with \.

>>> readline = Parser().readline
>>> readline('hello $name!\nbye!')
(<line: [t'hello ', $name, t'!\n']>, 'bye!')
>>> readline('hello $name!\\\nbye!')
(<line: [t'hello ', $name, t'!']>, 'bye!')
>>> readline('$f()\n\n')
(<line: [$f(), t'\n']>, '\n')

read_node(self, text)

source code 

Reads a node from the given text and returns the node and remaining text.

>>> read_node = Parser().read_node
>>> read_node('hello $name')
(t'hello ', '$name')
>>> read_node('$name')
($name, '')

read_text(self, text)

source code 

Reads a text node from the given text.

>>> read_text = Parser().read_text
>>> read_text('hello $name')
(t'hello ', '$name')

read_expr(self, text, escape=True)

source code 

Reads a python expression from the text and returns the expression and remaining text.

expr -> simple_expr | paren_expr simple_expr -> id extended_expr extended_expr -> attr_access | paren_expr extended_expr | '' attr_access -> dot id extended_expr paren_expr -> [ tokens ] | ( tokens ) | { tokens }

>>> read_expr = Parser().read_expr
>>> read_expr("name")
($name, '')
>>> read_expr("a.b and c")
($a.b, ' and c')
>>> read_expr("a. b")
($a, '. b')
>>> read_expr("name</h1>")
($name, '</h1>')
>>> read_expr("(limit)ing")
($(limit), 'ing')
>>> read_expr('a[1, 2][:3].f(1+2, "weird string[).", 3 + 4) done.')
($a[1, 2][:3].f(1+2, "weird string[).", 3 + 4), ' done.')

read_assignment(self, text)

source code 

Reads assignment statement from text.

>>> read_assignment = Parser().read_assignment
>>> read_assignment('a = b + 1\nfoo')
(<assignment: 'a = b + 1'>, 'foo')

python_lookahead(self, text)

source code 

Returns the first python token from the given text.

>>> python_lookahead = Parser().python_lookahead
>>> python_lookahead('for i in range(10):')
'for'
>>> python_lookahead('else:')
'else'
>>> python_lookahead(' x = 1')
' '

read_indented_block(self, text, indent)

source code 

Read a block of text. A block is what typically follows a for or it statement. It can be in the same line as that of the statement or an indented block.

>>> read_indented_block = Parser().read_indented_block
>>> read_indented_block('  a\n  b\nc', '  ')
('a\nb\n', 'c')
>>> read_indented_block('  a\n    b\n  c\nd', '  ')
('a\n  b\nc\n', 'd')
>>> read_indented_block('  a\n\n    b\nc', '  ')
('a\n\n  b\n', 'c')

read_statement(self, text)

source code 

Reads a python statement.

>>> read_statement = Parser().read_statement
>>> read_statement('for i in range(10): hello $name')
('for i in range(10):', ' hello $name')