/
Code.py
125 lines (121 loc) · 4.84 KB
/
Code.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from __future__ import absolute_import
# 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
import Exscript.interpreter.Template
from Exscript.interpreter.Scope import Scope
from Exscript.interpreter.Append import Append
from Exscript.interpreter.Assign import Assign
from Exscript.interpreter.Enter import Enter
from Exscript.interpreter.Extract import Extract
from Exscript.interpreter.Fail import Fail
from Exscript.interpreter.FunctionCall import FunctionCall
from Exscript.interpreter.IfCondition import IfCondition
from Exscript.interpreter.Loop import Loop
from Exscript.interpreter.Try import Try
from Exscript.interpreter.String import varname_re
varname = varname_re.pattern
keywords = ['append',
'as',
'else',
'end',
'enter',
'extract',
'fail',
'false',
'from',
'if',
'into',
'loop',
'try',
'to',
'true',
'until',
'when',
'while']
operators = ['in',
r'not\s+in',
r'is\s+not',
'is',
'ge',
'gt',
'le',
'lt',
'matches']
grammar = (
('escaped_data', r'\\.'),
('regex_delimiter', r'/'),
('string_delimiter', r'"'),
('open_curly_bracket', r'{'),
('close_curly_bracket', r'}'),
('close_bracket', r'\)'),
('comma', r','),
('whitespace', r'[ \t]+'),
('keyword', r'\b(?:' + '|'.join(keywords) + r')\b'),
('assign', r'='),
('octal_number', r'0\d+'),
('hex_number', r'0x(?:\w\w)+'),
('comparison', r'\b(?:' + '|'.join(operators) + r')\b'),
('arithmetic_operator', r'(?:\*|\+|-|%|\.)'),
('logical_operator', r'\b(?:and|or|not)\b'),
('open_function_call', varname + r'(?:\.' + varname + r')*\('),
('varname', varname),
('number', r'\d+'),
('newline', r'[\r\n]'),
('raw_data', r'[^\r\n{}]+')
)
grammar_c = []
for thetype, regex in grammar:
grammar_c.append((thetype, re.compile(regex)))
class Code(Scope):
def __init__(self, lexer, parser, parent):
Scope.__init__(self, 'Code', lexer, parser, parent)
lexer.set_grammar(grammar_c)
while True:
lexer.skip(['whitespace', 'newline'])
if lexer.next_if('close_curly_bracket'):
if isinstance(parent, Exscript.interpreter.Template.Template):
break
self.add(Exscript.interpreter.Template.Template(lexer, parser, self))
elif lexer.current_is('keyword', 'append'):
self.add(Append(lexer, parser, self))
elif lexer.current_is('keyword', 'extract'):
self.add(Extract(lexer, parser, self))
elif lexer.current_is('keyword', 'fail'):
self.add(Fail(lexer, parser, self))
elif lexer.current_is('keyword', 'if'):
self.add(IfCondition(lexer, parser, self))
elif lexer.current_is('keyword', 'loop'):
self.add(Loop(lexer, parser, self))
elif lexer.current_is('varname'):
self.add(Assign(lexer, parser, self))
elif lexer.current_is('keyword', 'try'):
self.add(Try(lexer, parser, self))
elif lexer.current_is('keyword', 'enter'):
self.add(Enter(lexer, parser, self))
elif lexer.current_is('keyword', 'else'):
if not isinstance(parent, Code):
lexer.syntax_error('"end" without a scope start', self)
break
elif lexer.next_if('keyword', 'end'):
if not isinstance(parent, Code):
lexer.syntax_error('"end" without a scope start', self)
lexer.skip(['whitespace', 'newline'])
break
elif lexer.current_is('open_function_call'):
self.add(FunctionCall(lexer, parser, self))
else:
lexer.syntax_error('Unexpected %s "%s"' % lexer.token(), self)
lexer.restore_grammar()