Stay organized with collections
Save and categorize content based on your preferences.
Source code for google.appengine.api.search.expression_parser
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Wrapper for ExpressionParser."""
from google.appengine._internal import antlr3
from google.appengine.api.search import ExpressionLexer
from google.appengine.api.search import ExpressionParser
from google.appengine.api.search import unicode_util
[docs]class ExpressionException(Exception):
"""An error occurred while parsing the expression input string."""
[docs]class ExpressionLexerWithErrors(ExpressionLexer.ExpressionLexer):
"""An overridden Lexer that raises exceptions."""
[docs] def emitErrorMessage(self, msg):
"""Raise an exception if the input fails to parse correctly.
Overriding the default, which normally just prints a message to
stderr.
Arguments:
msg: the error message
Raises:
ExpressionException: always.
"""
raise ExpressionException(msg)
[docs]class ExpressionParserWithErrors(ExpressionParser.ExpressionParser):
"""An overridden Parser that raises exceptions."""
[docs] def emitErrorMessage(self, msg):
"""Raise an exception if the input fails to parse correctly.
Overriding the default, which normally just prints a message to
stderr.
Arguments:
msg: the error message
Raises:
ExpressionException: always.
"""
raise ExpressionException(msg)
[docs]def CreateParser(expression):
"""Creates a Expression Parser."""
input_string = antlr3.ANTLRStringStream(unicode_util.LimitUnicode(expression))
lexer = ExpressionLexerWithErrors(input_string)
tokens = antlr3.CommonTokenStream(lexer)
parser = ExpressionParserWithErrors(tokens)
return parser
[docs]def Parse(expression):
"""Parses an expression and returns the ANTLR tree."""
parser = CreateParser(expression)
try:
return parser.expression()
except Exception, e:
raise ExpressionException(e.message)
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-06-16 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-16 UTC."],[[["This code provides a wrapper for parsing expressions within the Google App Engine search API, using ANTLR for lexical analysis and parsing."],["It defines a custom `ExpressionException` to handle errors during the parsing process, which is raised when parsing fails."],["The code overrides the default behavior of the lexer and parser (`ExpressionLexerWithErrors`, `ExpressionParserWithErrors`) to raise exceptions instead of printing error messages to stderr."],["The `CreateParser` function sets up the necessary components for parsing, including an ANTLR string stream, a lexer, and a parser."],["The `Parse` function takes an expression string, creates a parser, and returns the resulting ANTLR tree, or raises an `ExpressionException` if parsing fails."]]],[]]