From fbb9e3db9a4108d34c18ea71d011a11992936bbe Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 28 Oct 2012 18:05:25 +0100 Subject: tree parser --- auto-auto-complete.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/auto-auto-complete.py b/auto-auto-complete.py index 7227b09..bd10b97 100644 --- a/auto-auto-complete.py +++ b/auto-auto-complete.py @@ -42,6 +42,85 @@ def printerr(text = '', end = '\n'): + +''' +Bracket tree parser +''' +class Parser: + ''' + Parse a code and return a tree + + @param code:str The code to parse + @return :list|str> The root node in the tree + ''' + @staticmethod + def parse(code): + raw = code.replace('\f', '\n').replace('\r\n', '\n').replace('\r', '\n').replace('\t', ' ') + stack = [] + stackptr = -1 + + comment = False + escape = False + quote = None + buf = None + + for charindex in range(0, len(code)): + c = code[charindex] + if comment: + if c in '\n\r\f': + comment = False + elif escape: + escape = False + if c == 'a': buf += '\a' + elif c == 'b': buf += chr(8) + elif c == 'e': buf += '\033' + elif c == 'f': buf += '\f' + elif c == 'n': buf += '\n' + elif c == 'r': buf += '\r' + elif c == 't': buf += '\t' + elif c == 'v': buf += chr(11) + elif c == '0': buf += '\0' + else + buf += c + elif c == quote: + quote = None + elif (c == ';') and (quote is None): + if buf is not None: + stack[stackptr].append(buf) + buf = None + comment = True + elif (c == '(') and (quote is None): + if buf is not None: + stack[stackptr].append(buf) + buf = None + stackptr += 1 + stack[stackptr] = [] + elif (c == ')') and (quote is None): + if stackptr == 0: + return stack[0] + stackptr -= 1 + stack[stackptr].append(stack[stackptr + 1]) + elif (c in ' \t\n\r\f') and (quote is None): + if buf is not None: + stack[stackptr].append(buf) + buf = None + else: + if buf is None: + buf = '' + if c == '\\': + escape = True + elif c in '\'\"': + quote = c + else: + buf += c + + raise Exception('premature end of file') + + + +''' +mane! +''' if __name__ = '__main__': print('I am so sorry, this is not implement yet.') -- cgit v1.2.3-70-g09d2