<s> """<STR_LIT>""" <EOL> from google . appengine . _internal import antlr3 <EOL> from google . appengine . _internal . antlr3 import tree <EOL> from google . appengine . api . search import QueryLexer <EOL> from google . appengine . api . search import QueryParser <EOL> COMPARISON_TYPES = [ <EOL> QueryParser . EQ , <EOL> QueryParser . NE , <EOL> QueryParser . GT , <EOL> QueryParser . GE , <EOL> QueryParser . LT , <EOL> QueryParser . LE , <EOL> ] <EOL> class QueryException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class QueryLexerWithErrors ( QueryLexer . QueryLexer ) : <EOL> """<STR_LIT>""" <EOL> def emitErrorMessage ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> raise QueryException ( msg ) <EOL> class QueryParserWithErrors ( QueryParser . QueryParser ) : <EOL> """<STR_LIT>""" <EOL> def emitErrorMessage ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> raise QueryException ( msg ) <EOL> def CreateParser ( query ) : <EOL> """<STR_LIT>""" <EOL> input_string = antlr3 . ANTLRStringStream ( query ) <EOL> lexer = QueryLexerWithErrors ( input_string ) <EOL> tokens = antlr3 . CommonTokenStream ( lexer ) <EOL> parser = QueryParserWithErrors ( tokens ) <EOL> return parser <EOL> def ParseAndSimplify ( query ) : <EOL> """<STR_LIT>""" <EOL> node = Parse ( query ) . tree <EOL> node = _ColonToEquals ( node ) <EOL> node = SequenceToConjunction ( node ) <EOL> node = SimplifyNode ( node ) <EOL> return node <EOL> def Parse ( query ) : <EOL> """<STR_LIT>""" <EOL> parser = CreateParser ( query ) <EOL> try : <EOL> return parser . query ( ) <EOL> except Exception , e : <EOL> raise QueryException ( e . message ) <EOL> def ConvertNodes ( node , from_type , to_type , to_text ) : <EOL> """<STR_LIT>""" <EOL> if node . getType ( ) == from_type : <EOL> new_node = CreateQueryNode ( to_text , to_type ) <EOL> else : <EOL> new_node = node <EOL> convert_children = lambda c : ConvertNodes ( c , from_type , to_type , to_text ) <EOL> new_node . children = map ( convert_children , node . children ) <EOL> return new_node <EOL> def _ColonToEquals ( node ) : <EOL> """<STR_LIT>""" <EOL> return ConvertNodes ( node , QueryParser . HAS , QueryParser . EQ , '<STR_LIT:=>' ) <EOL> def SequenceToConjunction ( node ) : <EOL> """<STR_LIT>""" <EOL> return ConvertNodes ( <EOL> node , QueryParser . SEQUENCE , QueryParser . CONJUNCTION , '<STR_LIT>' ) <EOL> def Simplify ( parser_return ) : <EOL> """<STR_LIT>""" <EOL> if parser_return . tree : <EOL> return SimplifyNode ( parser_return . tree ) <EOL> return parser_return <EOL> def SimplifyNode ( node ) : <EOL> """<STR_LIT>""" <EOL> if not node . getType ( ) : <EOL> return SimplifyNode ( node . children [ <NUM_LIT:0> ] ) <EOL> elif node . getType ( ) == QueryParser . CONJUNCTION and node . getChildCount ( ) == <NUM_LIT:1> : <EOL> return SimplifyNode ( node . children [ <NUM_LIT:0> ] ) <EOL> elif node . getType ( ) == QueryParser . DISJUNCTION and node . getChildCount ( ) == <NUM_LIT:1> : <EOL> return SimplifyNode ( node . children [ <NUM_LIT:0> ] ) <EOL> elif ( ( node . getType ( ) == QueryParser . EQ or node . getType ( ) == QueryParser . HAS ) <EOL> and node . getChildCount ( ) == <NUM_LIT:1> ) : <EOL> return SimplifyNode ( node . children [ <NUM_LIT:0> ] ) <EOL> for i , child in enumerate ( node . children ) : <EOL> node . setChild ( i , SimplifyNode ( child ) ) <EOL> return node <EOL> def CreateQueryNode ( text , type ) : <EOL> token = tree . CommonTreeAdaptor ( ) . createToken ( tokenType = type , text = text ) <EOL> return tree . CommonTree ( token ) <EOL> def GetQueryNodeText ( node ) : <EOL> """<STR_LIT>""" <EOL> return GetQueryNodeTextUnicode ( node ) . encode ( '<STR_LIT:utf-8>' ) <EOL> def GetQueryNodeTextUnicode ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . getType ( ) == QueryParser . VALUE and len ( node . children ) >= <NUM_LIT:2> : <EOL> return u'<STR_LIT>' . join ( c . getText ( ) for c in node . children [ <NUM_LIT:1> : ] ) <EOL> elif node . getType ( ) == QueryParser . VALUE : <EOL> return None <EOL> return node . getText ( ) <EOL> def GetPhraseQueryNodeText ( node ) : <EOL> """<STR_LIT>""" <EOL> text = GetQueryNodeText ( node ) <EOL> if text : <EOL> if text [ <NUM_LIT:0> ] == '<STR_LIT:">' and text [ - <NUM_LIT:1> ] == '<STR_LIT:">' : <EOL> text = text [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> return text <EOL> def IsPhrase ( node ) : <EOL> """<STR_LIT>""" <EOL> text = GetQueryNodeText ( node ) <EOL> return ( node . getType ( ) == QueryParser . VALUE and <EOL> text . startswith ( '<STR_LIT:">' ) and text . endswith ( '<STR_LIT:">' ) ) </s>
