File: Synopsis/Parsers/Cxx/__init__.py
 1#
 2# Copyright (C) 2003 Stefan Seefeld
 3# All rights reserved.
 4# Licensed to the public under the terms of the GNU LGPL (>= 2),
 5# see the file COPYING for details.
 6#
 7
 8"""Parser for C++ using OpenC++ for low-level parsing.
 9This parser is written entirely in C++, and compiled into shared libraries for
10use by python.
11@see C++/Synopsis
12@see C++/SWalker
13"""
14
15from Synopsis.Processor import *
16import ParserImpl
17
18import os, os.path, tempfile
19
20class Parser(Processor):
21
22    preprocess = Parameter(True, 'whether or not to preprocess the input')
23    emulate_compiler = Parameter('', 'a compiler to emulate')
24    compiler_flags = Parameter([], 'list of flags for the emulated compiler')
25    cppflags = Parameter([], 'list of preprocessor flags such as -I or -D')
26    primary_file_only = Parameter(True, 'should only primary file be processed')
27    base_path = Parameter('', 'path prefix to strip off of the file names')
28    sxr_prefix = Parameter(None, 'path prefix (directory) to contain sxr info')
29
30    def process(self, ir, **kwds):
31
32        self.set_parameters(kwds)
33        if not self.input: raise MissingArgument('input')
34        self.ir = ir
35
36        if self.preprocess:
37
38            from Synopsis.Parsers import Cpp
39            cpp = Cpp.Parser(base_path = self.base_path,
40                             language = 'C++',
41                             flags = self.cppflags,
42                             emulate_compiler = self.emulate_compiler,
43                             compiler_flags = self.compiler_flags)
44
45        for file in self.input:
46
47            ii_file = file
48            if self.preprocess:
49
50                if self.output:
51                    ii_file = os.path.splitext(self.output)[0] + '.ii'
52                else:
53                    ii_file = os.path.join(tempfile.gettempdir(),
54                                           'synopsis-%s.ii'%os.getpid())
55                self.ir = cpp.process(self.ir,
56                                      cpp_output = ii_file,
57                                      input = [file],
58                                      primary_file_only = self.primary_file_only,
59                                      verbose = self.verbose,
60                                      debug = self.debug)
61
62            try:
63                self.ir = ParserImpl.parse(self.ir, ii_file,
64                                           os.path.abspath(file),
65                                           self.primary_file_only,
66                                           os.path.abspath(self.base_path) + os.sep,
67                                           self.sxr_prefix,
68                                           self.verbose,
69                                           self.debug)
70            finally:
71                if self.preprocess: os.remove(ii_file)
72
73        return self.output_and_return_ir()
74