#NEWLINE# def _EBQParser( clauses ) : #NEWLINE# #INDENT# "Defines the entire EBQ query.\n\n  Actions only occur when parseString is called on the BNF returned.\n  All actions will modify the original dictionary passed in as the argument\n  when the BNF was generated.\n\n  The dictionary will map clause names to their respective argument. Below\n  each clause's argument arrangement is explained:\n\n  SELECT:\n  List of postfix expressions (one for each comma separated expression).\n  Each postfix expression is also a stack represented using a list.\n  a, b + 1 --> [['a'], ['b', '1', '+']]\n\n  FROM:\n  List of table names.\n  table1, table2, ... --> [table1, table2, ...]\n\n  WITHIN:\n  Dictionary mapping expression indices to within modifiers.\n  expr0 within a, expr1, expr2 within b --> {0: 'a', 2: 'b'}\n\n  AS:\n  Dictionary mapping expression indices to aliases.\n  expr0 as a, expr1, expr as b --> {0: 'a', 2: 'b'}\n\n  WHERE:\n  List containing single postfix expression.\n  WHERE a < 1 --> ['a', '1', '<']\n\n  HAVING:\n  List containing single postfix expression.\n  HAVING SUM(Year) < 1 --> ['Year', 'AGGREGATION_1_SUM', '1', '<']\n\n  GROUP BY:\n  List of fields.\n  GROUP BY f1, f2, ... --> [f1, f2, ...]\n\n  ORDER BY:\n  List of fields.\n  ORDER BY f1, f2, ... --> [f1, f2, ...]\n\n  LIMIT:\n  List of a single integer.\n  LIMIT n --> [n]\n\n  Arguments:\n    clauses: Dictionary containing clause name to arguments. Originally, all\n      arguments have initial, empty values.\n\n  Returns:\n    A BNF of a EBQ query.\n  " #NEWLINE# def AddAll( tokens ) : #NEWLINE# #INDENT# temp_stack . append ( '' . join ( tokens ) ) #NEWLINE# #UNINDENT# def AddArgument( tokens ) : #NEWLINE# #INDENT# clauses [ tokens [ 0 ] ] . extend ( temp_stack ) #NEWLINE# temp_stack [ : ] = [ ] #NEWLINE# #UNINDENT# def AddSelectArgument( ) : #NEWLINE# #INDENT# clauses [ 'SELECT' ] . append ( list ( temp_stack ) ) #NEWLINE# temp_stack [ : ] = [ ] #NEWLINE# #UNINDENT# def AddLabel( tokens ) : #NEWLINE# #INDENT# temp_stack . append ( tokens [ 0 ] ) #NEWLINE# #UNINDENT# def AddAlias( tokens ) : #NEWLINE# #INDENT# clauses [ 'AS' ] [ len ( clauses [ 'SELECT' ] ) ] = tokens [ 0 ] #NEWLINE# #UNINDENT# def AddInteger( tokens ) : #NEWLINE# #INDENT# temp_stack . append ( int ( tokens [ 0 ] ) ) #NEWLINE# #UNINDENT# def AddLast( tokens ) : #NEWLINE# #INDENT# temp_stack [ ( len ( temp_stack ) - 1 ) ] += ( ' ' + tokens [ 0 ] ) #NEWLINE# #UNINDENT# def AddWithin( tokens ) : #NEWLINE# #INDENT# clauses [ 'WITHIN' ] [ len ( clauses [ 'SELECT' ] ) ] = tokens [ 0 ] #NEWLINE# #UNINDENT# temp_stack = [ ] #NEWLINE# as_kw = pp . CaselessKeyword ( 'AS' ) #NEWLINE# select_kw = pp . CaselessKeyword ( 'SELECT' ) #NEWLINE# within_kw = pp . CaselessKeyword ( 'WITHIN' ) #NEWLINE# flatten_kw = pp . CaselessKeyword ( 'FLATTEN' ) #NEWLINE# from_kw = pp . CaselessKeyword ( 'FROM' ) #NEWLINE# where_kw = pp . CaselessKeyword ( 'WHERE' ) #NEWLINE# having_kw = pp . CaselessKeyword ( 'HAVING' ) #NEWLINE# order_kw = pp . CaselessKeyword ( 'ORDER BY' ) #NEWLINE# asc_kw = pp . CaselessKeyword ( 'ASC' ) #NEWLINE# desc_kw = pp . CaselessKeyword ( 'DESC' ) #NEWLINE# group_kw = pp . CaselessKeyword ( 'GROUP BY' ) #NEWLINE# limit_kw = pp . CaselessKeyword ( 'LIMIT' ) #NEWLINE# push_label = pp . Word ( pp . alphas , ( ( ( pp . alphas + pp . nums ) + '_' ) + '.' ) ) . setParseAction ( AddLabel ) #NEWLINE# pos_int = pp . Word ( pp . nums ) . setParseAction ( AddInteger ) #NEWLINE# order_label = ( push_label + pp . Optional ( ( asc_kw | desc_kw ) . setParseAction ( AddLast ) ) ) #NEWLINE# label = pp . Word ( pp . alphas , ( ( ( pp . alphas + pp . nums ) + '_' ) + '.' ) ) #NEWLINE# alias_label = pp . Word ( pp . alphas , ( ( ( pp . alphas + pp . nums ) + '_' ) + '.' ) ) . setParseAction ( AddAlias ) #NEWLINE# within_label = pp . Word ( pp . alphas , ( ( ( pp . alphas + pp . nums ) + '_' ) + '.' ) ) . setParseAction ( AddWithin ) #NEWLINE# math_expr = _MathParser ( temp_stack ) #NEWLINE# within_expr = ( math_expr + pp . Optional ( ( within_kw + within_label ) ) ) #NEWLINE# alias_expr = ( within_expr + pp . Optional ( ( ( as_kw + alias_label ) | ( ( ( ( ( ( ( ~ from_kw ) + ( ~ where_kw ) ) + ( ~ having_kw ) ) + ( ~ order_kw ) ) + ( ~ group_kw ) ) + ( ~ limit_kw ) ) + alias_label ) ) ) ) #NEWLINE# select_expr = ( ( select_kw + alias_expr ) . setParseAction ( AddSelectArgument ) + pp . ZeroOrMore ( ( pp . Literal ( ',' ) + alias_expr ) . setParseAction ( AddSelectArgument ) ) ) #NEWLINE# flatten_expr = pp . OneOrMore ( ( ( ( ( ( ( ( pp . Literal ( '(' ) + flatten_kw ) + pp . Literal ( '(' ) ) + label ) + pp . Literal ( ',' ) ) + label ) + pp . Literal ( ')' ) ) + pp . Literal ( ')' ) ) ) . setParseAction ( AddAll ) #NEWLINE# from_expr = ( select_expr + pp . Optional ( ( from_kw + ( flatten_expr | push_label ) ) . setParseAction ( AddArgument ) ) ) #NEWLINE# where_expr = ( from_expr + pp . Optional ( ( where_kw + _MathParser ( temp_stack ) ) . setParseAction ( AddArgument ) ) ) #NEWLINE# having_expr = ( where_expr + pp . Optional ( ( having_kw + _MathParser ( temp_stack ) ) . setParseAction ( AddArgument ) ) ) #NEWLINE# group_expr = ( having_expr + pp . Optional ( ( ( group_kw + push_label ) + pp . ZeroOrMore ( ( pp . Literal ( ',' ) + push_label ) ) ) . setParseAction ( AddArgument ) ) ) #NEWLINE# order_expr = ( group_expr + pp . Optional ( ( ( order_kw + order_label ) + pp . ZeroOrMore ( ( pp . Literal ( ',' ) + order_label ) ) ) . setParseAction ( AddArgument ) ) ) #NEWLINE# limit_expr = ( order_expr + pp . Optional ( ( limit_kw + pos_int ) . setParseAction ( AddArgument ) ) ) #NEWLINE# entire_expr = ( limit_expr + pp . StringEnd ( ) ) #NEWLINE# return entire_expr