Skip to content

Commit cb1a008

Browse files
author
bludau
committed
Working tree format to be used with gumtree
1 parent 2beb879 commit cb1a008

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# pythonparser
22

33
Pythonparser is a python tool that produces a GumTree compatible Python AST. It uses under the hood asttokens and jsontree.
4+
This version has two scripts available. One for py2 and one for py3
45

56
## Installation
67

78
Just clone the repository and `pip install -r requirements.txt`
9+
10+
## Usage
11+
On Windows:
12+
* Add PYTHONPATH envvar to pip install directory
13+
* Change the PythonTreeGenerator.java to use python command instead of PATH
14+

pythonparser renamed to pythonparser.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,18 @@ def json2xml(tree):
159159
lines = []
160160
def convert_node(i, indent_level=0):
161161
node = tree[i]
162-
line = "\t" * indent_level + "<{}".format(node['type'])
163-
for key in ['value', 'lineno', 'col', 'end_line_no', 'end_col']:
162+
line = "\t" * indent_level + "<tree "
163+
keys = ['type', 'value', 'pos', 'length']
164+
tree_keys = ['type', 'label', 'pos', 'length']
165+
for i, key in enumerate(keys):
164166
if key in node:
165-
line += (' {}={}'.format(key, quoteattr(str(node[key]))))
167+
line += (' {}={}'.format(tree_keys[i], quoteattr(str(node[key]))))
166168
line += ">"
167169
lines.append(line)
168170
if "children" in node:
169171
for child in node["children"]:
170172
convert_node(int(child), indent_level + 1)
171-
lines.append("\t" * indent_level + "</" + node["type"] + ">")
173+
lines.append("\t" * indent_level + "</tree>")
172174
return lines
173175

174176
return "\n".join(convert_node(0))

pythonparser3 renamed to pythonparser3.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,18 @@ def json2xml(tree):
166166
lines = []
167167
def convert_node(i, indent_level=0):
168168
node = tree[i]
169-
line = "\t" * indent_level + "<{}".format(node['type'])
170-
for key in ['value', 'lineno', 'col', 'end_line_no', 'end_col']:
169+
line = "\t" * indent_level + "<tree "
170+
keys = ['type', 'value', 'pos', 'length']
171+
tree_keys = ['type', 'label', 'pos', 'length']
172+
for i, key in enumerate(keys):
171173
if key in node:
172-
line += (' {}={}'.format(key, quoteattr(str(node[key]))))
174+
line += (' {}={}'.format(tree_keys[i], quoteattr(str(node[key]))))
173175
line += ">"
174176
lines.append(line)
175177
if "children" in node:
176178
for child in node["children"]:
177179
convert_node(int(child), indent_level + 1)
178-
lines.append("\t" * indent_level + "</" + node["type"] + ">")
180+
lines.append("\t" * indent_level + "</tree>")
179181
return lines
180182

181183
return "\n".join(convert_node(0))

0 commit comments

Comments
 (0)