grammar.py (13682:907a4f6c8435) grammar.py (13720:18f5d3990ac9)
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

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

20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27import os
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

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

20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27import os
28from six import string_types
28
29import ply.lex
30import ply.yacc
31
32class ParseError(Exception):
33 def __init__(self, message, token=None):
34 Exception.__init__(self, message)
35 self.token = token

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

88 if not self.lexers:
89 return -1
90 return self.current_lexer.lineno
91
92 raise AttributeError(
93 "'%s' object has no attribute '%s'" % (type(self), attr))
94
95 def parse_string(self, data, source='<string>', debug=None, tracking=0):
29
30import ply.lex
31import ply.yacc
32
33class ParseError(Exception):
34 def __init__(self, message, token=None):
35 Exception.__init__(self, message)
36 self.token = token

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

89 if not self.lexers:
90 return -1
91 return self.current_lexer.lineno
92
93 raise AttributeError(
94 "'%s' object has no attribute '%s'" % (type(self), attr))
95
96 def parse_string(self, data, source='<string>', debug=None, tracking=0):
96 if not isinstance(data, basestring):
97 if not isinstance(data, string_types):
97 raise AttributeError(
98 "argument must be a string, was '%s'" % type(f))
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,
107 'goto' : self.yacc.goto,
108 'errorfunc' : self.yacc.errorfunc,
109 }
110 parser = new.instance(ply.yacc.LRParser, dict)
111 result = parser.parse(lexer=lexer, debug=debug, tracking=tracking)
112 self.lexers.pop()
113 return result
114
115 def parse_file(self, f, **kwargs):
98 raise AttributeError(
99 "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,
108 'goto' : self.yacc.goto,
109 'errorfunc' : self.yacc.errorfunc,
110 }
111 parser = new.instance(ply.yacc.LRParser, dict)
112 result = parser.parse(lexer=lexer, debug=debug, tracking=tracking)
113 self.lexers.pop()
114 return result
115
116 def parse_file(self, f, **kwargs):
116 if isinstance(f, basestring):
117 if isinstance(f, string_types):
117 source = f
118 f = open(f, 'r')
119 elif isinstance(f, file):
120 source = f.name
121 else:
122 raise AttributeError(
123 "argument must be either a string or file, was '%s'" % type(f))
124

--- 14 unchanged lines hidden ---
118 source = f
119 f = open(f, 'r')
120 elif isinstance(f, file):
121 source = f.name
122 else:
123 raise AttributeError(
124 "argument must be either a string or file, was '%s'" % type(f))
125

--- 14 unchanged lines hidden ---