-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathc_sharp_parser.py
executable file
·166 lines (142 loc) · 5.91 KB
/
c_sharp_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from typing import List, Dict, Any
import tree_sitter
import logging
from .language_parser import LanguageParser, get_node_by_kind, get_node_text
logger = logging.getLogger(name=__name__)
class CsharpParser(LanguageParser):
BLACKLISTED_FUNCTION_NAMES = []
@staticmethod
def get_docstring(node, blob=None):
"""
Get docstring description for node
Args:
node (tree_sitter.Node)
blob (str): original source code which parse the `node`
Returns:
str: docstring
"""
if blob:
logger.info('From version `0.0.6` this function will update argument in the API')
docstring_node = CsharpParser.get_docstring_node(node)
docstring = '\n'.join(get_node_text(s) for s in docstring_node)
return docstring
@staticmethod
def get_docstring_node(node):
"""
Get docstring node from it parent node.
C# docstring is written line by line and stay outside it own node, see example below.
Args:
node (tree_sitter.Node): parent node (usually function node) to get its docstring
Return:
List: list of docstring nodes
Example:
str = '''
// <summary>
// Docstring of a method
// </summary>
// <param name="animal_honk">Argument.</param>
// <returns>
// None.
public void honk(string animal_honk)
{
Console.WriteLine(animal_honk);
Console.WriteLine("Tuut, tuut!");
}
'''
...
print(C_sharp.get_docstring_node(function_node))
>>> [<Node type=comment, start_point=(5, 12), end_point=(5, 24)>, \
<Node type=comment, start_point=(6, 12), end_point=(6, 36)>, \
<Node type=comment, start_point=(7, 12), end_point=(7, 25)>, \
<Node type=comment, start_point=(8, 12), end_point=(8, 58)>, \
<Node type=comment, start_point=(9, 12), end_point=(9, 24)>, \
<Node type=comment, start_point=(10, 12), end_point=(10, 20)>]
"""
docstring_node = []
prev_node = node.prev_sibling
if prev_node and prev_node.type == 'comment':
docstring_node.append(prev_node)
prev_node = prev_node.prev_sibling
while prev_node and prev_node.type == 'comment':
# Assume the comment is dense
x_current = prev_node.start_point[0]
x_next = prev_node.next_sibling.start_point[0]
if x_next - x_current > 1:
break
docstring_node.insert(0, prev_node)
prev_node = prev_node.prev_sibling
return docstring_node
@staticmethod
def get_comment_node(node):
"""
Return all comment node inside a parent node
Args:
node (tree_sitter.Node)
Return:
List: list of comment nodes
"""
comment_node = get_node_by_kind(node, kind=['comment'])
return comment_node
@staticmethod
def get_function_list(node):
res = get_node_by_kind(node, ['local_function_statement', 'method_declaration'])
# We don't use "constructor_declaration"
return res
@staticmethod
def get_class_list(node):
res = get_node_by_kind(node, ['class_declaration'])
return res
@staticmethod
def get_function_metadata(function_node, blob: str = None) -> Dict[str, Any]:
"""
Function metadata contains:
- identifier (str): function name
- parameters (Dict[str, str]): parameter's name and their type (e.g: {'param_a': 'int'})
- type (str): type
"""
metadata = {
'identifier': '',
'parameters': {},
'return_type': None
}
assert type(function_node) == tree_sitter.Node
for child in function_node.children:
if child.type in ['predefined_type', 'generic_name']:
metadata['return_type'] = get_node_text(child)
elif child.type == 'identifier':
if child.next_named_sibling.type != 'parameter_list':
metadata['return_type'] = get_node_text(child)
else:
metadata['identifier'] = get_node_text(child)
elif child.type == 'parameter_list':
for param_node in child.children:
param_nodes = get_node_by_kind(param_node, ['parameter'])
for param in param_nodes:
param_type = get_node_text(param.children[0])
param_identifier = get_node_text(param.children[1])
metadata['parameters'][param_identifier] = param_type
return metadata
@staticmethod
def get_class_metadata(class_node, blob: str=None) -> Dict[str, str]:
"""
Class metadata contains:
- identifier (str): class's name
- parameters (List[str]): inheritance class
"""
if blob:
logger.info('From version `0.0.6` this function will update argument in the API')
metadata = {
'identifier': '',
'parameters': '',
}
assert type(class_node) == tree_sitter.Node
for child in class_node.children:
if child.type == 'identifier':
metadata['identifier'] = get_node_text(child)
elif child.type == 'base_list':
argument_list = []
for arg in child.children:
if arg.type == 'identifier':
argument_list.append(get_node_text(arg))
metadata['parameters'] = argument_list
return metadata