forked from ungod/LuaCWC-LuaAutoComplete
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildDefinition.py
More file actions
131 lines (111 loc) · 4.08 KB
/
buildDefinition.py
File metadata and controls
131 lines (111 loc) · 4.08 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
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
import os
import json
import sublime
import codecs
import re
try:
import utils
except ImportError:
from . import utils
class BuildDefinition:
def __init__(self):
self.dir = sublime.packages_path() + "/User/LuaCWC.cache/"
self.path = self.dir + "definition.json"
self.defi = {}
def loadCache(self):
if os.path.exists(self.path):
f = open(self.path)
data = f.read()
self.defi = json.loads(data)
def build(self, dirs):
self.defi = {}
def recurDir(d):
for item in os.listdir(d):
path = os.path.join(d, item)
if os.path.isdir(path):
recurDir(path)
elif utils.isLuaFile(path):
f = codecs.open(path, "r", "utf-8")
s = f.read()
self.parseLua(s)
recurDir(dirs)
def parseLua(self, strg):
m = re.match("Config", strg, re.MULTILINE)
# get Class = class("classname", {
# val = valCls
# }
# like
# mat = re.finditer(
# "^(?:local\s*){0,}([\w\d]+)\s*=\s*class.+,\s*\n*\{(\n+[^\}]*){1,}\}\)", strg, re.MULTILINE)
mat = re.finditer(
"^(?:local\s*){0,}([\w\d]+)\s*=\s*class.+,\s*\n*\{((?:\n*.*?){1,})\}\)", strg, re.MULTILINE)
for item in mat:
cls = item.group(1)
vals = utils.findValues("", item.group(2))
if not cls in self.defi:
self.defi[cls] = {}
for key, val in vals.items():
self.defi[cls][key] = val
# function block
mat = re.finditer(
"^function\s+([\w\d]+)[\:\.]([\w\d]+\s*\(.*\))(.*)$(?:\n+.+){1,}?\nend", strg, re.MULTILINE)
for item in mat:
cls = item.group(1)
func = item.group(2)
if cls == "this":
continue
if not cls in self.defi:
self.defi[cls] = {}
# get "function () --[return:returnValue]" like
funcWithNote = item.group(2) + item.group(3)
funcMat = re.search("(.+)\s*--\[return:(.+)\]", funcWithNote)
if funcMat:
self.defi[cls][funcMat.group(1)] = "func:" + funcMat.group(2)
else:
self.defi[cls][func] = "func:nil"
# get "self.val" like
vals = utils.findValues("self\.", item.group(0))
for key, val in vals.items():
self.defi[cls][key] = val
# valMat = re.finditer(
# "self\.([\w\d]+)\s*=\s*(.+)", item.group(0), re.MULTILINE)
# for valItem in valMat:
# val = valItem.group(1).encode("utf-8")
# itemMat = re.match("([\w\d]+)\.New.+", valItem.group(2))
# if itemMat:
# self.defi[cls][val] = "val:" + itemMat.group(1)
# else:
# self.defi[cls][val] = "val:nil"
# get super like "class("ClassName", Super)"
mat = re.finditer(
"^(?:local\s*){0,}([\w\d]+)\s*=\s*class\(.+,\s*([\w\d]+)\)", strg, re.MULTILINE)
for item in mat:
cls = item.group(1)
sup = item.group(2)
if not cls in self.defi:
self.defi[cls] = {}
self.defi[cls]["::super"] = sup
def save(self):
res = json.dumps(self.defi, indent=4)
if not os.path.exists(self.dir):
os.makedirs(self.dir)
f = open(self.path, "w+")
f.write(res)
f.close()
def delete(self, path, root):
if not os.path.exists(path):
return
if os.path.isfile(path):
try:
os.remove(path)
except Exception:
pass
elif os.path.isdir(path):
for item in os.listdir(path):
itemSrc = os.path.join(path, item)
delete(itemSrc, root)
if path != root:
try:
os.rmdir(path)
except Exception:
pass