/
Template.py
74 lines (70 loc) · 2.83 KB
/
Template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Copyright (C) 2007-2010 Samuel Abels.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import re
from Exscript.interpreter.Scope import Scope
from Exscript.interpreter.Code import Code
from Exscript.interpreter.Execute import Execute
grammar = (
('escaped_data', r'\\.'),
('open_curly_bracket', '{'),
('close_curly_bracket', '}'),
('newline', r'[\r\n]'),
('raw_data', r'[^\r\n{}\\]+')
)
grammar_c = []
for thetype, regex in grammar:
grammar_c.append((thetype, re.compile(regex)))
class Template(Scope):
def __init__(self, lexer, parser, parent, *args, **kwargs):
Scope.__init__(self, 'Template', lexer, parser, parent, **kwargs)
lexer.set_grammar(grammar_c)
#print "Opening Scope:", lexer.token()
buffer = ''
while 1:
if self.exit_requested or lexer.current_is('EOF'):
break
elif lexer.next_if('open_curly_bracket'):
if buffer.strip() != '':
self.add(Execute(lexer, parser, self, buffer))
buffer = ''
if isinstance(parent, Code):
break
self.add(Code(lexer, parser, self))
elif lexer.current_is('raw_data'):
if lexer.token()[1].lstrip().startswith('#'):
while not lexer.current_is('newline'):
next(lexer)
continue
buffer += lexer.token()[1]
next(lexer)
elif lexer.current_is('escaped_data'):
token = lexer.token()[1]
if token[1] == '$':
# An escaped $ is handeled by the Execute() token, so
# we do not strip the \ here.
buffer += token
else:
buffer += token[1]
next(lexer)
elif lexer.next_if('newline'):
if buffer.strip() != '':
self.add(Execute(lexer, parser, self, buffer))
buffer = ''
else:
ttype = lexer.token()[0]
lexer.syntax_error('Unexpected %s' % ttype, self)
lexer.restore_grammar()
def execute(self):
return self.value(self)