Beispiel #1
0
def process(t, ruleNames):
    global level
    if t.getChildCount() == 0:
        return escapeWhitespace(Trees.getNodeText(t, ruleNames), False)
    sb = ""
    sb += lead(level)
    level += 1
    s = escapeWhitespace(Trees.getNodeText(t, ruleNames), False)
    sb += (s + ' ')
    for i in range(t.getChildCount()):
        sb += process(t.getChild(i), ruleNames)
    level -= 1
    # sb += lead(level)
    return sb
Beispiel #2
0
    def toJson(cls, obj, t, ruleNames=None, recog=None):
        if recog is not None:
            ruleNames = recog.ruleNames
        s = escapeWhitespace(cls.getNodeText(t, ruleNames), False)
        #print s
        if t.getChildCount() == 0:
            return {"name": s}

        with StringIO() as buf:
            buf.write(u"(")
            obj = dict()
            obj['name'] = s
            obj['children'] = []
            #buf.write(s)
            #buf.write(u' ')
            for i in range(0, t.getChildCount()):
                if i > 0:
                    pass
                    #buf.write(u' ')
                    #print t.getChildCount()

                #buf.write(listElements)
                obj['children'].append(
                    cls.toJson(obj, t.getChild(i), ruleNames))
            #buf.write(u")")
            #return buf.getvalue()
            return (obj)
 def toStringTreeList(cls, t, ruleNames=None, recog=None):
     # from a tree to a list of names
     if recog is not None:
         ruleNames = recog.ruleNames
     s = escapeWhitespace(cls.getNodeText(t, ruleNames), False)
     lis = [s, None]
     if t.getChildCount() == 0:
         return lis
     lis[1] = []
     for i in range(t.getChildCount()):
         lis[1].append(cls.toStringTreeList(t.getChild(i), ruleNames))
     return lis
Beispiel #4
0
def treeDesc(t: Tree, p, indent=0):
    ruleNames = p.ruleNames
    s = escapeWhitespace(Trees.getNodeText(t, ruleNames), False)
    if t.getChildCount() == 0:
        return s
    with StringIO() as buf:
        buf.write(s + "\n")
        indent += 2
        for i in range(0, t.getChildCount()):
            buf.write(' ' * indent)
            buf.write(treeDesc(t.getChild(i), p, indent) + "\n")
        return buf.getvalue()
Beispiel #5
0
def build_tree(cls, t, recog, result=[]):
    s = escapeWhitespace(cls.getNodeText(t, recog.ruleNames), False)

    if t.getChildCount() == 0:
        return s

    result.append(s)
    res = []
    for i in range(0, t.getChildCount()):
        if i > 0:
            res.append(build_tree(cls, t.getChild(i), recog, result[-1]))
    result.append(res)

    return result
Beispiel #6
0
 def toStringTree(cls, t, ruleNames=None, recog=None):
     if recog is not None:
         ruleNames = recog.ruleNames
     s = escapeWhitespace(cls.getNodeText(t, ruleNames), False)
     if t.getChildCount()==0:
         return s
     with StringIO() as buf:
         buf.write(u"(")
         buf.write(s)
         buf.write(u' ')
         for i in range(0, t.getChildCount()):
             if i > 0:
                 buf.write(u' ')
             buf.write(cls.toStringTree(t.getChild(i), ruleNames))
         buf.write(u")")
         return buf.getvalue()
Beispiel #7
0
    def toStringTree(cls, t, ruleNames=None, recog=None, level=0):

        if recog is not None:
            ruleNames = recog.ruleNames

        s = escapeWhitespace(cls.getNodeText(t, ruleNames), False)

        if t.getChildCount() == 0:
            return '  ' * level + s

        with StringIO() as buf:
            buf.write('  ' * level + s + '\n')
            for i in range(0, t.getChildCount()):
                if i > 0:
                    buf.write('  ' * level + '\n')
                buf.write(
                    cls.toStringTree(t.getChild(i),
                                     ruleNames,
                                     recog=recog,
                                     level=level + 1))
            return buf.getvalue()
Beispiel #8
0
    def toStringTree(cls, t, ruleNames=None, recog=None):
        __string_list = []
        if recog is not None:
            ruleNames = recog.ruleNames
        s = escapeWhitespace(cls.getNodeText(t, ruleNames), False)
        if t.getChildCount()==0:
            return s
        with StringIO() as buf:
            
            if s not in __string_list :
                buf.write(u"<")
                buf.write(s)
                buf.write(u'>')
            for i in range(0, t.getChildCount()):
                if i > 0:
                    buf.write(u' ')
                buf.write(cls.toStringTree(t.getChild(i), ruleNames))
            if s not in __string_list:
                buf.write(u"</")
                buf.write(s)
                buf.write(u">")

            with open('/workhome/sql_python/original.xml','w') as file:
                file.write(buf.getvalue())
Beispiel #9
0
    from antlr4.tree.Trees import Trees
    from antlr4.Utils import escapeWhitespace

    # from analyzer.solParser import solParser

    results = []
    test_tree = list(Trees.getChildren(tree))[6:7][0]
    builded = build_tree(Trees, test_tree, parser)
    print(builded)

    for line in list(Trees.getChildren(tree))[6:7]:
        res = child_pairs(line)
        res_pairs = [('program', 'line')]
        for r in res:
            if escapeWhitespace(Trees.getNodeText(r[1], None, recog=parser),
                                False) == '\\r\\n':
                continue

            parent = escapeWhitespace(
                Trees.getNodeText(r[0], None, recog=parser), False)
            child = escapeWhitespace(
                Trees.getNodeText(r[1], None, recog=parser), False)

            print(parent, str(r[0]))
            print(child, str(r[1]))

            if parent in ('positive_number', 'row_of_numbers',
                          'natural_number'):
                continue

            res_pairs.append((parent, child))