def get_parsing_expr(klass): if klass.get_arity() == 2: raise Exception(klass) S = Suppress L = Literal inside = simple_sts_type | (S('(') - sts_type - S(')')) glyphs = klass.get_glyphs() glyph = S(MyOr(map(L, glyphs))) arity = klass.get_arity() if arity == 2: expr = (inside + glyph + inside) elif arity == ParseableWithOperators.TWO_OR_MORE: expr = (inside + OneOrMore(glyph + inside)) else: raise NotImplementedError(arity) def my_parse_action(s, loc, tokens): # @UnusedVariable if arity == 2: a = tokens[0] b = tokens[1] return klass(a, b) elif arity == ParseableWithOperators.TWO_OR_MORE: return klass(*tuple(tokens)) else: raise NotImplementedError() expr.addParseAction(wrap_parse_action(my_parse_action)) expr.setName(klass.__name__) return False, expr
def get_parsing_expr(): S = Suppress O = Optional identifier_expression = Combine(oneOf(list(alphas)) + O(Word('_' + alphanums))) expr = S(Literal('$')) - identifier_expression expr = expr ^ oneOf(list(alphas)) expr.setName('variable') def my_parse_action(s, loc, tokens): # @UnusedVariable name = tokens[0] return Variable(name) expr.addParseAction(wrap_parse_action(my_parse_action)) return True, expr
def get_parsing_expr(): S = Suppress inside = (S('(') - sts_type - S(')')) | sts_type elements = S(Literal('{')) + inside + ZeroOrMore(S(',') + inside) \ + S(Literal('}')) elements = Group(elements)('elements') expr = elements expr.setName('Finite set') def parse_action(s, loc, tokens): # @UnusedVariable values = list(tokens[0]) return FiniteSet(values) expr.setParseAction(wrap_parse_action(parse_action)) return True, expr
def get_sts_type(): global sts_type_final if sts_type_final is not None: # print('reusing') return sts_type_final else: simple_exprs = [] complex_exprs = [] operators = [] S = Suppress L = Literal for cls in HasComponents.klasses.values(): from typsy.parseables import Parseable if not issubclass(cls, Parseable): # print('Warn, %r is not Parseable' % cls) continue from typsy.parseables import ParseableWithOperators if issubclass(cls, ParseableWithOperators) and cls.get_arity() in [2]: glyphs = cls.get_glyphs() glyph = MyOr(map(L, glyphs)) spec = (glyph, cls.get_arity(), opAssoc.LEFT, wrap_parse_action(cls.op_system_parse_action)) operators.append(spec) else: pexpr = cls.get_parsing_expr() if pexpr is not None: if not isinstance(pexpr, tuple): msg = 'Could not %r' % cls raise Exception(msg) simple, expr = pexpr try: str(expr) # print cls, simple,str(expr) pass except: msg = 'invalid name for %r' % cls raise ValueError(msg) if simple: simple_exprs.append(expr) else: complex_exprs.append(expr) # print('simple: %r' % simple_exprs) # print('complex: %r' % complex_exprs) # print('operators: %r' % operators) operand = (S('(') + sts_type + S(')')) | MyOr(complex_exprs + simple_exprs) ops = myOperatorPrecedence(operand, operators) sts_type << ops simple_sts_type << MyOr(simple_exprs) sts_type_final = sts_type sts_type_final.setName('sts_type_final') return sts_type_final