Package EasyExtend :: Module csttools
[hide private]
[frames] | no frames]

Module csttools



Classes [hide private]
  sealed
Functions [hide private]
 
projection(node)
The projection function takes a parse tree of an arbitrary fiber and maps it onto a a Python parse tree.
 
lift(node, FIBER_OFFSET)
 
maybe_projection(node)
This is a variant of the projection() function.
 
maybe_lift(node, FIBER_OFFSET)
This is a variant of the projection() function.
 
proj_nid(node)
 
node_cmp(tree, node_id)
Compares first node of cst tree with node_id.
 
to_text(node_id)
 
present_node(node, mark=[], stop=False, indent=0, short_form=False)
 
pprint(node, mark=[], stop=True, indent=0, short_form=False)
 
node_replace(old, new)
 
left_shift(node)
 
replace_in(context, nid, in_nid, by)
 
prepare_source(source)
 
make_node(item)
 
is_stmt(node)
 
test_name(name)
 
is_simple(node)
check for the structure [a,[b,[c,[...[symbol.atom,...]...] This is called a "simple node"
 
new_name(name)
 
del_name(name)
 
varargs2arglist(varargs)
This function is used to turn the arguments of a function defintion into that of a function call.
 
to_signature(varargs)
 
priority_split(nodelist, operators, head=None)
split a node list of the kind [Arg0, op0, Arg1, op1, ..., opN, ArgN] into a list of sublists that reflects the binding behaviour of the operators.
 
concat_funcalls(funA, funB)
Two function calls funA(argsA), funB(argsB) are merged to one call funA(args).funB(argsB).
 
atomize(node)
A lot of substitutions involve either a test or an expr node as a target.
 
split_file_input(tree)
splits file_input node containing many stmts into several file_input nodes containing one stmt
 
split_expr(node)
splits an expr of the kind a.b(x).c().
 
exprlist2testlist(_exprlist)
 
add_to_suite(_suite, _stmt, pos=-1)
 
func_name(node)
 
normalize(node)
Sometimes a function f is defined using a simple_stmt node wrapping its SUITE.
 
remove_node(tree, node, level=10000, exclude=[])
 
find_one_of(tree, node_ids, level=10000, exclude=[])
Generalization of find_node.
 
find_all(tree, node_id, level=10000, exclude=[])
generator that finds all nodes with a certain node_id.
 
find_all_gen(tree, node_id, level=10000, exclude=[])
generator that finds all nodes with a certain node_id.
 
count_node(tree, node_id, level=10000, exclude=[])
generator that finds all nodes with a certain node_id.
 
find_all_of(tree, node_ids=[], level=10000, exclude=[])
Generalizes find_all such that all nodes are yielded that have a node id listed in node_ids.
 
power_merge(nodeA, nodeB)
This function merges a pair of power nodes in the following way:
 
any_node(arg, node_id)
 
find_node(tree, node_id, level=10000, exclude=[])
finds one node with a certain node_id.
 
any_stmt(arg)
Returns stmt node whenever the input is one of the following nodes:
 
any_test(arg)
Returns test node whenever the input is one of the following nodes:
 
any_expr(arg)
Variables [hide private]
  hierarchy = {256: 0, 257: 0, 258: 0, 261: 1, 262: 5, 263: 6, 2...
Function Details [hide private]

projection(node)

 

The projection function takes a parse tree of an arbitrary fiber and maps it onto a
a Python parse tree. This is done by shifting the node ids of each node to the left.
Since each node id can be described by
    nid = n + k*512,  with n<512
the node id of the Python node is just the rest of division by 512: n = nid%512

maybe_projection(node)

 
This is a variant of the projection() function. It projects on a Python cst only when the first node can be projected.

maybe_lift(node, FIBER_OFFSET)

 
This is a variant of the projection() function. It projects on a Python cst only when the first node can be projected.

node_cmp(tree, node_id)

 
Compares first node of cst tree with node_id.
Parameters:
  • tree - CST
  • node_id - integer representing a py_symbol not a py_token
Returns:
  • 0 if node_id is tree-root.
  • -1 if tree is a py_token or node_id cannot be the node_id of any subtree.
  • 1 otherwise

varargs2arglist(varargs)

 

This function is used to turn the arguments of a function defintion into that of a function
call.

Rationale:
    Let def f(x,y,*args):
            ...
    be a function definion. We might want to define a function def g(*args,**kwd): ...  that
    shall be called with the arguments of f in the body of f:

        def f(x,y,*args):
            ...
            g(x,y,*args)
            ...
    To call g with the correct arguments of f we need to transform the varargslist node according to
    f into the arglist of g.

priority_split(nodelist, operators, head=None)

 

split a node list of the kind [Arg0, op0, Arg1, op1, ..., opN, ArgN] into a list of sublists
that reflects the binding behaviour of the operators. If for example op0<op1 indicates that op0 has
a lower binding behaviour that op1 than we get:

L = [arg1, op0, arg2, op1, arg3, op0, arg4]

   priority_splitting(L,(op0,op1)) -> [arg1,op0,[arg2,op1,arg3],op0,arg4]

If two operators op1,op2 with the same priority, no split will be done. This would be
expressed by

   priority_splitting(L,(op0,(op1,op2)))

The maximum nesting level is 1. The length of operators is always 2.

The operators must be of the kind token.<operator> e.g. token.PLUS, token.SUB etc.

If the optional parameter head is available one might wrap each non-op argument of the final
list with head:

    priority_splitting(L, (op0,op1), hd) -> [[hd,arg1],op0,[hd,[arg2,op1,arg3]],op0,[hd,arg4]]

atomize(node)

 

A lot of substitutions involve either a test or an expr node as a target. But it might happen that
the node that shall be substituted is a child of an atom and replacing the parental expr node of this
atom substitutes too much i.e. all trailer nodes following the atom.

In this case we apply a trick which we called 'atomization'.
Let n be a node ( e.g. expr, power, test etc.) with n.node_id > atom.node_id than we apply

     atom("(",testlist_gexp(any_test(n)),")")

Syntactically atomization puts nothing but parentheses around the expression. Semantically the expression and
its atomization are identical.

split_expr(node)

 
splits an expr of the kind a.b(x).c(). ... into factors a, b, (x), c, (), ...

normalize(node)

 

Sometimes a function f is defined using a simple_stmt node wrapping its SUITE.

   def f(x):pass

Trying to insert a stmt node into f's SUITE will cause a wrong statement:

   def f(x):stmt
   pass

To prevent this we turn the simple_stmt node based SUITE of the original f into a
stmt node based one which yields the correct result:

   def f(x):
       stmt
       pass

find_one_of(tree, node_ids, level=10000, exclude=[])

 
Generalization of find_node. Instead of one node_id a list of several node ids can be passed. The first match will be returned.

power_merge(nodeA, nodeB)

 
This function merges a pair of power nodes in the following way:
     nodeA = atomA + trailerA   \
                                | =>   atomB + trailerB + trailerA
     nodeB = atomB + trailerB   /

any_stmt(arg)

 
Returns stmt node whenever the input is one of the following nodes:
  • stmt
  • simple_stmt
  • compound_stmt
  • small_stmt
  • if_stmt
  • for_stmt
  • while_stmt
  • try_stmt
  • break_stmt
  • continue_stmt
  • return_stmt
  • raise_stmt
  • yield_stmt

any_test(arg)

 
Returns test node whenever the input is one of the following nodes:
  • test
  • and_test
  • lambdef
  • not_test
  • comparison
  • expr
  • xor_expr
  • and_expr
  • shift_expr
  • arith_expr
  • term
  • factor
  • power
  • atom
  • NAME
  • STRING
  • NUMBER

Variables Details [hide private]

hierarchy

Value:
{256: 0,
 257: 0,
 258: 0,
 261: 1,
 262: 5,
 263: 6,
 264: 7,
 266: 1,
...