broadfield-dev commited on
Commit
217d002
·
verified ·
1 Parent(s): e08abc4

Update parser.py

Browse files
Files changed (1) hide show
  1. parser.py +51 -11
parser.py CHANGED
@@ -13,13 +13,38 @@ def get_category(node):
13
  return 'async_function'
14
  elif isinstance(node, ast.ClassDef):
15
  return 'class'
 
 
 
 
 
 
16
  elif isinstance(node, ast.Expr):
17
  return 'expression'
18
  else:
19
  return 'other'
20
 
21
- def parse_node(node, lines, prev_end, level=0):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  """Recursively parse an AST node and its children, assigning hierarchy levels."""
 
 
 
23
  parts = []
24
  start_line = getattr(node, 'lineno', prev_end + 1)
25
  end_line = getattr(node, 'end_lineno', start_line)
@@ -27,56 +52,71 @@ def parse_node(node, lines, prev_end, level=0):
27
  # Handle spacers before the node
28
  if start_line > prev_end + 1:
29
  spacer_lines = lines[prev_end:start_line - 1]
 
30
  parts.append({
31
  'category': 'spacer',
32
  'source': ''.join(spacer_lines),
33
  'location': (prev_end + 1, start_line - 1),
34
- 'level': level
 
35
  })
36
 
37
  # Capture the node's source
38
  stmt_lines = lines[start_line - 1:end_line]
 
39
  parts.append({
40
  'category': get_category(node),
41
  'source': ''.join(stmt_lines),
42
  'location': (start_line, end_line),
43
- 'level': level
 
44
  })
45
 
46
- # Process nested nodes (e.g., class or function bodies)
47
  if hasattr(node, 'body'):
48
  nested_prev_end = end_line - 1
49
  for child in node.body:
50
- child_parts = parse_node(child, lines, nested_prev_end, level + 1)
51
  parts.extend(child_parts)
52
  nested_prev_end = child_parts[-1]['location'][1]
53
 
 
 
 
 
 
 
 
 
54
  return parts
55
 
56
  def parse_python_code(code):
57
- """Parse Python code string and return parts with hierarchy."""
58
  lines = code.splitlines(keepends=True)
 
59
  try:
60
  tree = ast.parse(code)
61
  except SyntaxError:
62
- return [{'category': 'error', 'source': 'Invalid Python code', 'location': (1, 1), 'level': 0}]
63
 
64
  parts = []
65
  prev_end = 0
66
 
67
  for stmt in tree.body:
68
- stmt_parts = parse_node(stmt, lines, prev_end)
69
  parts.extend(stmt_parts)
70
  prev_end = stmt_parts[-1]['location'][1]
71
 
72
  # Capture trailing spacers
73
- if prev_end < len(lines):
74
  remaining_lines = lines[prev_end:]
 
75
  parts.append({
76
  'category': 'spacer',
77
  'source': ''.join(remaining_lines),
78
- 'location': (prev_end + 1, len(lines) + 1),
79
- 'level': 0
 
80
  })
81
 
82
  return parts
 
13
  return 'async_function'
14
  elif isinstance(node, ast.ClassDef):
15
  return 'class'
16
+ elif isinstance(node, ast.If):
17
+ return 'if'
18
+ elif isinstance(node, ast.While):
19
+ return 'while'
20
+ elif isinstance(node, ast.For):
21
+ return 'for'
22
  elif isinstance(node, ast.Expr):
23
  return 'expression'
24
  else:
25
  return 'other'
26
 
27
+ def create_vector(category, level, location, total_lines):
28
+ """Create a vector representation for a code part."""
29
+ # Vector: [category_id, level, start_line_normalized, end_line_normalized]
30
+ category_map = {
31
+ 'import': 1, 'assignment': 2, 'function': 3, 'async_function': 4, 'class': 5,
32
+ 'if': 6, 'while': 7, 'for': 8, 'expression': 9, 'spacer': 10, 'other': 11
33
+ }
34
+ category_id = category_map.get(category, 0)
35
+ start_line, end_line = location
36
+ return [
37
+ category_id,
38
+ level,
39
+ start_line / total_lines, # Normalized start position
40
+ end_line / total_lines # Normalized end position
41
+ ]
42
+
43
+ def parse_node(node, lines, prev_end, level=0, total_lines=None):
44
  """Recursively parse an AST node and its children, assigning hierarchy levels."""
45
+ if total_lines is None:
46
+ total_lines = len(lines)
47
+
48
  parts = []
49
  start_line = getattr(node, 'lineno', prev_end + 1)
50
  end_line = getattr(node, 'end_lineno', start_line)
 
52
  # Handle spacers before the node
53
  if start_line > prev_end + 1:
54
  spacer_lines = lines[prev_end:start_line - 1]
55
+ spacer_vector = create_vector('spacer', level, (prev_end + 1, start_line - 1), total_lines)
56
  parts.append({
57
  'category': 'spacer',
58
  'source': ''.join(spacer_lines),
59
  'location': (prev_end + 1, start_line - 1),
60
+ 'level': level,
61
+ 'vector': spacer_vector
62
  })
63
 
64
  # Capture the node's source
65
  stmt_lines = lines[start_line - 1:end_line]
66
+ node_vector = create_vector(get_category(node), level, (start_line, end_line), total_lines)
67
  parts.append({
68
  'category': get_category(node),
69
  'source': ''.join(stmt_lines),
70
  'location': (start_line, end_line),
71
+ 'level': level,
72
+ 'vector': node_vector
73
  })
74
 
75
+ # Process nested nodes (e.g., class/function bodies, control structures)
76
  if hasattr(node, 'body'):
77
  nested_prev_end = end_line - 1
78
  for child in node.body:
79
+ child_parts = parse_node(child, lines, nested_prev_end, level + 1, total_lines)
80
  parts.extend(child_parts)
81
  nested_prev_end = child_parts[-1]['location'][1]
82
 
83
+ # Handle additional bodies (e.g., else, elif, orelse for loops)
84
+ if hasattr(node, 'orelse') and node.orelse:
85
+ orelse_prev_end = parts[-1]['location'][1]
86
+ for child in node.orelse:
87
+ child_parts = parse_node(child, lines, orelse_prev_end, level + 1, total_lines)
88
+ parts.extend(child_parts)
89
+ orelse_prev_end = child_parts[-1]['location'][1]
90
+
91
  return parts
92
 
93
  def parse_python_code(code):
94
+ """Parse Python code string and return parts with hierarchy and vectors."""
95
  lines = code.splitlines(keepends=True)
96
+ total_lines = len(lines)
97
  try:
98
  tree = ast.parse(code)
99
  except SyntaxError:
100
+ return [{'category': 'error', 'source': 'Invalid Python code', 'location': (1, 1), 'level': 0, 'vector': [0, 0, 1.0, 1.0]}]
101
 
102
  parts = []
103
  prev_end = 0
104
 
105
  for stmt in tree.body:
106
+ stmt_parts = parse_node(stmt, lines, prev_end, total_lines=total_lines)
107
  parts.extend(stmt_parts)
108
  prev_end = stmt_parts[-1]['location'][1]
109
 
110
  # Capture trailing spacers
111
+ if prev_end < total_lines:
112
  remaining_lines = lines[prev_end:]
113
+ spacer_vector = create_vector('spacer', 0, (prev_end + 1, total_lines + 1), total_lines)
114
  parts.append({
115
  'category': 'spacer',
116
  'source': ''.join(remaining_lines),
117
+ 'location': (prev_end + 1, total_lines + 1),
118
+ 'level': 0,
119
+ 'vector': spacer_vector
120
  })
121
 
122
  return parts