Esempio n. 1
0
def CollectAll(operand, Union=None, reduce=None):
    '''Collect all subterms for which operand succeeds.

	collect all subterms with user-defined union operator and
	a skip argument.

	@param Union: transformation factory which takes two lists and produces a
	single one and produce a single one.
	If duplicates must be removed, then this argument should be union,
	otherwise it defaults to concat.

	@param reduce: it can be used to reduce the current term before
	collecting subterms of it. Producing the empty list will result
	in a complete skip of all subterms.
	'''
    if Union is None:
        from transf.lib.lists import Concat as Union
    collect = util.Proxy()
    crush = Crush(build.nil, Union, collect)
    if reduce is not None:
        crush = +reduce * crush
    collect.subject = build.Cons(operand, crush) + crush
    #else:
    #collect.subject = build.Cons(operand, crush) + reduce * collect + crush
    #collect.subject = build.Cons(operand, crush) + +reduce * crush
    return collect
Esempio n. 2
0
def SplitAllAfter(operand, ):
    splitall = util.Proxy()
    splitall.subject = (combine.GuardedChoice(
        SplitAfter(operand), congruent.Cons(base.ident,
                                            project.head * splitall),
        build.List((base.ident, ))))
    return splitall
Esempio n. 3
0
def AllTD(operand):
    '''Apply a transformation to all subterms, but stops recursing
	as soon as it finds a subterm to which the transformation succeeds.
	'''
    alltd = util.Proxy()
    alltd.subject = combine.Choice(operand, All(alltd))
    return alltd
Esempio n. 4
0
File: iterate.py Progetto: uxmal/idc
def Rec(Def):
    '''Recursive transformation.

	@param Def: transformation factory whose single argument is its own definition.
	'''
    rec = util.Proxy()
    rec.subject = Def(rec)
    return rec
Esempio n. 5
0
def Foldr(tail, Cons, operand=None):
    if operand is None:
        operand = base.ident
    foldr = util.Proxy()
    foldr.subject = combine.GuardedChoice(
        match.nil, tail,
        Cons(combine.Composition(project.head, operand),
             combine.Composition(project.tail, foldr)))
    return foldr
Esempio n. 6
0
def One(operand):
    '''Applies a transformation to exactly one direct subterm of a term.'''
    one = util.Proxy()
    one.subject = congruent.Subterms(
        # FIXME: write a non-recursive implementation
        combine.Choice(congruent.Cons(operand, base.ident),
                       congruent.Cons(base.ident, one)),
        base.fail)
    return one
Esempio n. 7
0
def DownUp(down=None, up=None, stop=None):
    downup = util.Proxy()
    downup.subject = All(downup)
    if stop is not None:
        downup.subject = combine.Choice(stop, downup.subject)
    if up is not None:
        downup.subject = combine.Composition(downup.subject, up)
    if down is not None:
        downup.subject = combine.Composition(down, downup.subject)
    return downup
Esempio n. 8
0
def Some(operand):
    '''Applies a transformation to as many direct subterms of a term, but at list one.'''
    some = util.Proxy()
    some.subject = congruent.Subterms(
        # FIXME: write a non-recursive implementation
        combine.Choice(
            congruent.Cons(operand, lists.Map(combine.Try(operand))),
            congruent.Cons(base.ident, some),
        ),
        base.fail)
    return some
Esempio n. 9
0
def Traverse(Subterms, down=None, up=None, stop=None, Enter=None, Leave=None):
    '''Generic traversal.'''
    traverse = util.Proxy()
    traverse.subject = Subterms(traverse)
    if Leave is not None:
        traverse.subject = Leave(traverse.subject)
    if stop is not None:
        traverse.subject = combine.Choice(stop, traverse.subject)
    if up is not None:
        traverse.subject = combine.Composition(traverse.subject, up)
    if down is not None:
        traverse.subject = combine.Composition(down, traverse.subject)
    if Enter is not None:
        traverse.subject = Enter(traverse.subject)
    return traverse
Esempio n. 10
0
def OneStmt(pre, post=trf.base.ident):
    stmt = util.Proxy()
    stmts = trf.lists.One(stmt)
    stmt.subject = pre**post**Stmt(stmt, stmts, trf.base.fail)
    return stmt
Esempio n. 11
0
def AllStmts(**kargs):
    stmt = util.Proxy()
    stmts = trf.lists.Map(stmt)
    stmt.subject = Stmt(stmt, stmts, trf.base.ident, **kargs)
    return stmt
Esempio n. 12
0
def CountAll(operand):
    '''Count the number of occorrences in all subterms.'''
    count = util.Proxy()
    count.subject = arith.AddInt(_CountOne(operand),
                                 Crush(build.zero, arith.AddInt, count))
Esempio n. 13
0
def AtSuffixR(operand):
    atsuffix = util.Proxy()
    atsuffix.subject = combine.Choice(congruent.Cons(base.ident, atsuffix),
                                      operand)
    return atsuffix
Esempio n. 14
0
def AllBU(operand):
    allbu = util.Proxy()
    allbu.subject = combine.Choice(All(allbu), operand)
    return allbu
Esempio n. 15
0
def InnerMost(operand):
    innermost = util.Proxy()
    innermost.subject = BottomUp(
        combine.Try(combine.Composition(operand, innermost)))
    return innermost