/
Parser.py
59 lines (52 loc) · 2.1 KB
/
Parser.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
# 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 copy
from Exscript.parselib import Lexer
from Exscript.interpreter.Program import Program
class Parser(object):
def __init__(self, **kwargs):
self.no_prompt = kwargs.get('no_prompt', False)
self.strip_command = kwargs.get('strip_command', True)
self.secure_only = kwargs.get('secure', False)
self.debug = kwargs.get('debug', 0)
self.variables = {}
def define(self, **kwargs):
for key, value in kwargs.iteritems():
if hasattr(value, '__iter__') or hasattr(value, '__call__'):
self.variables[key] = value
else:
self.variables[key] = [value]
def define_object(self, **kwargs):
self.variables.update(kwargs)
def _create_lexer(self):
variables = copy.deepcopy(self.variables)
return Lexer(Program, self, variables, debug = self.debug)
def parse(self, string, filename = None):
lexer = self._create_lexer()
return lexer.parse(string, filename)
def parse_file(self, filename):
lexer = self._create_lexer()
return lexer.parse_file(filename)
if __name__ == "__main__":
import sys
if len(sys.argv) == 1:
filename = 'test.exscript'
elif len(sys.argv) == 2:
filename = sys.argv[1]
else:
sys.exit(1)
parser = Parser(debug = 5)
compiled = parser.parse_file(filename)
compiled.dump()