Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 1 | # Copyright (c) 2006-2011 Nathan Binkert <nate@binkert.org> |
Nathan Binkert | 2ecaa99 | 2009-08-16 13:40:01 -0700 | [diff] [blame] | 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 |
| 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. |
| 14 | # |
| 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. |
| 26 | |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 27 | import os |
Nathan Binkert | 2ecaa99 | 2009-08-16 13:40:01 -0700 | [diff] [blame] | 28 | |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 29 | import ply.lex |
| 30 | import ply.yacc |
Nathan Binkert | 2ecaa99 | 2009-08-16 13:40:01 -0700 | [diff] [blame] | 31 | |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 32 | |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 33 | class ParseError(Exception): |
Nathan Binkert | 2ecaa99 | 2009-08-16 13:40:01 -0700 | [diff] [blame] | 34 | def __init__(self, message, token=None): |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 35 | Exception.__init__(self, message) |
Nathan Binkert | 2ecaa99 | 2009-08-16 13:40:01 -0700 | [diff] [blame] | 36 | self.token = token |
| 37 | |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 38 | |
Nathan Binkert | 2ecaa99 | 2009-08-16 13:40:01 -0700 | [diff] [blame] | 39 | class Grammar(object): |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 40 | def setupLexerFactory(self, **kwargs): |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 41 | if "module" in kwargs: |
Andreas Sandberg | fa21127 | 2019-01-25 11:32:25 +0000 | [diff] [blame] | 42 | raise AttributeError("module is an illegal attribute") |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 43 | self.lex_kwargs = kwargs |
Nathan Binkert | 2ecaa99 | 2009-08-16 13:40:01 -0700 | [diff] [blame] | 44 | |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 45 | def setupParserFactory(self, **kwargs): |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 46 | if "module" in kwargs: |
Andreas Sandberg | fa21127 | 2019-01-25 11:32:25 +0000 | [diff] [blame] | 47 | raise AttributeError("module is an illegal attribute") |
Nathan Binkert | 2ecaa99 | 2009-08-16 13:40:01 -0700 | [diff] [blame] | 48 | |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 49 | if "output" in kwargs: |
| 50 | dir, tab = os.path.split(output) |
| 51 | if not tab.endswith(".py"): |
| 52 | raise AttributeError("The output file must end with .py") |
| 53 | kwargs["outputdir"] = dir |
| 54 | kwargs["tabmodule"] = tab[:-3] |
Nathan Binkert | 2ecaa99 | 2009-08-16 13:40:01 -0700 | [diff] [blame] | 55 | |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 56 | self.yacc_kwargs = kwargs |
| 57 | |
| 58 | def __getattr__(self, attr): |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 59 | if attr == "lexers": |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 60 | self.lexers = [] |
| 61 | return self.lexers |
| 62 | |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 63 | if attr == "lex_kwargs": |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 64 | self.setupLexerFactory() |
| 65 | return self.lex_kwargs |
| 66 | |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 67 | if attr == "yacc_kwargs": |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 68 | self.setupParserFactory() |
| 69 | return self.yacc_kwargs |
| 70 | |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 71 | if attr == "lex": |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 72 | self.lex = ply.lex.lex(module=self, **self.lex_kwargs) |
| 73 | return self.lex |
| 74 | |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 75 | if attr == "yacc": |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 76 | self.yacc = ply.yacc.yacc(module=self, **self.yacc_kwargs) |
| 77 | return self.yacc |
| 78 | |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 79 | if attr == "current_lexer": |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 80 | if not self.lexers: |
| 81 | return None |
| 82 | return self.lexers[-1][0] |
| 83 | |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 84 | if attr == "current_source": |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 85 | if not self.lexers: |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 86 | return "<none>" |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 87 | return self.lexers[-1][1] |
| 88 | |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 89 | if attr == "current_line": |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 90 | if not self.lexers: |
| 91 | return -1 |
| 92 | return self.current_lexer.lineno |
| 93 | |
Andreas Sandberg | fa21127 | 2019-01-25 11:32:25 +0000 | [diff] [blame] | 94 | raise AttributeError( |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 95 | "'%s' object has no attribute '%s'" % (type(self), attr) |
| 96 | ) |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 97 | |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 98 | def parse_string(self, data, source="<string>", debug=None, tracking=0): |
Andreas Sandberg | 4b9c46c | 2021-01-21 17:09:38 +0000 | [diff] [blame] | 99 | if not isinstance(data, str): |
Andreas Sandberg | fa21127 | 2019-01-25 11:32:25 +0000 | [diff] [blame] | 100 | raise AttributeError( |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 101 | "argument must be a string, was '%s'" % type(f) |
| 102 | ) |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 103 | |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 104 | lexer = self.lex.clone() |
| 105 | lexer.input(data) |
| 106 | self.lexers.append((lexer, source)) |
Giacomo Travaglini | 735267e | 2020-02-28 13:42:03 +0000 | [diff] [blame] | 107 | |
| 108 | lrtab = ply.yacc.LRTable() |
| 109 | lrtab.lr_productions = self.yacc.productions |
| 110 | lrtab.lr_action = self.yacc.action |
| 111 | lrtab.lr_goto = self.yacc.goto |
| 112 | |
| 113 | parser = ply.yacc.LRParser(lrtab, self.yacc.errorfunc) |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 114 | result = parser.parse(lexer=lexer, debug=debug, tracking=tracking) |
| 115 | self.lexers.pop() |
| 116 | return result |
| 117 | |
| 118 | def parse_file(self, f, **kwargs): |
Andreas Sandberg | 4b9c46c | 2021-01-21 17:09:38 +0000 | [diff] [blame] | 119 | if isinstance(f, str): |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 120 | source = f |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 121 | f = open(f, "r") |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 122 | elif isinstance(f, file): |
| 123 | source = f.name |
| 124 | else: |
Andreas Sandberg | fa21127 | 2019-01-25 11:32:25 +0000 | [diff] [blame] | 125 | raise AttributeError( |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 126 | "argument must be either a string or file, was '%s'" % type(f) |
| 127 | ) |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 128 | |
| 129 | return self.parse_string(f.read(), source, **kwargs) |
Nathan Binkert | 2ecaa99 | 2009-08-16 13:40:01 -0700 | [diff] [blame] | 130 | |
| 131 | def p_error(self, t): |
| 132 | if t: |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 133 | msg = "Syntax error at %s:%d:%d\n>>%s<<" % ( |
| 134 | self.current_source, |
| 135 | t.lineno, |
| 136 | t.lexpos + 1, |
| 137 | t.value, |
| 138 | ) |
Nathan Binkert | 2ecaa99 | 2009-08-16 13:40:01 -0700 | [diff] [blame] | 139 | else: |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 140 | msg = "Syntax error at end of %s" % (self.current_source,) |
Nathan Binkert | 2ecaa99 | 2009-08-16 13:40:01 -0700 | [diff] [blame] | 141 | raise ParseError(msg, t) |
| 142 | |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 143 | def t_error(self, t): |
Bobby R. Bruce | 2bc5a8b | 2022-08-22 12:34:19 -0700 | [diff] [blame] | 144 | msg = "Illegal character %s @ %d:%d" % ( |
| 145 | repr(t.value[0]), |
| 146 | t.lineno, |
| 147 | t.lexpos, |
| 148 | ) |
Nathan Binkert | 3d252f8 | 2011-07-05 18:30:04 -0700 | [diff] [blame] | 149 | raise ParseError(msg, t) |