App Engine Python SDK version 1.7.7
[gae.git] / python / google / appengine / cron / groc.py
blobe907d2a73b174a23402230bb39205a550d9c33aa
1 #!/usr/bin/env python
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."""
29 import google
31 import antlr3
33 import GrocLexer
34 import GrocParser
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
48 stderr.
50 Arguments:
51 msg: the error message
52 Raises:
53 GrocException: always.
54 """
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
65 stderr.
67 Arguments:
68 msg: the error message
69 Raises:
70 GrocException: always.
71 """
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)
81 return parser