1 # Copyright (c) 2006-2011 Nathan Binkert <nate@binkert.org>
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
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution;
11 # neither the name of the copyright holders nor the names of its
12 # contributors may be used to endorse or promote products derived from
13 # this software without specific prior written permission.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
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.
32 class ParseError(Exception):
33 def __init__(self
, message
, token
=None):
34 Exception.__init
__(self
, message
)
37 class Grammar(object):
38 def setupLexerFactory(self
, **kwargs
):
39 if 'module' in kwargs
:
40 raise AttributeError, "module is an illegal attribute"
41 self
.lex_kwargs
= kwargs
43 def setupParserFactory(self
, **kwargs
):
44 if 'module' in kwargs
:
45 raise AttributeError, "module is an illegal attribute"
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'
52 kwargs
['outputdir'] = dir
53 kwargs
['tabmodule'] = tab
[:-3]
55 self
.yacc_kwargs
= kwargs
57 def __getattr__(self
, attr
):
62 if attr
== 'lex_kwargs':
63 self
.setupLexerFactory()
64 return self
.lex_kwargs
66 if attr
== 'yacc_kwargs':
67 self
.setupParserFactory()
68 return self
.yacc_kwargs
71 self
.lex
= ply
.lex
.lex(module
=self
, **self
.lex_kwargs
)
75 self
.yacc
= ply
.yacc
.yacc(module
=self
, **self
.yacc_kwargs
)
78 if attr
== 'current_lexer':
81 return self
.lexers
[-1][0]
83 if attr
== 'current_source':
86 return self
.lexers
[-1][1]
88 if attr
== 'current_line':
91 return self
.current_lexer
.lineno
93 raise AttributeError, \
94 "'%s' object has no attribute '%s'" % (type(self
), attr
)
96 def parse_string(self
, data
, source
='<string>', debug
=None, tracking
=0):
97 if not isinstance(data
, basestring
):
98 raise AttributeError, \
99 "argument must be a string, was '%s'" % type(f
)
102 lexer
= self
.lex
.clone()
104 self
.lexers
.append((lexer
, source
))
106 'productions' : self
.yacc
.productions
,
107 'action' : self
.yacc
.action
,
108 'goto' : self
.yacc
.goto
,
109 'errorfunc' : self
.yacc
.errorfunc
,
111 parser
= new
.instance(ply
.yacc
.LRParser
, dict)
112 result
= parser
.parse(lexer
=lexer
, debug
=debug
, tracking
=tracking
)
116 def parse_file(self
, f
, **kwargs
):
117 if isinstance(f
, basestring
):
120 elif isinstance(f
, file):
123 raise AttributeError, \
124 "argument must be either a string or file, was '%s'" % type(f
)
126 return self
.parse_string(f
.read(), source
, **kwargs
)
128 def p_error(self
, t
):
130 msg
= "Syntax error at %s:%d:%d\n>>%s<<" % \
131 (self
.current_source
, t
.lineno
, t
.lexpos
+ 1, t
.value
)
133 msg
= "Syntax error at end of %s" % (self
.current_source
, )
134 raise ParseError(msg
, t
)
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
)