~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/interpreter/pyparser/syntaxtree.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""SyntaxTree class definition"""
 
2
# try:
 
3
# #    from pypy.interpreter.pyparser.pysymbol import sym_values
 
4
#     from pypy.interpreter.pyparser.pytoken import tok_values
 
5
# except ImportError:
 
6
# #    from pysymbol import sym_values
 
7
#     from pytoken import tok_values
 
8
 
 
9
from pypy.tool.uid import uid
 
10
 
 
11
from pypy.tool.uid import uid
 
12
 
 
13
class AbstractSyntaxVisitor(object):
 
14
    def visit_syntaxnode( self, node ):
 
15
        pass
 
16
 
 
17
    def visit_tempsyntaxnode( self, node ):
 
18
        pass
 
19
 
 
20
    def visit_tokennode( self, node ):
 
21
        pass
 
22
 
 
23
class SyntaxNode(object):
 
24
    """A syntax node"""
 
25
    def __init__(self, name, args, lineno=-1):
 
26
        self.name = name
 
27
        self.nodes = args
 
28
        self.lineno = lineno
 
29
        
 
30
    def dumptree(self, treenodes, indent):
 
31
        """helper function used to dump the syntax tree"""
 
32
        treenodes.append(str(self.name))
 
33
        if len(self.nodes) > 1:
 
34
            treenodes.append(" -> (\n")
 
35
            treenodes.append(indent+" ")
 
36
            for node in self.nodes:
 
37
                node.dumptree(treenodes, indent+" ")
 
38
            treenodes.append(")\n")
 
39
            treenodes.append(indent)
 
40
        elif len(self.nodes) == 1:
 
41
            treenodes.append(" ->\n")
 
42
            treenodes.append(indent+" ")
 
43
            self.nodes[0].dumptree(treenodes, indent+" ")
 
44
 
 
45
    def dumpstr(self):
 
46
        """turns the tree repr into a string"""
 
47
        treenodes = []
 
48
        self.dumptree(treenodes, "")
 
49
        return "".join(treenodes)
 
50
 
 
51
    def __repr__(self):
 
52
        return "<node [%s] at 0x%x>" % (self.name, uid(self))
 
53
 
 
54
    def __str__(self):
 
55
        return "(%s)" % self.name
 
56
 
 
57
    def visit(self, visitor):
 
58
        assert isinstance(visitor, AbstractSyntaxVisitor) 
 
59
        return visitor.visit_syntaxnode(self)
 
60
 
 
61
    def expand(self):
 
62
        """expand the syntax node to its content,
 
63
        do nothing here since we are a regular node and not
 
64
        a TempSyntaxNode"""
 
65
        return [ self ]
 
66
 
 
67
    def totuple(self, sym_values, lineno=False ):
 
68
        """returns a tuple representation of the syntax tree
 
69
        needs symbols+tokens value to name mapping to represent the nodes
 
70
        """
 
71
        symvalue = sym_values.get( self.name, (0, self.name) )
 
72
        l = [ symvalue ]
 
73
        l += [node.totuple(lineno, sym_values ) for node in self.nodes]
 
74
        return tuple(l)
 
75
    
 
76
 
 
77
class TempSyntaxNode(SyntaxNode):
 
78
    """A temporary syntax node to represent intermediate rules"""
 
79
    def expand(self):
 
80
        """expand the syntax node to its content"""
 
81
        return self.nodes
 
82
 
 
83
    def visit(self, visitor):
 
84
        assert isinstance(visitor, AbstractSyntaxVisitor) 
 
85
        return visitor.visit_tempsyntaxnode(self)
 
86
 
 
87
class TokenNode(SyntaxNode):
 
88
    """A token node"""
 
89
    def __init__(self, name, value, lineno = -1):
 
90
        SyntaxNode.__init__(self, name, [], lineno)
 
91
        self.value = value
 
92
 
 
93
    def dumptree(self, treenodes, indent):
 
94
        """helper function used to dump the syntax tree"""
 
95
        if self.value:
 
96
            treenodes.append("%s='%s' (%d) " % (self.name, self.value,
 
97
                                                self.lineno))
 
98
        else:
 
99
            treenodes.append("'%s' (%d) " % (self.name, self.lineno))
 
100
 
 
101
    def __repr__(self):
 
102
        if self.value is not None:
 
103
            return "<%s=%s>" % ( self.name, repr(self.value))
 
104
        else:
 
105
            return "<%s!>" % (self.name,)
 
106
 
 
107
    def visit(self, visitor):
 
108
        assert isinstance(visitor, AbstractSyntaxVisitor) 
 
109
        return visitor.visit_tokennode(self)
 
110
 
 
111
    def totuple(self, tok_values, lineno=False):
 
112
        """returns a tuple representation of the syntax tree
 
113
        needs symbols+tokens value to name mapping to represent the nodes
 
114
        """
 
115
        num = tok_values.get(self.name, -1)
 
116
        if num == -1:
 
117
            print "Unknown", self.name, self.value
 
118
        if self.value is not None:
 
119
            val = self.value
 
120
        else:
 
121
            if self.name not in ("NEWLINE", "INDENT", "DEDENT", "ENDMARKER"):
 
122
                val = self.name
 
123
            else:
 
124
                val = self.value or ''
 
125
        if lineno:
 
126
            return (num, val, self.lineno)
 
127
        else:
 
128
            return (num, val)