3 # Copyright 2007 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
26 """A wrapper around the generated Groc parser and lexer."""
37 class GrocException(Exception):
38 """An error occurred while parsing the groc input string."""
41 class GrocLexerWithErrors(GrocLexer
.GrocLexer
):
42 """An overridden Lexer that raises exceptions."""
44 def emitErrorMessage(self
, msg
):
45 """Raise an exception if the input fails to parse correctly.
47 Overriding the default, which normally just prints a message to
51 msg: the error message
53 GrocException: always.
55 raise GrocException(msg
)
58 class GrocParserWithErrors(GrocParser
.GrocParser
):
59 """An overridden Parser that raises exceptions."""
61 def emitErrorMessage(self
, msg
):
62 """Raise an exception if the input fails to parse correctly.
64 Overriding the default, which normally just prints a message to
68 msg: the error message
70 GrocException: always.
72 raise GrocException(msg
)
75 def CreateParser(parse_string
):
76 """Creates a Groc Parser."""
77 input_string
= antlr3
.ANTLRStringStream(parse_string
)
78 lexer
= GrocLexerWithErrors(input_string
)
79 tokens
= antlr3
.CommonTokenStream(lexer
)
80 parser
= GrocParserWithErrors(tokens
)