File: Transformer.py

package info (click to toggle)
synopsis 0.8.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,112 kB
  • ctags: 12,996
  • sloc: cpp: 34,254; ansic: 33,620; python: 10,975; sh: 7,261; xml: 6,369; makefile: 773; asm: 445
file content (49 lines) | stat: -rw-r--r-- 1,366 bytes parent folder | download | duplicates (2)
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
#
# Copyright (C) 2005 Stefan Seefeld
# All rights reserved.
# Licensed to the public under the terms of the GNU LGPL (>= 2),
# see the file COPYING for details.
#

from Synopsis import AST
from Processor import Processor

class Transformer(Processor):
    """A class that creates a new AST from an old one. This is a helper base for
    more specialized classes that manipulate the AST based on
    the comments in the nodes"""

    def __init__(self, **kwds):
        """Constructor"""

        Processor.__init__(self, **kwds)
        self.__scopestack = []
        self.__currscope = []

    def finalize(self):
        """replace the AST with the newly created one"""

        self.ast.declarations()[:] = self.__currscope

    def push(self):
        """Pushes the current scope onto the stack and starts a new one"""

        self.__scopestack.append(self.__currscope)
        self.__currscope = []

    def pop(self, decl):
        """Pops the current scope from the stack, and appends the given
        declaration to it"""

        self.__currscope = self.__scopestack.pop()
        self.__currscope.append(decl)

    def add(self, decl):
        """Adds the given decl to the current scope"""

        self.__currscope.append(decl)

    def currscope(self):
        """Returns the current scope: a list of declarations"""

        return self.__currscope