forked from tech-srl/code2seq
- Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcpp_extractor.py
More file actions
Latest commit
43 lines (38 loc) · 1.82 KB
/
Copy pathcpp_extractor.py
File metadata and controls
43 lines (38 loc) · 1.82 KB
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
importos
fromcppminer.cpp_parserimportAstParser
fromcppminer.cpp_parser.sampleimportmake_str_key
fromcommonimportPathContextInformation
importtempfile
classCppExtractor:
def__init__(self, config, ):
self.config=config
self.parser=AstParser(max_contexts_num=self.config.MAX_CONTEXTS,
max_path_len=self.config.MAX_PATH_LENGTH,
max_subtokens_num=self.config.MAX_NAME_PARTS,
max_ast_depth=100,
out_path=None)
defextract_paths(self, code_string):
tmp=tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.cc')
try:
tmp.write(code_string)
tmp.close()
returnself.extract_paths(compiler_args=[], file_path=tmp.name)
finally:
os.unlink(tmp.name)
defextract_paths(self, compiler_args, file_path):
self.parser.parse(compiler_args=compiler_args, file_path=file_path)
pc_info_dict= {}
result= []
forsampleinself.parser.samples:
forcontextinsample.contexts:
info_context= {'name1': make_str_key(context.start_token),
'name2': make_str_key(context.end_token),
'path': make_str_key(context.path.tokens),
'shortPath': make_str_key(context.path.tokens)}
pc_info=PathContextInformation(info_context)
pc_info_dict[(pc_info.token1, pc_info.shortPath, pc_info.token2)] =pc_info
result_line=str(sample)
space_padding=' '* (self.config.DATA_NUM_CONTEXTS-len(sample.contexts))
result_line+=space_padding
result.append(result_line)
returnresult, pc_info_dict