-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax_file_parser.py
59 lines (44 loc) · 1.72 KB
/
syntax_file_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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pathlib
import json
from typing import Dict, List
class SyntaxFileParser(object):
def __init__(self, file_path: str):
self.file_path: pathlib.Path = pathlib.Path(file_path)
self.syntax = self.parse_syntax_file(self.file_path)
@staticmethod
def parse_syntax_file(file_path: pathlib.Path) -> Dict[str, List[str]]:
"""
Parse syntax file and get builtin_functions, params, special_variables
:return: dictionary containing syntax
:rtype: Dict[str, List[str]]
"""
syntax = {
"builtin_functions": [],
"params": [],
"special_variables": [],
}
with open(file_path, "r") as syntax_file:
raw_syntax = json.load(syntax_file)
syntax["special_variables"] = raw_syntax.get("special_variables", [])
builtin_functions = raw_syntax.get("builtin_functions", [])
for function in builtin_functions:
for func_name, func_params in function.items():
syntax["builtin_functions"].append(func_name)
for param in func_params:
for param_name in param.values():
syntax["params"].extend(param_name)
return syntax
def get_keywords(self) -> List[str]:
"""
Get a list of all syntax keywords
:return: list of all syntax kywords
:rtype: List[str]
"""
keywords = [kw for cat in self.syntax.values() for kw in cat]
return list(set(keywords)) # to remove possible duplicates
if __name__ == '__main__':
t = SyntaxFileParser("config/syntax.json")
print(t.syntax)
print(t.get_keywords())