A semantic search engine for source code https://bitshift.benkurtovic.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

127 lines
3.9 KiB

  1. import ast
  2. class _TreeCutter(ast.NodeVisitor):
  3. """
  4. Local node visitor for python abstract syntax trees.
  5. :ivar accum: (dict) Information on variables, functions, and classes
  6. accumulated from an abstract syntax tree.
  7. :ivar cache: (dict or None) Information stored about parent nodes. Added
  8. to accum when node reaches the lowest possible level.
  9. .. todo::
  10. Add visit funciton for ast.Name to record all uses of a variable.
  11. Use self.cache to store extra information about nodes.
  12. """
  13. def __init__(self):
  14. """
  15. Create a _TreeCutter instance.
  16. """
  17. self.accum = {'vars': {}, 'functions': {}, 'classes': {}}
  18. self.cache = None
  19. def start_n_end(self, node):
  20. """
  21. Helper function to get the start and end lines of an AST node.
  22. :param node: The node.
  23. :type node: ast.FunctionDef or ast.ClassDef or ast.Module
  24. """
  25. start_line, start_col = node.lineno, node.col_offset
  26. temp_node = node
  27. while 'body' in temp_node.__dict__:
  28. temp_node = temp_node.body[-1]
  29. end_line, end_col = temp_node.lineno, temp_node.col_offset
  30. return (start_line, start_col, end_line, end_col)
  31. def visit_Assign(self, node):
  32. """
  33. Visits Assign nodes in a tree. Adds relevant data about them to accum.
  34. :param node: The current node.
  35. :type node: ast.Assign
  36. .. todo::
  37. Add value and type metadata to accum.
  38. """
  39. for t in node.targets:
  40. if isinstance(t, ast.Tuple):
  41. for n in t.elts:
  42. line, col = n.lineno, n.col_offset
  43. self.accum['functions'][n.id]['start_ln'] = line
  44. self.accum['functions'][n.id]['start_col'] = col
  45. self.accum['functions'][n.id]['end_ln'] = line
  46. self.accum['functions'][n.id]['end_ln'] = col
  47. else:
  48. line, col = t.lineno, t.col_offset
  49. self.accum['functions'][t.id]['start_ln'] = line
  50. self.accum['functions'][t.id]['start_col'] = col
  51. self.accum['functions'][t.id]['end_ln'] = line
  52. self.accum['functions'][t.id]['end_ln'] = col
  53. self.generic_visit(node)
  54. def visit_FunctionDef(self, node):
  55. """
  56. Visits FunctionDef nodes in a tree. Adds relevant data about them to accum.
  57. :param node: The current node.
  58. :type node: ast.FunctionDef
  59. .. todo::
  60. Add arguments and decorators metadata to accum.
  61. """
  62. start_line, start_col, end_line, end_col = self.start_n_end(node)
  63. self.accum['functions'][node.name]['start_ln'] = start_line
  64. self.accum['functions'][node.name]['start_col'] = start_col
  65. self.accum['functions'][node.name]['end_ln'] = end_line
  66. self.accum['functions'][node.name]['end_ln'] = end_col
  67. self.generic_visit(node)
  68. def visit_ClassDef(self, node):
  69. """
  70. Visits ClassDef nodes in a tree. Adds relevant data about them to accum.
  71. :param node: The current node.
  72. :type node: ast.ClassDef
  73. .. todo::
  74. Add arguments, inherits, and decorators metadata to accum.
  75. """
  76. start_line, start_col, end_line, end_col = self.start_n_end(node)
  77. self.accum['functions'][node.name]['start_ln'] = start_line
  78. self.accum['functions'][node.name]['start_col'] = start_col
  79. self.accum['functions'][node.name]['end_ln'] = end_line
  80. self.accum['functions'][node.name]['end_ln'] = end_col
  81. self.generic_visit(node)
  82. def parse_py(codelet):
  83. """
  84. Adds 'symbols' field to the codelet after parsing the python code.
  85. :param codelet: The codelet object to parsed.
  86. :type code: Codelet
  87. """
  88. tree = ast.parse(codelet.code)
  89. cutter = _TreeCutter()
  90. cutter.visit(tree)
  91. codelet.symbols = cutter.accum