Skip to content

Commit 5cfa947

Browse files
committed
Update parser 'return_type'; Update ver0.0.6
1 parent 55646d0 commit 5cfa947

21 files changed

+283
-144
lines changed

src/codetext/parser/c_sharp_parser.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class CsharpParser(LanguageParser):
1212
BLACKLISTED_FUNCTION_NAMES = []
1313

1414
@staticmethod
15-
def get_docstring(node, blob):
15+
def get_docstring(node, blob=None):
1616
"""
1717
Get docstring description for node
1818
@@ -22,7 +22,8 @@ def get_docstring(node, blob):
2222
Returns:
2323
str: docstring
2424
"""
25-
logger.info('From version `0.0.6` this function will update argument in the API')
25+
if blob:
26+
logger.info('From version `0.0.6` this function will update argument in the API')
2627
docstring_node = CsharpParser.get_docstring_node(node)
2728
docstring = '\n'.join(get_node_text(s) for s in docstring_node)
2829
return docstring
@@ -104,14 +105,15 @@ def get_class_list(node):
104105
return res
105106

106107
@staticmethod
107-
def get_function_metadata(function_node, blob: str) -> Dict[str, Any]:
108+
def get_function_metadata(function_node, blob: str=None) -> Dict[str, Any]:
108109
"""
109110
Function metadata contains:
110111
- identifier (str): function name
111112
- parameters (Dict[str, str]): parameter's name and their type (e.g: {'param_a': 'int'})
112113
- type (str): type
113114
"""
114-
logger.info('From version `0.0.6` this function will update argument in the API')
115+
if blob:
116+
logger.info('From version `0.0.6` this function will update argument in the API')
115117
metadata = {
116118
'identifier': '',
117119
'parameters': {},
@@ -121,7 +123,7 @@ def get_function_metadata(function_node, blob: str) -> Dict[str, Any]:
121123

122124
for child in function_node.children:
123125
if child.type == 'predefined_type':
124-
metadata['type'] = get_node_text(child)
126+
metadata['return_type'] = get_node_text(child)
125127
elif child.type == 'identifier':
126128
metadata['identifier'] = get_node_text(child)
127129
elif child.type == 'parameter_list':
@@ -135,13 +137,14 @@ def get_function_metadata(function_node, blob: str) -> Dict[str, Any]:
135137
return metadata
136138

137139
@staticmethod
138-
def get_class_metadata(class_node, blob: str) -> Dict[str, str]:
140+
def get_class_metadata(class_node, blob: str=None) -> Dict[str, str]:
139141
"""
140142
Class metadata contains:
141143
- identifier (str): class's name
142144
- parameters (List[str]): inheritance class
143145
"""
144-
logger.info('From version `0.0.6` this function will update argument in the API')
146+
if blob:
147+
logger.info('From version `0.0.6` this function will update argument in the API')
145148
metadata = {
146149
'identifier': '',
147150
'parameters': '',

src/codetext/parser/cpp_parser.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CppParser(LanguageParser):
1313
BLACKLISTED_FUNCTION_NAMES = ['main', 'constructor']
1414

1515
@staticmethod
16-
def get_docstring(node, blob):
16+
def get_docstring(node, blob=None):
1717
"""
1818
Get docstring description for node
1919
@@ -23,7 +23,8 @@ def get_docstring(node, blob):
2323
Returns:
2424
str: docstring
2525
"""
26-
logger.info('From version `0.0.6` this function will update argument in the API')
26+
if blob:
27+
logger.info('From version `0.0.6` this function will update argument in the API')
2728
docstring_node = CppParser.get_docstring_node(node)
2829
docstring = '\n'.join(get_node_text(s) for s in docstring_node)
2930
return docstring
@@ -99,14 +100,15 @@ def get_comment_node(node):
99100
return comment_node
100101

101102
@staticmethod
102-
def get_function_metadata(function_node, blob: str) -> Dict[str, Any]:
103+
def get_function_metadata(function_node, blob: str=None) -> Dict[str, Any]:
103104
"""
104105
Function metadata contains:
105106
- identifier (str): function name
106107
- parameters (Dict[str, str]): parameter's name and their type (e.g: {'param_a': 'int'})
107108
- return_type (str or NoneType): function's return type
108109
"""
109-
logger.info('From version `0.0.6` this function will update argument in the API')
110+
if blob:
111+
logger.info('From version `0.0.6` this function will update argument in the API')
110112
metadata = {
111113
'identifier': '',
112114
'parameters': {},
@@ -118,29 +120,41 @@ def get_function_metadata(function_node, blob: str) -> Dict[str, Any]:
118120
if child.type in ['primitive_type', 'type_identifier']:
119121
metadata['return_type'] = get_node_text(child)
120122
# search for "function_declarator"
121-
elif child.type == 'function_declarator':
123+
elif child.type == 'pointer_declarator':
124+
for subchild in child.children:
125+
if subchild.type == 'function_declarator':
126+
child = subchild
127+
if child.type == 'function_declarator':
122128
for subchild in child.children:
123129
if subchild.type in ['qualified_identifier', 'identifier']:
124130
metadata['identifier'] = get_node_text(subchild)
125131
elif subchild.type == 'parameter_list':
126132
param_nodes = get_node_by_kind(subchild, ['parameter_declaration'])
127133
for param in param_nodes:
128-
for item in param.children:
129-
if item.type in ['type_identifier', 'primitive_type']:
130-
param_type = get_node_text(item)
131-
elif item.type == 'identifier':
132-
param_identifier = get_node_text(item)
133-
metadata['parameters'][param_identifier] = param_type
134+
param_type = param.child_by_field_name('type')
135+
param_type = get_node_text(param_type)
136+
list_name = get_node_by_kind(param, ['identifier'])
137+
assert len(list_name) == 1
138+
param_name = get_node_text(list_name[0])
139+
metadata['parameters'][param_name] = param_type
140+
# for item in param.children:
141+
142+
# if item.type in ['type_identifier', 'primitive_type']:
143+
# param_type = get_node_text(item)
144+
# elif item.type == 'identifier':
145+
# param_identifier = get_node_text(item)
134146

135147
return metadata
136148

137149
@staticmethod
138-
def get_class_metadata(class_node, blob: str) -> Dict[str, str]:
150+
def get_class_metadata(class_node, blob: str=None) -> Dict[str, str]:
139151
"""
140152
Class metadata contains:
141153
- identifier (str): class's name
142154
- parameters (List[str]): inheritance class
143155
"""
156+
if blob:
157+
logger.info('From version `0.0.6` this function will update argument in the API')
144158
metadata = {
145159
'identifier': '',
146160
'parameters': '',

src/codetext/parser/go_parser.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def get_docstring_node(node):
7171
return docstring_node
7272

7373
@staticmethod
74-
def get_docstring(node, blob:str):
74+
def get_docstring(node, blob:str=None):
7575
"""
7676
Get docstring description for node
7777
@@ -81,7 +81,8 @@ def get_docstring(node, blob:str):
8181
Returns:
8282
str: docstring
8383
"""
84-
logger.info('From version `0.0.6` this function will update argument in the API')
84+
if blob:
85+
logger.info('From version `0.0.6` this function will update argument in the API')
8586
docstring_node = GoParser.get_docstring_node(node)
8687
docstring = '\n'.join(get_node_text(s) for s in docstring_node)
8788
return docstring
@@ -92,8 +93,9 @@ def get_function_list(node):
9293
return res
9394

9495
@staticmethod
95-
def get_function_metadata(function_node, blob: str) -> Dict[str, str]:
96-
logger.info('From version `0.0.6` this function will update argument in the API')
96+
def get_function_metadata(function_node, blob: str=None) -> Dict[str, str]:
97+
if blob:
98+
logger.info('From version `0.0.6` this function will update argument in the API')
9799
metadata = {
98100
'identifier': '',
99101
'parameters': {},
@@ -104,16 +106,16 @@ def get_function_metadata(function_node, blob: str) -> Dict[str, str]:
104106
if child.type in ['field_identifier', 'identifier']:
105107
metadata['identifier'] = get_node_text(child)
106108
elif child.type == 'type_identifier':
107-
metadata['type'] = get_node_text(child)
109+
metadata['return_type'] = get_node_text(child)
108110
elif child.type == 'parameter_list':
109111
for subchild in child.children:
110112
if subchild.type in ['parameter_declaration', 'variadic_parameter_declaration']:
111113
identifier_node = subchild.child_by_field_name('name')
112-
param_type = get_node_text(subchild.child_by_field_name('type'))
113114

114115
if not identifier_node:
115116
continue
116117

118+
param_type = get_node_text(subchild.child_by_field_name('type'))
117119
identifier = get_node_text(identifier_node)
118120
if identifier and param_type:
119121
metadata['parameters'][identifier] = param_type
@@ -125,6 +127,7 @@ def get_class_list(node):
125127
pass
126128

127129
@staticmethod
128-
def get_class_metadata(class_node, blob) -> Dict[str, str]:
129-
logger.info('From version `0.0.6` this function will update argument in the API')
130+
def get_class_metadata(class_node, blob=None) -> Dict[str, str]:
131+
if blob:
132+
logger.info('From version `0.0.6` this function will update argument in the API')
130133
pass

src/codetext/parser/java_parser.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def get_docstring_node(node):
3434
return docstring_node
3535

3636
@staticmethod
37-
def get_docstring(node, blob):
37+
def get_docstring(node, blob=None):
3838
"""
3939
Get docstring description for node
4040
@@ -44,7 +44,8 @@ def get_docstring(node, blob):
4444
Returns:
4545
str: docstring
4646
"""
47-
logger.info('From version `0.0.6` this function will update argument in the API')
47+
if blob:
48+
logger.info('From version `0.0.6` this function will update argument in the API')
4849
docstring_node = JavaParser.get_docstring_node(node)
4950

5051
docstring = ''
@@ -82,8 +83,9 @@ def is_method_body_empty(node):
8283
return True
8384

8485
@staticmethod
85-
def get_class_metadata(class_node, blob: str) -> Dict[str, str]:
86-
logger.info('From version `0.0.6` this function will update argument in the API')
86+
def get_class_metadata(class_node, blob: str=None) -> Dict[str, str]:
87+
if blob:
88+
logger.info('From version `0.0.6` this function will update argument in the API')
8789
metadata = {
8890
'identifier': '',
8991
'parameters': '',
@@ -101,26 +103,25 @@ def get_class_metadata(class_node, blob: str) -> Dict[str, str]:
101103
return metadata
102104

103105
@staticmethod
104-
def get_function_metadata(function_node, blob: str) -> Dict[str, str]:
105-
logger.info('From version `0.0.6` this function will update argument in the API')
106+
def get_function_metadata(function_node, blob: str=None) -> Dict[str, str]:
107+
if blob:
108+
logger.info('From version `0.0.6` this function will update argument in the API')
106109
metadata = {
107110
'identifier': '',
108-
'parameters': '',
111+
'parameters': {},
109112
'return_type': None
110113
}
111114

112-
params = {}
113115
for child in function_node.children:
114116
if child.type == 'identifier':
115117
metadata['identifier'] = get_node_text(child)
116118
elif child.type == 'type_identifier':
117-
metadata['type'] = get_node_text(child)
119+
metadata['return_type'] = get_node_text(child)
118120
elif child.type == 'formal_parameters':
119-
param_list = get_node_by_kind(child, ['formal_parameter'])
121+
param_list = get_node_by_kind(child, ['formal_parameter']) # speed_parameter
120122
for param in param_list:
121123
param_type = get_node_text(param.child_by_field_name('type'))
122124
identifier = get_node_text(param.child_by_field_name('name'))
123-
params[identifier] = param_type
125+
metadata['parameters'][identifier] = param_type
124126

125-
metadata['parameters'] = params
126127
return metadata

src/codetext/parser/javascript_parser.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ def get_docstring_node(node):
3131
return docstring_node
3232

3333
@staticmethod
34-
def get_docstring(node, blob):
35-
logger.info('From version `0.0.6` this function will update argument in the API')
34+
def get_docstring(node, blob=None):
35+
if blob:
36+
logger.info('From version `0.0.6` this function will update argument in the API')
3637
docstring_node = JavascriptParser.get_docstring_node(node)
3738

3839
docstring = ''
@@ -65,8 +66,9 @@ def get_class_list(node):
6566
return res
6667

6768
@staticmethod
68-
def get_function_metadata(function_node, blob: str) -> Dict[str, str]:
69-
logger.info('From version `0.0.6` this function will update argument in the API')
69+
def get_function_metadata(function_node, blob: str=None) -> Dict[str, str]:
70+
if blob:
71+
logger.info('From version `0.0.6` this function will update argument in the API')
7072
metadata = {
7173
'identifier': '',
7274
'parameters': {},
@@ -77,16 +79,20 @@ def get_function_metadata(function_node, blob: str) -> Dict[str, str]:
7779
if child.type in ['identifier', 'property_identifier']:
7880
metadata['identifier'] = get_node_text(child)
7981
elif child.type == 'formal_parameters':
80-
for subchild in child.children:
81-
if subchild.type == 'identifier':
82-
param.append(get_node_text(subchild))
83-
84-
metadata['parameters'] = param
82+
params = get_node_by_kind(child, ['identifier'])
83+
for param in params:
84+
identifier = get_node_text(param)
85+
metadata['parameters'][identifier] = None # JS not have type define
86+
87+
return_statement = get_node_by_kind(function_node, ['return_statement'])
88+
if len(return_statement) > 0:
89+
metadata['return_type'] = '<not_specific>'
8590
return metadata
8691

8792
@staticmethod
88-
def get_class_metadata(class_node, blob):
89-
logger.info('From version `0.0.6` this function will update argument in the API')
93+
def get_class_metadata(class_node, blob=None):
94+
if blob:
95+
logger.info('From version `0.0.6` this function will update argument in the API')
9096
metadata = {
9197
'identifier': '',
9298
'parameters': '',

src/codetext/parser/php_parser.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ class PhpParser(LanguageParser):
2020
'__unserialize']
2121

2222
@staticmethod
23-
def get_docstring(node, blob: str) -> str:
24-
logger.info('From version `0.0.6` this function will update argument in the API')
23+
def get_docstring(node, blob: str=None) -> str:
24+
if blob:
25+
logger.info('From version `0.0.6` this function will update argument in the API')
2526
docstring_node = PhpParser.get_docstring_node(node)
2627

2728
docstring = ''
@@ -57,34 +58,45 @@ def get_function_list(node):
5758
return res
5859

5960
@staticmethod
60-
def get_function_metadata(function_node, blob: str) -> Dict[str, str]:
61-
logger.info('From version `0.0.6` this function will update argument in the API')
61+
def get_function_metadata(function_node, blob: str=None) -> Dict[str, str]:
62+
if blob:
63+
logger.info('From version `0.0.6` this function will update argument in the API')
6264
metadata = {
6365
'identifier': '',
64-
'parameters': '',
66+
'parameters': {},
6567
'return_type': None,
6668
}
6769

68-
params = []
6970
for n in function_node.children:
7071
if n.type == 'name':
7172
metadata['identifier'] = get_node_text(n)
72-
if n.type == 'union_type':
73-
metadata['type'] = get_node_text(n)
73+
if n.type in ['union_type', 'intersection_type']:
74+
metadata['return_type'] = get_node_text(n)
7475
elif n.type == 'formal_parameters':
7576
for param_node in n.children:
7677
if param_node.type in ['simple_parameter', 'variadic_parameter', 'property_promotion_parameter']:
77-
identifier = param_node.child_by_field_name('name')
78-
name = get_node_text(identifier)
79-
params.append(name)
78+
identifier = get_node_text(param_node.child_by_field_name('name'))
79+
param_type = param_node.child_by_field_name('type')
80+
if param_type:
81+
param_type = get_node_text(param_type)
82+
metadata['parameters'][identifier] = param_type
83+
else:
84+
metadata['parameters'][identifier] = None
8085

81-
metadata['parameters'] = params
86+
if not metadata['return_type']:
87+
return_statement = get_node_by_kind(function_node, ['return_statement'])
88+
if len(return_statement) > 0:
89+
metadata['return_type'] = '<not_specific>'
90+
else:
91+
metadata['return_type'] = None
92+
8293
return metadata
8394

8495

8596
@staticmethod
86-
def get_class_metadata(class_node, blob):
87-
logger.info('From version `0.0.6` this function will update argument in the API')
97+
def get_class_metadata(class_node, blob: str=None):
98+
if blob:
99+
logger.info('From version `0.0.6` this function will update argument in the API')
88100
metadata = {
89101
'identifier': '',
90102
'parameters': '',

0 commit comments

Comments
 (0)