File: astsupport.py

package info (click to toggle)
antlr 2.7.7%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 9,888 kB
  • sloc: java: 54,649; cs: 12,537; makefile: 8,945; cpp: 7,359; pascal: 5,273; sh: 4,337; python: 4,301; lisp: 1,969; xml: 220; lex: 192; ansic: 127
file content (107 lines) | stat: -rw-r--r-- 2,452 bytes parent folder | download | duplicates (11)
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
import sys
import antlr

version = sys.version.split()[0]
if version < '2.2.1':
    False = 0
if version < '2.3':
    True = not False

class MyAST(antlr.CommonAST): pass

class ASTType49(antlr.CommonAST): pass




def testDefaultCreate():
   t =factory.create()
   return checkNode(t, antlr.CommonAST, antlr.INVALID_TYPE)


def testSpecificHomoCreate() :
   ### class names as strings not supported
   factory.setASTNodeClass(MyAST)
   t = factory.create()
   factory.setASTNodeClass(antlr.CommonAST)
   return checkNode(t, MyAST, antlr.INVALID_TYPE)


def testDynamicHeteroCreate() :
    factory.setTokenTypeASTNodeType(49,ASTType49)
    t = factory.create(49)
    a = checkNode(t, ASTType49, 49)
    u = factory.create(55)
    b = checkNode(u,antlr.CommonAST, 55)
    v = factory.create(49,"",MyAST)
    c = checkNode(v, MyAST, 49)
    factory.setTokenTypeASTNodeType(49,None)
    return a and b and c
  

def testNodeDup() :
    t = factory.create()
    a = t.equals(antlr.dup(t,factory))
    b = not t.equals(None)
    u = factory.create(49,"",ASTType49)
    c = checkNode(antlr.dup(u,factory),ASTType49, 49)
    d = u.equals(antlr.dup(u,factory))
    return a and b and c and d

def testHeteroTreeDup() :
    x = factory.create(1,"[type 1]",MyAST) ## will be root
    y = factory.create(2,"[type 2]",MyAST)
    z = factory.create(3,"[type 3]",MyAST)
    sub = factory.create(49,"[type 49]",ASTType49)
    sub.addChild(factory.create(3,"[type 3 #2]",MyAST))
    t = antlr.make(x,y,sub,z)
    dup_t = antlr.dupList(t,factory)
    ## check structure
    a = dup_t.equalsList(t)
    ## check types
    b = equalsNodeTypesList(t,dup_t)
    return a and b 

def checkNode(t,c, tokenType) :
   if not t:
      return False
   if t.__class__ != c:
      return False

   if t.getType()!=tokenType:
      return False
    
   return True 
  

def equalsNodeTypesList(this, t) :
   return antlr.cmptree(this,t,partial=False)

def error(test) :
    print "Test "+test+" FAILED"

def success(test) :
    print "Test "+test+" succeeded"

if __name__ == "__main__" :
   factory = antlr.ASTFactory()

   funcs = [
      testDefaultCreate,
      testSpecificHomoCreate,
      testDefaultCreate,
      testSpecificHomoCreate,
      testNodeDup,
      testHeteroTreeDup
      ]

   for f in funcs:
      if f():
         success(f.__name__)
      else:
         error(f.__name__)
          
   import ASTsupportParser
   P = ASTsupportParser.Parser()

   P.main()