def value(value): value = m.any(m.sequence(svtok('('), value, svtok(')')), case_expression, simplified_case_expression, cast_expression, count_all, call_analytics_function, call_set_function, special_calls, call, null, integer, string, bool_, name, float_) value = m.any( m.construct( a.Cast, m.keyword(value=value), svtok('::'), m.keyword(type=value), ), value, ) value = unary_op(value, '+', '-') value = binary_op(value, '^') value = binary_op(value, '*', '/', '%') value = binary_op(value, '||') value = binary_op(value, '+', '-', '&', '|') value = binary_op(value, '#', '<<', '>>') value = unary_op(value, '~') value = binary_op(value, '=', '!=', '>', '<', '>=', '<=', '<>', '!>', '!<') value = unary_op(value, 'not') value = binary_op(value, 'and') value = m.transform( build_binary_tree, m.list_of( m.any( compound_token('not', 'like'), compound_token('not', 'in'), verbatim_token('in', 'or', 'like'), ), value)) return value
def test__record(): assert _apply(m.wildcard, a.Integer('42')) == ([a.Integer('42')], []) assert _apply(m.record(a.Integer, value=m.wildcard), a.Integer('42')) == (['42'], []) assert _apply(m.eq('42'), '42') == (['42'], []) assert _apply(m.eq('42'), '43') == (None, ['43']) matcher = m.any( m.record(a.Integer, value=m.eq('42')), m.record(a.Name, name=m.eq('foo')), ) assert _apply(matcher, a.Name('foo')) == (['foo'], []) assert _apply(matcher, a.Integer('42')) == (['42'], [])
def unary_op(value, *ops): return m.any( m.construct(a.UnaryOp, m.keyword(op=verbatim_token(*ops)), m.keyword(arg=value)), value, )
'!>', '!<', } null = m.construct(a.Null, svtok('null')) integer = m.construct(a.Integer, m.keyword(value=regex_token(integer_format))) float_ = m.construct(a.Float, m.keyword(value=regex_token(float_format))) bool_ = m.construct(a.Bool, m.keyword(value=verbatim_token('true', 'false'))) string = m.construct(a.String, m.keyword(value=base_string())) base_name = m.any( m.pred(lambda v: v not in keywords and re.match(name_format, v)), base_string('"'), ) name = m.transform( lambda *parts: [a.Name('.'.join(*parts))], m.sequence( m.optional(m.sequence(base_name, svtok('.'))), m.optional(m.sequence(base_name, svtok('.'))), base_name, )) @m.define def value(value): value = m.any(m.sequence(svtok('('), value, svtok(')')), case_expression, simplified_case_expression, cast_expression, count_all,