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

Module csttools

Classes [hide private]
  GrammarConformanceWarning
  cstnode
  add_info
  Chain
Functions [hide private]
 
smallest_node(node)
 
is_token(nid)
 
remove_from(A, Z)
 
is_simple(node)
check for the structure [a,[b,[c,[...[k,[symbol.atom,...]]...] This is called a "simple node"
 
is_supersimple(node)
checks for the structure [a,[b,[c,[...[k,[tid, ...]]...] where tid is a token nid.
 
is_atomic(node)
 
projection(*args, **kwargs)
The projection function takes a parse tree of an arbitrary langlet and maps it onto a a Python parse tree.
 
lift(node, LANGLET_OFFSET)
Adds LANGLET_OFFSET to each node id N where n<LANGLET_OFFSET.
 
get_node_name(langlet, nid, type='parse')
 
maybe_projection(node)
This is a variant of the projection() function.
 
func_name(node)
 
proj_nid(node)
 
has_type(node, nid)
 
node_cmp(tree, node_id)
Compares first node of cst tree with node_id.
 
check_node(node, target_langlet, strict=True)
Recursive validity check of a CST node against a grammar.
 
to_text(node_id)
Returns textual representation of a node id regardless of the langlet.
 
prepare_source(source)
Formats an indented source string by stripping away "leading" whitespaces.
 
present_node(node, mark=[], stop=False, indent=0, short_form=False)
 
pprint(node, mark=[], stop=True, indent=0, short_form=False)
 
rule_error(langlet, cst, expected_nids, keywords=set([]), parts=10, type='parse')
 
node_replace(old, new)
inplace replacement of old node by new new node.
 
clone_node(node, with_marker=True)
Creates an identical copy of node.
 
left_shift(node)
inplace replacement of [k,[l,...]] by [l,...]
 
replace_in(context, nid, in_nid, node)
Replace all nodes N within i{context} where nid(N) = i{in_nid} by i{node} when a node M with nid(M) = i{nid} can be found in N.
 
make_node(item)
 
unparen(node)
Unparenthesizes all subnodes of i{node} that are already atomic.
 
new_name(name)
Creates a new identifier from a string by appending a random number.
 
left_distribute(a_atom, a_test, func=None)
Suppose a_test is a predicate of the form `A == X or B > Y`.
 
varargs2arglist(varargs)
This function is used to turn the arguments of a function defintion into that of a function call.
 
to_signature(varargs)
Creates a dictionary from a varargs node.
 
concat_funcalls(funA, funB)
Two function calls funA(argsA), funB(argsB) are merged to one call funA(args).funB(argsB).
 
parens(node)
 
atomize(node, enforce=False)
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)
 
normalize(node)
Sometimes a function f is defined using a simple_stmt node wrapping its SUITE.
 
pushstmt(stmt1, stmt2)
If stmt1 has following structure :
 
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.
 
find_all_of(tree, node_ids=[], level=10000, exclude=[])
generator that finds all nodes with a certain node_id.
 
find_all_of_gen(tree, node_ids=[], level=10000, exclude=[])
generator that finds all nodes with a certain node_id.
 
find_node_chain(tree, nid, level=10000, exclude=[], chain=[])
Find node and returns as node chain.
 
find_all_chains_gen(tree, node_id, level=10000, exclude=[], chain=[])
generator that finds all nodes with a certain node_id.
 
find_all_chains(tree, node_id, level=10000, exclude=[], chain=[])
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.
 
get_node(node, nid)
 
get_one_of(node, node_ids)
 
get_all(node, nid)
 
get_all_of(node, node_ids)
 
is_node(node, nid)
 
power_merge(nodeA, nodeB)
This function merges a pair of power nodes in the following way:
 
wrap_arg(arg)
 
any_node(arg, node_id)
 
find_node(tree, node_id, level=10000, exclude=[])
finds one node with a certain node_id.
Function Details [hide private]

is_supersimple(node)

 

checks for the structure [a,[b,[c,[...[k,[tid, ...]]...] where tid is a token nid. The node is basically a wrapper of a single token.

projection(*args, **kwargs)

 

The projection function takes a parse tree of an arbitrary langlet 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

Decorators:
  • @EasyExtend.util.psyco_optimized

lift(node, LANGLET_OFFSET)

 

Adds LANGLET_OFFSET to each node id N where n<LANGLET_OFFSET.

Parameters:
  • node - node to be lifted.
  • LANGLET_OFFSET - amount to be lifted.

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.

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

check_node(node, target_langlet, strict=True)

 

Recursive validity check of a CST node against a grammar.

Parameters:
  • node - node to check.
  • target_langlet - langlet to check against.
  • strict - the modifier strict is used as follows True -> node id's must match those of the grammar precisely False -> it suffices that projected node id's match those of the Python grammar

prepare_source(source)

 

Formats an indented source string by stripping away "leading" whitespaces. A whitespace block is considered as "leading" if has the size of the whitespace preceding the first non-whitespace token. So it actually dedents the source.

Parameters:
  • source - source string

node_replace(old, new)

 

inplace replacement of old node by new new node.

Parameters:
  • old - node to be replaced
  • new - node replacement

replace_in(context, nid, in_nid, node)

 

Replace all nodes N within i{context} where nid(N) = i{in_nid} by i{node} when a node M with nid(M) = i{nid} can be found in N.

Parameters:
  • context - contextual cst node
  • nid - node id that constraints the node to be replaced.
  • in_nid - node id of the target node of replacement.
  • node - substitution.

unparen(node)

 

Unparenthesizes all subnodes of i{node} that are already atomic.

Examples:

    (8)   -> 8
    ((a)) -> a
    (a+b) -> (a+b)  -!-  a+b is not atomic

left_distribute(a_atom, a_test, func=None)

 

Suppose a_test is a predicate of the form `A == X or B > Y`. Then we map a_atom against the boolean expressions s.t. we yield `a_atom.A == X or a_atom.B > Y`.

If func is available we distribute as `func(a_atom, A) == X or func(a_atom, B) > Y`.

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.

to_signature(varargs)

 

Creates a dictionary from a varargs node.

Parameters:
  • varargs - node of type varargslist.
Returns:
dict of following structure: {'args': dict, 'defaults': dict, 'star_args': dict, 'dstar_args': dict}

atomize(node, enforce=False)

 

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.

Parameters:
  • enforce - wrapped even if node is already atomic. This causes an atom x being transformed int (x).

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

pushstmt(stmt1, stmt2)

 

If stmt1 has following structure :

   EXPR1:
       STMT11
       ...
       STMT1k
       EXPR2:
           STMT21
           ...
           STMT2m

then we insert the second argument stmt2 at the end :

   EXPR1:
       STMT11
       ...
       STMT1k
       EXPR2:
           STMT21
           ...
           STMT2m
 -->       stmt2

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   /