def balanced_braces(gen): print u'balanced braces start' count = 0 lexemes = [] while True: lexemes.append(gen.parse(alt(any, EOF))) tokid = lexemes[-1].get_token().tokid if tokid == Token.LBRACK: count += 1 print u'incr count to: %d' % count elif tokid == Token.RBRACK: if count <= 0: gen.fail(u'balanced braces') count -= 1 print u'decr count to: %d' % count elif count == 0 and tokid in [Token.NL, Token.EOF]: print u'balanced braces end' return Box.List(lexemes)
def pattern(gen): return gen.parse(alt(var_pattern, tag_pattern, named_pattern, paren_pattern))
def expr(gen): box = gen.parse(alt(definition, chain).many()) elements = [a.get_ast() for a in box.get_list()] return ASTBox(Sequence(elements))
def atom(gen): return gen.parse(alt(var, number, paren, lam, tag, autovar))
def module_item(gen): return gen.parse(alt(definition, unparsed_annotation))
out[i+1] = rest[i].get_ast() return ASTBox(Chain(out)) @generate def definition(gen): box = gen.parse(assign).get_list() name = box[0].get_token().value args = [a.get_ast() for a in box[1].get_list()] body = gen.parse(chain).get_ast() return ASTBox(Definition(sym(name), args, body)) expr_element = alt(chain, definition) @generate def expr(gen): box = gen.parse(alt(definition, chain).many()) elements = [a.get_ast() for a in box.get_list()] return ASTBox(Sequence(elements)) @generate('pattern') def pattern(gen): return gen.parse(alt(var_pattern, tag_pattern, named_pattern, paren_pattern)) paren_pattern = LPAREN.then(pattern).skip(RPAREN) var_pattern = NAME.map(lambda s: ASTBox(VarPat(sym(s.get_token().value))))