# 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. import pyparsing as p from monasca_analytics.banana.cli import const EQUALS = p.Literal("=").suppress() CONNECT = p.Literal("->").suppress() DISCONNECT = p.Literal("!->").suppress() LPAREN = p.Literal("(").suppress() RPAREN = p.Literal(")").suppress() LOAD = p.CaselessKeyword("load").suppress() SAVE = p.CaselessKeyword("save").suppress() REMOVE = p.CaselessKeyword("rm").suppress() PRINT = p.CaselessKeyword("print").suppress() LIST = p.CaselessKeyword("list").suppress() HELP = p.CaselessKeyword("help").suppress() DOT = p.Literal(".").suppress() VARNAME = p.Word(p.alphas + "_", p.alphanums + "_") PARAMETER = p.Word(p.alphanums + "_-") MODULE_NAME = p.Word(p.alphanums + "_-") VALUE = p.Word(p.alphanums + "_-.") PATH = p.Word(p.alphanums + "_-/\.") cmd_create = (VARNAME + EQUALS + MODULE_NAME) cmd_connect = (VARNAME + CONNECT + VARNAME) cmd_disconnect = (VARNAME + DISCONNECT + VARNAME)
def make_integer_word_expr(int_name, int_value): return pp.CaselessKeyword(int_name).addParseAction( pp.replaceWith(int_value))
def __init__(self): """ Our simple BNF: SELECT [fields[*] FROM table WHERE clause """ integer = pp.Combine(pp.Optional(pp.oneOf("+ -")) + pp.Word(pp.nums)).setParseAction( lambda t: int(t[0])) floatNumber = pp.Regex(r'\d+(\.\d*)?([eE]\d+)?') point = pp.Literal(".") e = pp.CaselessLiteral("E") kw_select = pp.CaselessKeyword('select') kw_update = pp.CaselessKeyword('update') kw_insert = pp.CaselessKeyword('insert') kw_delete = pp.CaselessKeyword('delete') kw_from = pp.CaselessKeyword('from') kw_into = pp.CaselessKeyword('into') kw_where = pp.CaselessKeyword('where') kw_at = pp.CaselessKeyword('at') kw_set = pp.CaselessKeyword('set') kw_true = pp.CaselessKeyword('true').setParseAction(lambda t: 1) kw_false = pp.CaselessKeyword('false').setParseAction(lambda t: 0) # Regex string representing the set of possible operators # Example : ">=|<=|!=|>|<|=" OPERATOR_RX = "(?i)%s" % '|'.join( [re.sub('\|', '\|', o) for o in Predicate.operators.keys()]) # predicate field = pp.Word(pp.alphanums + '_' + '.' + '-') operator = pp.Regex(OPERATOR_RX).setName("operator") variable = pp.Literal('$').suppress() + pp.Word( pp.alphanums + '_' + '.' + '-').setParseAction(lambda t: "$%s" % t[0]) obj = pp.Forward() value = obj | pp.QuotedString('"') | pp.QuotedString( "'") | kw_true | kw_false | integer | variable def handle_value_list(s, l, t): t = t.asList() new_t = [t] debug("[handle_value_list] s = %(s)s ** l = %(l)s ** t = %(t)s" % locals()) debug(" new_t = %(new_t)s" % locals()) return new_t value_list = value \ | (pp.Literal("[").suppress() + pp.Literal("]").suppress()) \ .setParseAction(lambda s, l, t: [[]]) \ | pp.Literal("[").suppress() \ + pp.delimitedList(value) \ .setParseAction(handle_value_list) \ + pp.Literal("]") \ .suppress() table = pp.Word(pp.alphanums + ':_-').setResultsName('object') field_list = pp.Literal("*") | pp.delimitedList(field).setParseAction( lambda tokens: set(tokens)) assoc = (field + pp.Literal(":").suppress() + value_list).setParseAction(lambda tokens: [tokens.asList()]) obj << pp.Literal("{").suppress() + pp.delimitedList( assoc).setParseAction(lambda t: dict(t.asList())) + pp.Literal( "}").suppress() # PARAMETER (SET) # X = Y --> t=(X, Y) def handle_param(s, l, t): t = t.asList() assert len(t) == 2 new_t = tuple(t) debug("[handle_param] s = %(s)s ** l = %(l)s ** t = %(t)s" % locals()) debug(" new_t = %(new_t)s" % locals()) debug(" (we expect a tuple)") return new_t param = (field + pp.Literal("=").suppress() + value_list) \ .setParseAction(handle_param) # PARAMETERS (SET) # PARAMETER[, PARAMETER[, ...]] --> dict() def handle_parameters(s, l, t): t = list(t.asList()) new_t = dict(t) if t else dict() debug("[handle_parameters] s = %(s)s ** l = %(l)s ** t = %(t)s" % locals()) debug(" new_t = %(new_t)s" % locals()) debug(" (we expect a dict)") return new_t parameters = pp.delimitedList(param) \ .setParseAction(handle_parameters) predicate = (field + operator + value_list).setParseAction( self.handlePredicate) # clause of predicates and_op = pp.CaselessLiteral("and") | pp.Keyword("&&") or_op = pp.CaselessLiteral("or") | pp.Keyword("||") not_op = pp.Keyword("!") predicate_precedence_list = [ (not_op, 1, pp.opAssoc.RIGHT, lambda x: self.handleClause(*x)), (and_op, 2, pp.opAssoc.LEFT, lambda x: self.handleClause(*x)), (or_op, 2, pp.opAssoc.LEFT, lambda x: self.handleClause(*x)) ] clause = pp.operatorPrecedence( predicate, predicate_precedence_list ) #.setParseAction(lambda clause: Filter.from_clause(clause)) # END: clause of predicates # For the time being, we only support simple filters and not full clauses filter = pp.delimitedList( predicate, delim='&&').setParseAction(lambda tokens: Filter(tokens.asList())) datetime = pp.Regex(r'....-..-.. ..:..:..') timestamp = pp.CaselessKeyword('now') | datetime select_elt = (kw_select.suppress() + field_list.setResultsName('fields')) where_elt = (kw_where.suppress() + filter.setResultsName('filters')) set_elt = (kw_set.suppress() + parameters.setResultsName('params')) at_elt = (kw_at.suppress() + timestamp.setResultsName('timestamp')) # SELECT *|field_list [AT timestamp] FROM table [WHERE clause] # UPDATE table SET parameters [WHERE clause] [SELECT *|field_list] # INSERT INTO table SET parameters [SELECT *|field_list] # DELETE FROM table [WHERE clause] select = (select_elt + pp.Optional(at_elt) + kw_from.suppress() + table + pp.Optional(where_elt) ).setParseAction(lambda args: self.action(args, 'get')) update = (kw_update + table + set_elt + pp.Optional(where_elt) + pp.Optional(select_elt) ).setParseAction(lambda args: self.action(args, 'update')) insert = (kw_insert + kw_into + table + set_elt + pp.Optional(select_elt) ).setParseAction(lambda args: self.action(args, 'create')) delete = (kw_delete + kw_from + table + pp.Optional(where_elt) ).setParseAction(lambda args: self.action(args, 'delete')) self.bnf = select | update | insert | delete
return self.value def visit(self, visitor: Callable[[Any], None]): visitor(self) visitor(self.value) lparen = pp.Suppress("(") rparen = pp.Suppress(")") and_ = pp.CaselessLiteral("AND") or_ = pp.CaselessLiteral("OR") op = pp.oneOf((op for op in OPERATORS)) true_ = pp.CaselessKeyword("true").setParseAction(Boolean) false_ = pp.CaselessKeyword("false").setParseAction(Boolean) alphaword = pp.Word(pp.alphanums + "_" + "-") string = pp.QuotedString(quoteChar="'").setParseAction(String) boolean = true_ | false_ number = (pp.Word(pp.nums) + pp.Optional("." + pp.OneOrMore(pp.Word(pp.nums)))).setParseAction(Number) identifier = alphaword.setParseAction(Identifier) expr = pp.Forward() condition = pp.Group(identifier + pp.Optional(op + (string | number | boolean | identifier))).setParseAction( Condition
def __init__(self): # Bibtex keywords string_def_start = pp.CaselessKeyword("@string") preamble_start = pp.CaselessKeyword("@preamble") comment_line_start = pp.CaselessKeyword('@comment') # String names string_name = pp.Word(pp.alphanums + '_')('StringName') self.set_string_name_parse_action(lambda s, l, t: None) string_name.addParseAction(self._string_name_parse_action) # Values inside bibtex fields # Values can be integer or string expressions. The latter may use # quoted or braced values. # Integer values integer = pp.Word(pp.nums)('Integer') # Braced values: braced values can contain nested (but balanced) braces braced_value_content = pp.CharsNotIn('{}') braced_value = pp.Forward() # Recursive definition for nested braces braced_value <<= pp.originalTextFor( '{' + pp.ZeroOrMore(braced_value | braced_value_content) + '}' )('BracedValue') braced_value.setParseAction(remove_braces) # TODO add ignore for "\}" and "\{" ? # TODO @ are not parsed by bibtex in braces # Quoted values: may contain braced content with balanced braces brace_in_quoted = pp.nestedExpr('{', '}', ignoreExpr=None) text_in_quoted = pp.CharsNotIn('"{}') # (quotes should be escaped by braces in quoted value) quoted_value = pp.originalTextFor( '"' + pp.ZeroOrMore(text_in_quoted | brace_in_quoted) + '"' )('QuotedValue') quoted_value.addParseAction(pp.removeQuotes) # String expressions string_expr = pp.delimitedList( (quoted_value | braced_value | string_name), delim='#' )('StringExpression') self.set_string_expression_parse_action(lambda s, l, t: None) string_expr.addParseAction(self._string_expr_parse_action) value = (integer | string_expr)('Value') # Entries # @EntryType { ... entry_type = (pp.Suppress('@') + pp.Word(pp.alphas))('EntryType') entry_type.setParseAction(first_token) # Entry key: any character up to a ',' without leading and trailing # spaces. key = pp.SkipTo(',')('Key') # Exclude @',\#}{~% key.setParseAction(lambda s, l, t: first_token(s, l, t).strip()) # Field name: word of letters, digits, dashes and underscores field_name = pp.Word(pp.alphanums + '_-()')('FieldName') field_name.setParseAction(first_token) # Field: field_name = value field = pp.Group(field_name + pp.Suppress('=') + value)('Field') field.setParseAction(field_to_pair) # List of fields: comma separeted fields field_list = (pp.delimitedList(field) + pp.Suppress(pp.Optional(',')) )('Fields') field_list.setParseAction( lambda s, l, t: {k: v for (k, v) in reversed(t.get('Fields'))}) # Entry: type, key, and fields self.entry = (entry_type + in_braces_or_pars(key + pp.Suppress(',') + field_list) )('Entry') # Other stuff: comments, string definitions, and preamble declarations # Explicit comments: @comment + everything up to next valid declaration # starting on new line. not_an_implicit_comment = (pp.LineStart() + pp.Literal('@') ) | pp.stringEnd() self.explicit_comment = ( pp.Suppress(comment_line_start) + pp.originalTextFor(pp.SkipTo(not_an_implicit_comment), asString=True))('ExplicitComment') self.explicit_comment.addParseAction(remove_trailing_newlines) self.explicit_comment.addParseAction(remove_braces) # Previous implementation included comment until next '}'. # This is however not inline with bibtex behavior that is to only # ignore until EOL. Brace stipping is arbitrary here but avoids # duplication on bibtex write. # Empty implicit_comments lead to infinite loop of zeroOrMore def mustNotBeEmpty(t): if not t[0]: raise pp.ParseException("Match must not be empty.") # Implicit comments: not anything else self.implicit_comment = pp.originalTextFor( pp.SkipTo(not_an_implicit_comment).setParseAction(mustNotBeEmpty), asString=True)('ImplicitComment') self.implicit_comment.addParseAction(remove_trailing_newlines) # String definition self.string_def = (pp.Suppress(string_def_start) + in_braces_or_pars( string_name + pp.Suppress('=') + string_expr('StringValue') ))('StringDefinition') # Preamble declaration self.preamble_decl = (pp.Suppress(preamble_start) + in_braces_or_pars(value))('PreambleDeclaration') # Main bibtex expression self.main_expression = pp.ZeroOrMore( self.string_def | self.preamble_decl | self.explicit_comment | self.entry | self.implicit_comment)
def _EBQParser(clauses): """Defines the entire EBQ query. Actions only occur when parseString is called on the BNF returned. All actions will modify the original dictionary passed in as the argument when the BNF was generated. The dictionary will map clause names to their respective argument. Below each clause's argument arrangement is explained: SELECT: List of postfix expressions (one for each comma separated expression). Each postfix expression is also a stack represented using a list. a, b + 1 --> [['a'], ['b', '1', '+']] FROM: List of table names. table1, table2, ... --> [table1, table2, ...] WITHIN: Dictionary mapping expression indices to within modifiers. expr0 within a, expr1, expr2 within b --> {0: 'a', 2: 'b'} AS: Dictionary mapping expression indices to aliases. expr0 as a, expr1, expr as b --> {0: 'a', 2: 'b'} WHERE: List containing single postfix expression. WHERE a < 1 --> ['a', '1', '<'] HAVING: List containing single postfix expression. HAVING SUM(Year) < 1 --> ['Year', 'AGGREGATION_1_SUM', '1', '<'] GROUP BY: List of fields. GROUP BY f1, f2, ... --> [f1, f2, ...] ORDER BY: List of fields. ORDER BY f1, f2, ... --> [f1, f2, ...] LIMIT: List of a single integer. LIMIT n --> [n] Arguments: clauses: Dictionary containing clause name to arguments. Originally, all arguments have initial, empty values. Returns: A BNF of a EBQ query. """ def AddAll(tokens): temp_stack.append(''.join(tokens)) def AddArgument(tokens): clauses[tokens[0]].extend(temp_stack) temp_stack[:] = [] def AddSelectArgument(): clauses['SELECT'].append(list(temp_stack)) temp_stack[:] = [] def AddLabel(tokens): temp_stack.append(tokens[0]) def AddAlias(tokens): clauses['AS'][len(clauses['SELECT'])] = tokens[0] def AddInteger(tokens): temp_stack.append(int(tokens[0])) def AddLast(tokens): temp_stack[len(temp_stack) - 1] += ' ' + tokens[0] def AddWithin(tokens): clauses['WITHIN'][len(clauses['SELECT'])] = tokens[0] def AddJoinArgument(tokens): clauses[tokens[0]].append(list(temp_stack)) temp_stack[:] = [] temp_stack = [] as_kw = pp.CaselessKeyword('AS') select_kw = pp.CaselessKeyword('SELECT') within_kw = pp.CaselessKeyword('WITHIN') flatten_kw = pp.CaselessKeyword('FLATTEN') from_kw = pp.CaselessKeyword('FROM') join_kw = pp.CaselessKeyword('JOIN') join_on_kw = pp.CaselessKeyword('ON') where_kw = pp.CaselessKeyword('WHERE') having_kw = pp.CaselessKeyword('HAVING') order_kw = pp.CaselessKeyword('ORDER BY') asc_kw = pp.CaselessKeyword('ASC') desc_kw = pp.CaselessKeyword('DESC') group_kw = pp.CaselessKeyword('GROUP BY') limit_kw = pp.CaselessKeyword('LIMIT') push_label = pp.Word( pp.alphas, pp.alphas + pp.nums + '_' + '.').setParseAction(AddLabel) pos_int = pp.Word(pp.nums).setParseAction(AddInteger) order_label = (push_label + pp.Optional((asc_kw | desc_kw).setParseAction(AddLast))) label = pp.Word(pp.alphas, pp.alphas + pp.nums + '_' + '.') alias_label = pp.Word( pp.alphas, pp.alphas + pp.nums + '_' + '.').setParseAction(AddAlias) within_label = pp.Word( pp.alphas, pp.alphas + pp.nums + '_' + '.').setParseAction(AddWithin) math_expr = _MathParser(temp_stack) within_expr = math_expr + pp.Optional(within_kw + within_label) alias_expr = within_expr + pp.Optional(( (as_kw + alias_label) | (~from_kw + ~where_kw + ~group_kw + ~having_kw + ~order_kw + ~limit_kw + alias_label))) select_expr = ( (select_kw + alias_expr).setParseAction(AddSelectArgument) + pp.ZeroOrMore((pp.Literal(',') + alias_expr).setParseAction( AddSelectArgument))) flatten_expr = (pp.OneOrMore(pp.Literal('(') + flatten_kw + pp.Literal('(') + label + pp.Literal(',') + label + pp.Literal(')') + pp.Literal( ')'))).setParseAction(AddAll) from_expr = select_expr + pp.Optional(( from_kw + (flatten_expr | push_label)).setParseAction(AddArgument)) join_expr = from_expr + pp.ZeroOrMore(( join_kw + push_label + join_on_kw + _MathParser(temp_stack)).setParseAction(AddJoinArgument)) where_expr = join_expr + pp.Optional(( where_kw + _MathParser(temp_stack)).setParseAction(AddArgument)) group_expr = where_expr + pp.Optional(( group_kw + push_label + pp.ZeroOrMore( pp.Literal(',') + push_label)).setParseAction(AddArgument)) having_expr = group_expr + pp.Optional(( having_kw + _MathParser(temp_stack)).setParseAction(AddArgument)) order_expr = having_expr + pp.Optional(( order_kw + order_label + pp.ZeroOrMore( pp.Literal(',') + order_label)).setParseAction(AddArgument)) limit_expr = order_expr + pp.Optional(( limit_kw + pos_int).setParseAction(AddArgument)) entire_expr = limit_expr + pp.StringEnd() return entire_expr
def _MathParser(math_stack): """Defines the entire math expression for BigQuery queries. Converts the expression into postfix notation. The stack is reversed (i.e. the last element acts the top of the stack). Actions do not occur unless parseString is called on the BNF returned. The actions will modify the original list that was passed when the BNF was generated. The <math_stack> will return the single expression converted to postfix. Arguments: math_stack: Returns postfix notation of one math expression. Returns: A BNF of an math/string expression. """ def PushAggregation(tokens): """Pushes aggregation functions onto the stack. When the aggregation is pushed, the name is rewritten. The label is prepended with AGGREGATION_ to signal that an aggregation is occurring. Following this prefix is an integer, which represents the number of comma separated arguments that were provided. Finally, the name of the function is appended to the label. For most functions, the aggregation name is simply appended. However, there are special exceptions for COUNT. A normal count function is rewritten as AGGREGATION_i_COUNT. However, a count with the distinct keyword is rewritten to AGGREGATION_i_DISTINCTCOUNT. Args: tokens: The function name and arguments in a list object. """ function_name = tokens[0] # Rename count with distinct keyword as distinctcount. if function_name == 'COUNT': if 'DISTINCT' in list(tokens): function_name = 'DISTINCTCOUNT' # Assume all aggregation functions have at least one argument. # If a function n commas, then it has n + 1 arguments. num_args = 1 for token in tokens: if token == ',': num_args += 1 math_stack.append(util.AggregationFunctionToken(function_name, num_args)) def PushFunction(tokens): """Push a function token onto the stack. Args: tokens: list of all tokens, tokens[0] is the function name str. """ math_stack.append(util.BuiltInFunctionToken(tokens[0])) def PushSingleToken(tokens): """Push the topmost token onto the stack.""" if util.IsFloat(tokens[0]): try: token = int(tokens[0]) except ValueError: token = float(tokens[0]) elif tokens[0].startswith('\'') or tokens[0].startswith('"'): token = util.StringLiteralToken(tokens[0]) elif tokens[0].lower() in util.BIGQUERY_CONSTANTS: token = util.LiteralToken(tokens[0].lower(), util.BIGQUERY_CONSTANTS[tokens[0].lower()]) else: token = util.FieldToken(tokens[0]) math_stack.append(token) def PushCountStar(tokens): if tokens[0] != '*': raise ValueError('Not a count star argument.') math_stack.append(util.CountStarToken()) def PushUnaryOperators(tokens): # The list must be reversed since unary operations are unwrapped in the # other direction. An example is ~-1. The negation occurs before the bit # inversion. for i in reversed(range(0, len(tokens))): if tokens[i] == '-': math_stack.append(int('-1')) math_stack.append(util.OperatorToken('*', 2)) elif tokens[i] == '~': math_stack.append(util.OperatorToken('~', 1)) elif tokens[i].lower() == 'not': math_stack.append(util.OperatorToken('not', 1)) def PushBinaryOperator(tokens): math_stack.append(util.OperatorToken(tokens[0], 2)) # Miscellaneous symbols and keywords. comma = pp.Literal(',') decimal = pp.Literal('.') exponent_literal = pp.CaselessLiteral('E') lp = pp.Literal('(') rp = pp.Literal(')') count_star = pp.Literal('*') distinct_keyword = pp.CaselessKeyword('DISTINCT') # Any non-space containing sequence of characters that must begin with # an alphabetical character and contain alphanumeric characters # and underscores (i.e. function or variable names). label = pp.Word(pp.alphas, pp.alphas + pp.nums + '_' + '.') # A single/double quote surrounded string. string = pp.quotedString # Various number representations. integer = pp.Word(pp.nums) decimal_type1 = pp.Combine(integer + decimal + pp.Optional(integer)) decimal_type2 = pp.Combine(decimal + integer) real = decimal_type1 | decimal_type2 exponent = exponent_literal + pp.Word('+-' + pp.nums, pp.nums) number_without_exponent = real | integer number = pp.Combine(number_without_exponent + pp.Optional(exponent)) integer_argument = pp.Word(pp.nums) integer_argument.setParseAction(PushSingleToken) # Forward declaration for recusive grammar. We assume that full_expression can # represent any expression that is valid. full_expression = pp.Forward() # Aggregation function definitions. avg_function = pp.CaselessKeyword('AVG') + lp + full_expression + rp count_star.setParseAction(PushCountStar) count_argument = ((pp.Optional(distinct_keyword) + full_expression) | count_star) count_function = (pp.CaselessKeyword('COUNT') + lp + count_argument + pp.Optional(comma + integer_argument) + rp) quantiles_function = (pp.CaselessKeyword('QUANTILES') + lp + full_expression + pp.Optional(comma + integer_argument) + rp) stddev_function = pp.CaselessKeyword('STDDEV') + lp + full_expression + rp variance_function = pp.CaselessKeyword('VARIANCE') + lp + full_expression + rp last_function = pp.CaselessKeyword('LAST') + lp + full_expression + rp max_function = pp.CaselessKeyword('MAX') + lp + full_expression + rp min_function = pp.CaselessKeyword('MIN') + lp + full_expression + rp nth_function = (pp.CaselessKeyword('NTH') + lp + integer_argument + comma + full_expression + rp) group_concat_function = (pp.CaselessKeyword('GROUP_CONCAT') + lp + full_expression + rp) sum_function = pp.CaselessKeyword('SUM') + lp + full_expression + rp top_function = (pp.CaselessKeyword('TOP') + lp + full_expression + pp.Optional(comma + integer_argument + pp.Optional(comma + integer_argument)) + rp) aggregate_functions = (avg_function | count_function | quantiles_function | stddev_function | variance_function | last_function | max_function | min_function | nth_function | group_concat_function | sum_function | top_function) aggregate_functions.setParseAction(PushAggregation) functions_arguments = pp.Optional(full_expression + pp.ZeroOrMore(comma.suppress() + full_expression)) functions = label + lp + functions_arguments + rp functions.setParseAction(PushFunction) literals = number | string | label literals.setParseAction(PushSingleToken) # Any expression that can be modified by an unary operator. # We include strings (even though they can't be modified by any unary # operator) since atoms do not necessitate modification by unary operators. # These errors will be caught by the interpreter. atom = ((lp + full_expression + rp) | aggregate_functions | functions | literals) unary_operators = (pp.CaselessLiteral('+') | pp.CaselessLiteral('-') | pp.CaselessLiteral('~') | pp.CaselessKeyword('not')) # Take all unary operators preceding atom (possibly many). current_expression = (pp.ZeroOrMore(unary_operators) + atom.suppress()) current_expression.setParseAction(PushUnaryOperators) # All operators in same set have same precedence. Precedence is top to bottom. binary_operators = [ (pp.CaselessLiteral('*') | pp.CaselessLiteral('/') | pp.CaselessLiteral('%')), pp.CaselessLiteral('+') | pp.CaselessLiteral('-'), pp.CaselessLiteral('>>') | pp.CaselessLiteral('<<'), (pp.CaselessLiteral('<=') | pp.CaselessLiteral('>=') | pp.CaselessLiteral('<') | pp.CaselessLiteral('>')), (pp.CaselessLiteral('==') | pp.CaselessLiteral('=') | pp.CaselessLiteral('!=')), pp.CaselessKeyword('is') | pp.CaselessKeyword('contains'), pp.CaselessLiteral('&'), pp.CaselessLiteral('^'), pp.CaselessLiteral('|'), pp.CaselessKeyword('and'), pp.CaselessKeyword('or'), ] # Take the operator set of the most precedence that has not been parsed. # Find and collapse all operators of the set. Thus, order of operations # is not broken. Equivalent to recursive descent parsing. # Below code is equivalent to: # expression = expression + pp.ZeroOrMore(op_level1 + expression) # expression = expression + pp.ZeroOrMore(op_level2 + expression) # ... for operator_set in binary_operators: # Represents _i-1 ai part of expression that is added to current expression. operator_expression = operator_set + current_expression # Push only the operator, both atoms will have already been pushed. operator_expression.setParseAction(PushBinaryOperator) # pylint: disable=g-no-augmented-assignment current_expression = (current_expression + pp.ZeroOrMore(operator_expression)) # pylint: disable=pointless-statement full_expression << current_expression return full_expression
import taxcalc from taxcalc import Policy PYTHON_MAJOR_VERSION = sys.version_info.major INT_TO_NTH_MAP = [ 'first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'nineth', 'tenth' ] SPECIAL_INFLATABLE_PARAMS = {'_II_credit', '_II_credit_ps'} SPECIAL_NON_INFLATABLE_PARAMS = { '_ACTC_ChildNum', '_EITC_MinEligAge', '_EITC_MaxEligAge' } # Grammar for Field inputs TRUE = pp.CaselessKeyword('true') FALSE = pp.CaselessKeyword('false') WILDCARD = pp.Word('*') INT_LIT = pp.Word(pp.nums) NEG_DASH = pp.Word('-', exact=1) FLOAT_LIT = pp.Word(pp.nums + '.') DEC_POINT = pp.Word('.', exact=1) FLOAT_LIT_FULL = pp.Word(pp.nums + '.' + pp.nums) COMMON = pp.Word(",", exact=1) REVERSE = pp.Word("<") + COMMON VALUE = WILDCARD | NEG_DASH | FLOAT_LIT_FULL | FLOAT_LIT | INT_LIT MORE_VALUES = COMMON + VALUE BOOL = WILDCARD | TRUE | FALSE MORE_BOOLS = COMMON + BOOL