grammar.py (8452:3f2c329e9046) grammar.py (13663:9b64aeabf9a5)
1# Copyright (c) 2006-2011 Nathan Binkert <nate@binkert.org>
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright

--- 23 unchanged lines hidden (view full) ---

32class ParseError(Exception):
33 def __init__(self, message, token=None):
34 Exception.__init__(self, message)
35 self.token = token
36
37class Grammar(object):
38 def setupLexerFactory(self, **kwargs):
39 if 'module' in kwargs:
1# Copyright (c) 2006-2011 Nathan Binkert <nate@binkert.org>
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright

--- 23 unchanged lines hidden (view full) ---

32class ParseError(Exception):
33 def __init__(self, message, token=None):
34 Exception.__init__(self, message)
35 self.token = token
36
37class Grammar(object):
38 def setupLexerFactory(self, **kwargs):
39 if 'module' in kwargs:
40 raise AttributeError, "module is an illegal attribute"
40 raise AttributeError("module is an illegal attribute")
41 self.lex_kwargs = kwargs
42
43 def setupParserFactory(self, **kwargs):
44 if 'module' in kwargs:
41 self.lex_kwargs = kwargs
42
43 def setupParserFactory(self, **kwargs):
44 if 'module' in kwargs:
45 raise AttributeError, "module is an illegal attribute"
45 raise AttributeError("module is an illegal attribute")
46
47 if 'output' in kwargs:
48 dir,tab = os.path.split(output)
49 if not tab.endswith('.py'):
46
47 if 'output' in kwargs:
48 dir,tab = os.path.split(output)
49 if not tab.endswith('.py'):
50 raise AttributeError, \
51 'The output file must end with .py'
50 raise AttributeError('The output file must end with .py')
52 kwargs['outputdir'] = dir
53 kwargs['tabmodule'] = tab[:-3]
54
55 self.yacc_kwargs = kwargs
56
57 def __getattr__(self, attr):
58 if attr == 'lexers':
59 self.lexers = []

--- 25 unchanged lines hidden (view full) ---

85 return '<none>'
86 return self.lexers[-1][1]
87
88 if attr == 'current_line':
89 if not self.lexers:
90 return -1
91 return self.current_lexer.lineno
92
51 kwargs['outputdir'] = dir
52 kwargs['tabmodule'] = tab[:-3]
53
54 self.yacc_kwargs = kwargs
55
56 def __getattr__(self, attr):
57 if attr == 'lexers':
58 self.lexers = []

--- 25 unchanged lines hidden (view full) ---

84 return '<none>'
85 return self.lexers[-1][1]
86
87 if attr == 'current_line':
88 if not self.lexers:
89 return -1
90 return self.current_lexer.lineno
91
93 raise AttributeError, \
94 "'%s' object has no attribute '%s'" % (type(self), attr)
92 raise AttributeError(
93 "'%s' object has no attribute '%s'" % (type(self), attr))
95
96 def parse_string(self, data, source='<string>', debug=None, tracking=0):
97 if not isinstance(data, basestring):
94
95 def parse_string(self, data, source='<string>', debug=None, tracking=0):
96 if not isinstance(data, basestring):
98 raise AttributeError, \
99 "argument must be a string, was '%s'" % type(f)
97 raise AttributeError(
98 "argument must be a string, was '%s'" % type(f))
100
101 import new
102 lexer = self.lex.clone()
103 lexer.input(data)
104 self.lexers.append((lexer, source))
105 dict = {
106 'productions' : self.yacc.productions,
107 'action' : self.yacc.action,

--- 7 unchanged lines hidden (view full) ---

115
116 def parse_file(self, f, **kwargs):
117 if isinstance(f, basestring):
118 source = f
119 f = file(f, 'r')
120 elif isinstance(f, file):
121 source = f.name
122 else:
99
100 import new
101 lexer = self.lex.clone()
102 lexer.input(data)
103 self.lexers.append((lexer, source))
104 dict = {
105 'productions' : self.yacc.productions,
106 'action' : self.yacc.action,

--- 7 unchanged lines hidden (view full) ---

114
115 def parse_file(self, f, **kwargs):
116 if isinstance(f, basestring):
117 source = f
118 f = file(f, 'r')
119 elif isinstance(f, file):
120 source = f.name
121 else:
123 raise AttributeError, \
124 "argument must be either a string or file, was '%s'" % type(f)
122 raise AttributeError(
123 "argument must be either a string or file, was '%s'" % type(f))
125
126 return self.parse_string(f.read(), source, **kwargs)
127
128 def p_error(self, t):
129 if t:
130 msg = "Syntax error at %s:%d:%d\n>>%s<<" % \
131 (self.current_source, t.lineno, t.lexpos + 1, t.value)
132 else:
133 msg = "Syntax error at end of %s" % (self.current_source, )
134 raise ParseError(msg, t)
135
136 def t_error(self, t):
137 msg = "Illegal character %s @ %d:%d" % \
138 (`t.value[0]`, t.lineno, t.lexpos)
139 raise ParseError(msg, t)
124
125 return self.parse_string(f.read(), source, **kwargs)
126
127 def p_error(self, t):
128 if t:
129 msg = "Syntax error at %s:%d:%d\n>>%s<<" % \
130 (self.current_source, t.lineno, t.lexpos + 1, t.value)
131 else:
132 msg = "Syntax error at end of %s" % (self.current_source, )
133 raise ParseError(msg, t)
134
135 def t_error(self, t):
136 msg = "Illegal character %s @ %d:%d" % \
137 (`t.value[0]`, t.lineno, t.lexpos)
138 raise ParseError(msg, t)