File: Synopsis/Parsers/C/__init__.py
 1#
 2# Copyright (C) 2004 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
 8from Synopsis.Processor import *
 9import ParserImpl
10
11import os, os.path, tempfile
12
13class Parser(Processor):
14
15    preprocess = Parameter(True, 'whether or not to preprocess the input')
16    emulate_compiler = Parameter('cc', 'a compiler to emulate')
17    compiler_flags = Parameter([], 'list of flags for the emulated compiler')
18    cppflags = Parameter([], 'list of preprocessor flags such as -I or -D')
19    primary_file_only = Parameter(True, 'should only primary file be processed')
20    base_path = Parameter('', 'path prefix to strip off of the file names')
21    sxr_prefix = Parameter(None, 'path prefix (directory) to contain syntax info')
22
23    def process(self, ir, **kwds):
24
25        self.set_parameters(kwds)
26        if not self.input: raise MissingArgument('input')
27        self.ir = ir
28
29        if self.preprocess:
30
31            from Synopsis.Parsers import Cpp
32            cpp = Cpp.Parser(base_path = self.base_path,
33                             language = 'C',
34                             flags = self.cppflags,
35                             emulate_compiler = self.emulate_compiler,
36                             compiler_flags = self.compiler_flags)
37
38        base_path = self.base_path and os.path.abspath(self.base_path) + os.sep or ''
39
40        for file in self.input:
41
42            i_file = file
43            if self.preprocess:
44
45                if self.output:
46                    i_file = os.path.splitext(self.output)[0] + '.i'
47                else:
48                    i_file = os.path.join(tempfile.gettempdir(),
49                                          'synopsis-%s.i'%os.getpid())
50                self.ir = cpp.process(self.ir,
51                                      cpp_output = i_file,
52                                      input = [file],
53                                      primary_file_only = self.primary_file_only,
54                                      verbose = self.verbose,
55                                      debug = self.debug)
56            try:
57                self.ir = ParserImpl.parse(self.ir, i_file,
58                                           os.path.abspath(file),
59                                           self.primary_file_only,
60                                           base_path,
61                                           self.sxr_prefix,
62                                           self.verbose,
63                                           self.debug)
64            finally:
65                if self.preprocess: os.remove(i_file)
66
67        return self.output_and_return_ir()
68