Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- simple expression2 Garry's mod analyzer
- # -- main -- #
- import functional
- import cutter
- import anytree as an
- functional.core.eol = '\n'
- source_code = open("D:/inp2.txt", "r").read()
- source_code = functional.delete_comments(source_code)
- # source_code = functional.core.delete_spc(source_code)
- # source_code = functional.compress_inline(source_code, 140)
- namespaces = functional.trace_namespaces(source_code, functional.create_prepared_ex_zones(source_code))
- """pos = 0
- while pos < len(namespaces):
- print("+ + + + + + + + + + + +")
- print(source_code[namespaces[pos].beg:namespaces[pos].end+1])
- print("- - - - - - - - - - - ->", namespaces[pos].level, "<- - -\n")
- pos += 1"""
- tr = functional.core.structures.NamespaceTree(namespaces)
- for I in an.PreOrderIter(tr.tree):
- b = I.name.beg
- e = I.name.end
- print(source_code[b:e])
- print("- - - - - - - - - - - - - - - - - - - - - - ")
- file = open("D:/out.txt", "w")
- file.write(source_code)
- file.close()
- # -- functional -- #
- import core_functs as core
- buffer_traced_code = {}
- def delete_spaces(src):
- g = core.delete_spc(src, True)
- return g
- def delete_comments(src):
- g = core.trace_code(src)
- return core.str_list_cutter(src, g["comments"] + g["multiline_comments"])
- def create_prepared_ex_zones(src):
- return core.trace_code(src)["strings"]
- def set_eol(new_eol_symbol):
- core.eol = new_eol_symbol
- def trace_namespaces(src, excluded_zones, brtype='{}'):
- if len(src) == 0:
- print("> stderr: functional/trace_namespaces called with src=''")
- return []
- if len(excluded_zones) == 0:
- print("> stderr: functional/trace_namespaces called with excluded_zones = []")
- return []
- ret = []
- pos = 0
- while True:
- curr = core.safety_find(src, brtype[0], pos, excluded_zones)
- if curr != -1:
- curr_end = core.trace_bracket(src, curr, brtype)
- xp = 0
- level = 0
- while xp < len(ret):
- if ret[xp].end > curr:
- level += 1
- xp += 1
- ths = core.structures.Namespace(curr, curr_end, level)
- ret.append(ths)
- if curr+1 < len(src):
- pos = curr+1
- else:
- break
- else:
- break
- return ret
- def get_global_vars(lined_src, srclen):
- if len(lined_src) == 0:
- print("> stderr: functional/get_global_vars called with lined_src = []")
- return []
- persist_parser = core.structures.Persist("", "", "")
- ret = []
- ginit = {"inputs": [], "outputs": [], "persist": [], "trigger": []}
- keys = ginit.keys()
- for I in lined_src:
- for K in keys:
- if I.find("@" + K) == 0:
- ginit[K].append(I[len(K) + 2:])
- for I in keys:
- for K in ginit[I]:
- ret += persist_parser.parse_list(K, srclen, I)
- return ret
- def compress_inline(src, limiter=80):
- if len(src) == 0:
- print("> stderr: functional/compress_inline called with src = ''")
- return src
- l = src.split(core.eol)
- ret = ""
- line_len = 0
- for I in l:
- if len(I) != 0 and I[0] == '@':
- ret += I.lstrip() + core.eol
- continue
- if line_len <= limiter:
- if line_len != 0:
- ret += ' '
- ret += I
- line_len += len(I)+1
- else:
- ret += core.eol + I
- line_len = len(I)+1
- return ret
- # -- cutter -- #
- import functional
- class Cutter:
- def __init__(self, src=""):
- self.src = src
- self.context_eol = functional.core.eol
- self.prepared = self.prepare_code()
- self.lined_prepared = self.prepared.split(self.context_eol)
- self.excluded_zones = self.create_prepared_zones(self.prepared)
- self.persist_parser = functional.core.structures.Persist("", "", "")
- self.gvars = self.get_gvars()
- def prepare_code(self, **kwargs):
- self.prepared = functional.delete_comments(self.src)
- self.prepared = functional.delete_spaces(self.prepared)
- return self.prepared
- def create_prepared_zones(self, src):
- return functional.create_prepared_ex_zones(self.prepared)
- def get_gvars(self):
- return functional.get_global_vars(self.lined_prepared, len(self.prepared))
- # -- core_functs -- #
- import structures
- eol = chr(10)
- def check_excluded(ind, ex_list):
- for I in ex_list:
- if I[0] <= ind <= I[1]:
- return False
- return True
- def safety_find(src, pattern, start, zones):
- pos = start
- while True:
- ans = src.find(pattern, pos)
- if ans != -1:
- if check_excluded(ans, zones):
- return ans
- else:
- pos = ans+1
- else:
- return -1
- def advfind(src, spos, nf, f):
- pos = spos
- while True:
- a = src.find(f, pos)
- if a != -1:
- sum = 0
- ctr = 0
- for I in range(a - len(nf), a):
- if src[I] == nf[ctr]:
- ctr += 1
- sum += 1
- else:
- break
- if sum != len(nf):
- return a
- else:
- pos = a+1
- else:
- return -1
- def trace_code(src, end_of_line=eol, fullwrap_string=True):
- ret = {"strings": [], "multiline_comments": [], "comments": []}
- pos = 1
- while pos < len(src):
- if src[pos] == '"' and src[pos-1] != '\\': # code_line "<THIS TEXT WILL BE CAPTURED WITHOUT ">"
- spos = pos
- epos = advfind(src, spos+1, '\\', '"') + 1
- if epos == -1:
- break
- pos = epos+1
- if fullwrap_string:
- ret["strings"].append([spos, epos])
- else:
- ret["strings"].append([spos+1, epos-1])
- elif src[pos-1] == '#' and src[pos] != '[':
- spos = pos-1
- epos = spos+1
- if epos == -1:
- break
- while src[epos] != end_of_line and epos < len(src):
- epos += 1
- pos = epos+1
- ret["comments"].append([spos, epos])
- elif src[pos-1] == "#" and src[pos] == '[':
- spos = pos-1
- epos = src.find("]#", spos) + 2
- if epos == -1:
- break
- pos = epos+1
- ret["multiline_comments"].append([spos, epos])
- else:
- pos += 1
- return ret
- def cut_from_string(src, f, t):
- return src[:f] + src[t:]
- def str_list_cutter(src, listof: list):
- if len(listof) == 0:
- print("> stderr: str_list_cutter called with listof = []")
- return src
- listof.sort()
- ret = ""
- pos = 0
- for I in listof:
- ret += src[pos:I[0]]
- pos = I[1]
- ret += src[listof[-1][1]:]
- return ret
- def delete_spc(src, del_endls=True):
- symbs = [' ', ',', '.', '+', '-', '=', '/', '%', '^', '!', '*', '{', '}', '&', '|', '$', '?']
- lenght = len(src)
- pos = 1
- ret = "" + src[0]
- while pos < lenght-1:
- if src[pos] == ' ':
- is_skip = False
- if src[pos-1] in symbs or src[pos+1] in symbs:
- is_skip = True
- if is_skip:
- pos += 1
- continue
- else:
- ret += src[pos]
- else:
- ret += src[pos]
- pos += 1
- ret += src[-1]
- if del_endls:
- lines = ret.split(eol)
- ret = ""
- for I in lines:
- if I != '':
- ret += I+eol
- pos = 1
- src = '' + ret[0]
- while pos < len(ret)-1:
- if ret[pos] == eol:
- is_skip = False
- if ret[pos-1] in ['(', '{', '[', ')', '}', ']'] and ret[pos+1] in ['(', '{', '[', ')', '}', ']']:
- is_skip = True
- if is_skip:
- pos += 1
- continue
- else:
- src += ret[pos]
- pos += 1
- continue
- src += ret[pos]
- pos += 1
- src += ret[-1]
- return src
- return ret
- def trace_bracket(src, pos, brtype="{}"):
- cntr = 0
- while pos < len(src):
- if src[pos] == brtype[0]:
- cntr += 1
- if src[pos] == brtype[1]:
- cntr -= 1
- if cntr == 0:
- return pos
- pos += 1
- print("> stderr: core_functs/trace_brackets couldn't find closer-bracket, returned -1")
- return -1
- def trace_brackets_rev(src, pos, brtype=")("):
- cntr = 0
- while pos > 0:
- if src[pos] == brtype[0]:
- cntr += 1
- if src[pos] == brtype[1]:
- cntr -= 1
- if cntr == 0:
- return pos
- pos -= 1
- print("> stderr: core_functs/trace_brackets_rev couldn't find opener-bracket, returned -1")
- return -1
- def find_nearest(src, pos, symb, direction):
- it = 0
- if direction >= 1:
- it = 1
- while src[pos] != symb and pos < len(src):
- pos += it
- else:
- it = -1
- while src[pos] != symb and pos > -1:
- pos += it
- if src[pos] != symb:
- print("> stderr: core_functs/find_nearest couldn't find nearest symbol to", symb, "from pt =", pos)
- return -1
- return pos
- def get_word(src, pos, direction):
- if direction >= 1:
- it = 1
- while (not src[pos].isspace()) and (pos < len(src)):
- pos += it
- while src[pos].isspace() and (pos < len(src)):
- pos += it
- sp = pos
- while (not src[pos].isspace()) and (pos < len(src)):
- pos += it
- return [pos, src[sp:pos+1]]
- else:
- it = -1
- while (not src[pos].isspace()) and pos > -1:
- pos += it
- while src[pos].isspace() and (pos > -1):
- pos += it
- sp = pos
- while (not src[pos].isspace()) and (pos > -1):
- pos += it
- print(">>>>>", pos, sp, src[pos:sp+1].strip())
- return [pos, src[pos:sp+1].strip()]
- # -- structures -- #
- import anytree as an
- allowed_types = ["quaternion", "vector", "vector4", "vector2", "array", "table", "string", "number", "ranger", "bone", "wirelink", "gtable", "angle", "entity"]
- class Namespace:
- def __init__(self, beginning, ending, level=0):
- self.beg = abs(beginning)
- self.end = abs(ending)
- self.level = abs(level)
- self.content = ""
- self.header = ''
- # function abs(A,B,C) - header
- # if (STATE_A ^ STATE_B) - header
- def set_content(self, src, hed=''):
- self.content = src
- self.header = hed
- class NamespaceTree:
- def __init__(self, traced):
- pos = 0
- self.tree = an.Node(Namespace(0, 0, -1))
- ptr = self.tree
- prev_level = -1
- while pos < len(traced):
- if prev_level < traced[pos].level:
- adder = an.Node(traced[pos], ptr)
- ptr = adder
- prev_level = traced[pos].level
- elif prev_level == traced[pos].level:
- ptr = ptr.parent
- adder = an.Node(traced[pos], ptr)
- ptr = adder
- else:
- for I in range(abs(prev_level - traced[pos].level) + 1):
- ptr = ptr.parent
- adder = an.Node(traced[pos], ptr)
- ptr = adder
- prev_level = traced[pos].level
- pos += 1
- print(an.RenderTree(self.tree, style=an.AsciiStyle()).by_attr())
- class Persist:
- def __init__(self, name: str, logic_type: str, param_type: str, namespace=Namespace(0, 0)):
- if not name[0].isupper():
- print("> stderr: object core_functs/Persist created with arg<name> =", name, "but "
- "persist name must begin with a capital letter, a parsing error is possible!")
- if logic_type not in allowed_types:
- print("> stderr: object core_functs/Persist created with arg<logic_type> =", logic_type, "but "
- "allowed only " + ", ".join(sorted(allowed_types)), ", a parsing error is possible!")
- self.name = name
- self.logic_type = logic_type
- self.param_type = param_type
- self.namespace_limits = namespace
- self.defined_name = name
- def parse_list(self, src_line, src_len, param_type="persist"):
- ret = []
- src_line = src_line.strip()
- pos = 0
- while pos < len(src_line):
- if src_line[pos] not in ['[', ']', ' ']:
- end = src_line.find(' ', pos)
- if end == -1:
- end = len(src_line)
- ths = src_line[pos:end]
- tpos = ths.find(':')
- ths_type = ""
- if tpos == -1:
- ths_type = "number"
- else:
- ths_type = ths[tpos+1:]
- if tpos != -1:
- ths_name = ths[:tpos]
- else:
- ths_name = ths
- ret.append(Persist(ths_name, ths_type, param_type, namespace=Namespace(0, src_len)))
- pos = end
- elif src_line[pos] == '[':
- end = src_line.find(']', pos)
- closed_names = src_line[pos+1:end].split(' ')
- ends = src_line.find(' ', end)
- ths_type = ''
- if src_line[end+1] != ':':
- ths_type = "number"
- else:
- if ends != -1:
- ths_type = src_line[end+2:ends]
- else:
- ths_type = src_line[end+2:]
- for I in closed_names:
- ret.append(Persist(I, ths_type, param_type, namespace=Namespace(0, src_len)))
- pos = ends
- if pos == -1:
- break
- pos += 1
- return ret
- def debug_out(self):
- print("> stderr: name =", self.name)
- print("> stderr: self.logic_type =" + self.logic_type + "\n"
- "self.param_type =" + self.param_type + "\n"
- "self.defined_name =" + self.name + "\n"
- )
- # -- END -- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement