Browse Source

Modify the python parser. Make data more descriptive by adding data

about function calls.
tags/v1.0^2
Benjamin Attal 10 years ago
parent
commit
71dec1d269
1 changed files with 56 additions and 16 deletions
  1. +56
    -16
      bitshift/parser/python.py

+ 56
- 16
bitshift/parser/python.py View File

@@ -58,16 +58,20 @@ class _TreeCutter(ast.NodeVisitor):
if isinstance(t, ast.Tuple):
for n in t.elts:
line, col = n.lineno, n.col_offset
self.accum['functions'][n.id]['start_ln'] = line
self.accum['functions'][n.id]['start_col'] = col
self.accum['functions'][n.id]['end_ln'] = line
self.accum['functions'][n.id]['end_ln'] = col

if not self.accum['vars'].has_key(node.name):
self.accum['vars'][node.name] = {'declaration': {}, 'uses': []}

self.accum['vars'][n.id]['declaration']['start_ln'] = line
self.accum['vars'][n.id]['declaration']['start_col'] = col
self.accum['vars'][n.id]['declaration']['end_ln'] = line
self.accum['vars'][n.id]['declaration']['end_ln'] = col
else:
line, col = t.lineno, t.col_offset
self.accum['functions'][t.id]['start_ln'] = line
self.accum['functions'][t.id]['start_col'] = col
self.accum['functions'][t.id]['end_ln'] = line
self.accum['functions'][t.id]['end_ln'] = col
self.accum['vars'][t.id]['declaration']['start_ln'] = line
self.accum['vars'][t.id]['declaration']['start_col'] = col
self.accum['vars'][t.id]['declaration']['end_ln'] = line
self.accum['vars'][t.id]['declaration']['end_ln'] = col

self.generic_visit(node)

@@ -84,13 +88,45 @@ class _TreeCutter(ast.NodeVisitor):
"""

start_line, start_col, end_line, end_col = self.start_n_end(node)
self.accum['functions'][node.name]['start_ln'] = start_line
self.accum['functions'][node.name]['start_col'] = start_col
self.accum['functions'][node.name]['end_ln'] = end_line
self.accum['functions'][node.name]['end_ln'] = end_col

if not self.accum['functions'].has_key(node.name):
self.accum['functions'][node.name] = {'declaration': {}, 'calls': []}

self.accum['functions'][node.name]['declaration']['start_ln'] = start_line
self.accum['functions'][node.name]['declaration']['start_col'] = start_col
self.accum['functions'][node.name]['declaration']['end_ln'] = end_line
self.accum['functions'][node.name]['declaration']['end_ln'] = end_col

self.generic_visit(node)

def visit_Call(self, node):
"""
Visits Function Call nodes in a tree. Adds relevant data about them
in the functions section for accum.

:param node: The current node.

:type node: ast.Call

.. todo::
Add arguments and decorators metadata to accum.
"""

line, col = node.line_no, node.col_offset

if not self.accum['functions'].has_key(node.name):
self.accum['functions'][node.name] = {'declaration': {}, 'calls': []}

pos = {}
pos['start_line'] = line
pos['start_col'] = col
pos['end_line'] = line
pos['end_col'] = col
self.accum['functions'][node.name]['calls'].append(pos)

self.generic_visit(node)


def visit_ClassDef(self, node):
"""
Visits ClassDef nodes in a tree. Adds relevant data about them to accum.
@@ -104,13 +140,17 @@ class _TreeCutter(ast.NodeVisitor):
"""

start_line, start_col, end_line, end_col = self.start_n_end(node)
self.accum['functions'][node.name]['start_ln'] = start_line
self.accum['functions'][node.name]['start_col'] = start_col
self.accum['functions'][node.name]['end_ln'] = end_line
self.accum['functions'][node.name]['end_ln'] = end_col

self.accum['classes'][node.name]['start_ln'] = start_line
self.accum['classes'][node.name]['start_col'] = start_col
self.accum['classes'][node.name]['end_ln'] = end_line
self.accum['classes'][node.name]['end_ln'] = end_col

self.generic_visit(node)

def visit_Name(self, node):
pass

def parse_py(codelet):
"""
Adds 'symbols' field to the codelet after parsing the python code.


Loading…
Cancel
Save