gobject-introspection r174 - in trunk: . giscanner tools
- From: johan svn gnome org
- To: svn-commits-list gnome org
- Subject: gobject-introspection r174 - in trunk: . giscanner tools
- Date: Fri, 18 Apr 2008 21:20:06 +0100 (BST)
Author: johan
Date: Fri Apr 18 20:20:06 2008
New Revision: 174
URL: http://svn.gnome.org/viewvc/gobject-introspection?rev=174&view=rev
Log:
2008-04-18 Johan Dahlin <johan gnome org>
* giscanner/sourcescanner.py:
* giscanner/treebuilder.py:
* tools/g-ir-scanner:
split tree building and source scanning interface to separate files.
Added:
trunk/giscanner/sourcescanner.py (contents, props changed)
trunk/giscanner/treebuilder.py (contents, props changed)
Modified:
trunk/ChangeLog
trunk/tools/g-ir-scanner
Added: trunk/giscanner/sourcescanner.py
==============================================================================
--- (empty file)
+++ trunk/giscanner/sourcescanner.py Fri Apr 18 20:20:06 2008
@@ -0,0 +1,72 @@
+import os
+import subprocess
+
+import giscanner
+
+class SourceScanner(object):
+ def __init__(self):
+ self._scanner = giscanner.SourceScanner()
+ self._filenames = []
+ self._cpp_options = []
+
+ # Public API
+
+ def set_cpp_options(self, includes, defines, undefines):
+ for prefix, args in [('-I', includes),
+ ('-D', defines),
+ ('-U', undefines)]:
+ for arg in (args or []):
+ self._cpp_options.append(prefix + arg)
+
+ def parse_file(self, filename):
+ self._parse_one(filename)
+ self._filenames.append(filename)
+
+ def parse_macros(self):
+ self._scanner.set_macro_scan(True)
+ for filename in self._filenames:
+ self._parse_one(filename)
+ self._scanner.set_macro_scan(False)
+
+ def get_symbols(self):
+ for symbol in self._scanner.get_symbols():
+ yield symbol
+
+ def dump(self):
+ print '-'*30
+ for symbol in self._scanner.get_symbols():
+ print symbol.ident, symbol.base_type.name, symbol.type
+
+ # Private
+
+ def _parse_one(self, filename):
+ if filename.endswith('.c'):
+ # FIXME lex only
+ return
+
+ filename = os.path.abspath(filename)
+ proc = self._preprocess(filename)
+ fd = proc.stdout.fileno()
+ if proc is None:
+ return
+
+ self._scanner.parse_file(fd, filename)
+
+ def _preprocess(self, filename):
+ cpp_args = [
+ 'cpp',
+ '-C',
+ '-U__GNUC__',
+ '-I.',
+ ]
+ cpp_args += self._cpp_options
+ proc = subprocess.Popen(
+ cpp_args,
+ bufsize=4096,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ )
+ proc.stdin.write('#include <%s>\n' % (filename,))
+ proc.stdin.close()
+ return proc
Added: trunk/giscanner/treebuilder.py
==============================================================================
--- (empty file)
+++ trunk/giscanner/treebuilder.py Fri Apr 18 20:20:06 2008
@@ -0,0 +1,126 @@
+import giscanner
+
+class Node(object):
+ pass
+
+
+class Function(Node):
+ def __init__(self, name, retval, parameters):
+ self.name = name
+ self.retval = retval
+ self.parameters = parameters
+
+ def __repr__(self):
+ return 'Function(%r, %r, %r)' % (self.name, self.retval, self.parameters)
+
+
+class Parameter(Node):
+ def __init__(self, name, type):
+ self.name = name
+ self.type = type
+
+ def __repr__(self):
+ return 'Parameter(%r, %r)' % (self.name, self.type)
+
+
+class Enum(Node):
+ def __init__(self, name, members):
+ self.name = name
+ self.members = members
+
+ def __repr__(self):
+ return 'Enum(%r, %r)' % (self.name, self.members)
+
+
+class Member(Node):
+ def __init__(self, name, value):
+ self.name = name
+ self.value = value
+
+ def __repr__(self):
+ return 'Member(%r, %r)' % (self.name, self.value)
+
+
+class Struct(Node):
+ def __init__(self, name):
+ self.name = name
+
+ def __repr__(self):
+ return 'Struct(%r)' % (self.name,)
+
+
+class Return(Node):
+ def __init__(self, type):
+ self.type = type
+
+ def __repr__(self):
+ return 'Return(%r)' % (self.type,)
+
+
+class TreeBuilder(object):
+ def __init__(self, generator):
+ self.generator = generator
+ self.nodes = []
+
+ self._traverse()
+
+ def get_nodes(self):
+ for node in self.nodes:
+ yield node
+
+ def _traverse(self):
+ for symbol in self.generator.get_symbols():
+ node = self._traverse_one(symbol)
+ if node is not None:
+ self.nodes.append(node)
+
+ def _traverse_one(self, symbol, stype=None):
+ if stype is None:
+ stype = symbol.type
+ if stype == giscanner.CSYMBOL_TYPE_FUNCTION:
+ return self._create_function(symbol)
+ elif stype == giscanner.CSYMBOL_TYPE_TYPEDEF:
+ return self._traverse_one(symbol, symbol.base_type.type)
+ elif stype == giscanner.CSYMBOL_TYPE_STRUCT:
+ return self._create_struct(symbol)
+ elif stype == giscanner.CSYMBOL_TYPE_ENUM:
+ return self._create_enum(symbol)
+ else:
+ print 'unhandled symbol', symbol.type
+
+ def _create_enum(self, symbol):
+ members = []
+ for child in symbol.base_type.child_list:
+ members.append(Member(child.ident,
+ child.const_int))
+
+ return Enum(symbol.ident, members)
+
+ def _create_function(self, symbol):
+ parameters = []
+ for child in symbol.base_type.child_list:
+ parameters.append(self._create_parameter(child))
+
+ retval = Return(self._create_source_type(symbol.base_type.base_type))
+ return Function(symbol.ident, retval, parameters)
+
+ def _create_source_type(self, source_type):
+ if source_type.type == giscanner.CTYPE_VOID:
+ value = 'void'
+ elif source_type.type == giscanner.CTYPE_BASIC_TYPE:
+ value = source_type.name
+ elif source_type.type == giscanner.CTYPE_TYPEDEF:
+ value = source_type.name
+ elif source_type.type == giscanner.CTYPE_POINTER:
+ value = self._create_source_type(source_type.base_type) + '*'
+ else:
+ print 'Unhandled source type: %d' % (source_type.type,)
+ value = '???'
+ return value
+
+ def _create_parameter(self, symbol):
+ return Parameter(symbol.ident,
+ self._create_source_type(symbol.base_type))
+
+ def _create_struct(self, symbol):
+ return Struct(symbol.ident)
Modified: trunk/tools/g-ir-scanner
==============================================================================
--- trunk/tools/g-ir-scanner (original)
+++ trunk/tools/g-ir-scanner Fri Apr 18 20:20:06 2008
@@ -1,208 +1,11 @@
#!/usr/bin/env python
import optparse
-import os
-import subprocess
import sys
sys.path.insert(0, '.')
-import giscanner
-
-
-class Node(object):
- pass
-
-
-class Function(Node):
- def __init__(self, name, retval, parameters):
- self.name = name
- self.retval = retval
- self.parameters = parameters
-
- def __repr__(self):
- return 'Function(%r, %r, %r)' % (self.name, self.retval, self.parameters)
-
-
-class Parameter(Node):
- def __init__(self, name, type):
- self.name = name
- self.type = type
-
- def __repr__(self):
- return 'Parameter(%r, %r)' % (self.name, self.type)
-
-
-class Enum(Node):
- def __init__(self, name, members):
- self.name = name
- self.members = members
-
- def __repr__(self):
- return 'Enum(%r, %r)' % (self.name, self.members)
-
-
-class Member(Node):
- def __init__(self, name, value):
- self.name = name
- self.value = value
-
- def __repr__(self):
- return 'Member(%r, %r)' % (self.name, self.value)
-
-
-class Struct(Node):
- def __init__(self, name):
- self.name = name
-
- def __repr__(self):
- return 'Struct(%r)' % (self.name,)
-
-
-class Return(Node):
- def __init__(self, type):
- self.type = type
-
- def __repr__(self):
- return 'Return(%r)' % (self.type,)
-
-
-class Generator(object):
- def __init__(self):
- self._scanner = giscanner.SourceScanner()
- self._filenames = []
- self._cpp_options = []
-
- # Public API
-
- def set_cpp_options(self, includes, defines, undefines):
- for prefix, args in [('-I', includes),
- ('-D', defines),
- ('-U', undefines)]:
- for arg in (args or []):
- self._cpp_options.append(prefix + arg)
-
- def parse_file(self, filename):
- self._parse_one(filename)
- self._filenames.append(filename)
-
- def parse_macros(self):
- self._scanner.set_macro_scan(True)
- for filename in self._filenames:
- self._parse_one(filename)
- self._scanner.set_macro_scan(False)
-
- def get_symbols(self):
- for symbol in self._scanner.get_symbols():
- yield symbol
-
- def dump(self):
- print '-'*30
- for symbol in self._scanner.get_symbols():
- print symbol.ident, symbol.base_type.name, symbol.type
-
- # Private
-
- def _parse_one(self, filename):
- if filename.endswith('.c'):
- # FIXME lex only
- return
-
- filename = os.path.abspath(filename)
- proc = self._preprocess(filename)
- fd = proc.stdout.fileno()
- if proc is None:
- return
-
- self._scanner.parse_file(fd, filename)
-
- def _preprocess(self, filename):
- cpp_args = [
- 'cpp',
- '-C',
- '-U__GNUC__',
- '-I.',
- ]
- cpp_args += self._cpp_options
- proc = subprocess.Popen(
- cpp_args,
- bufsize=4096,
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT,
- )
- proc.stdin.write('#include <%s>\n' % (filename,))
- proc.stdin.close()
- return proc
-
-
-class TreeBuilder(object):
- def __init__(self, generator):
- self.generator = generator
- self.nodes = []
-
- self._traverse()
-
- def get_nodes(self):
- for node in self.nodes:
- yield node
-
- def _traverse(self):
- for symbol in self.generator.get_symbols():
- node = self._traverse_one(symbol)
- if node is not None:
- self.nodes.append(node)
-
- def _traverse_one(self, symbol, stype=None):
- if stype is None:
- stype = symbol.type
- if stype == giscanner.CSYMBOL_TYPE_FUNCTION:
- return self._create_function(symbol)
- elif stype == giscanner.CSYMBOL_TYPE_TYPEDEF:
- return self._traverse_one(symbol, symbol.base_type.type)
- elif stype == giscanner.CSYMBOL_TYPE_STRUCT:
- return self._create_struct(symbol)
- elif stype == giscanner.CSYMBOL_TYPE_ENUM:
- return self._create_enum(symbol)
- else:
- print 'unhandled symbol', symbol.type
-
- def _create_enum(self, symbol):
- members = []
- for child in symbol.base_type.child_list:
- members.append(Member(child.ident,
- child.const_int))
-
- return Enum(symbol.ident, members)
-
- def _create_function(self, symbol):
- parameters = []
- for child in symbol.base_type.child_list:
- parameters.append(self._create_parameter(child))
-
- retval = Return(self._create_source_type(symbol.base_type.base_type))
- return Function(symbol.ident, retval, parameters)
-
- def _create_source_type(self, source_type):
- if source_type.type == giscanner.CTYPE_VOID:
- value = 'void'
- elif source_type.type == giscanner.CTYPE_BASIC_TYPE:
- value = source_type.name
- elif source_type.type == giscanner.CTYPE_TYPEDEF:
- value = source_type.name
- elif source_type.type == giscanner.CTYPE_POINTER:
- value = self._create_source_type(source_type.base_type) + '*'
- else:
- print 'Unhandled source type: %d' % (source_type.type,)
- value = '???'
- return value
-
- def _create_parameter(self, symbol):
- return Parameter(symbol.ident,
- self._create_source_type(symbol.base_type))
-
- def _create_struct(self, symbol):
- return Struct(symbol.ident)
-
+from giscanner.sourcescanner import SourceScanner
+from giscanner.treebuilder import TreeBuilder
def main(args):
parser = optparse.OptionParser('%prog [options] sources')
@@ -226,10 +29,10 @@
print 'ERROR: Needs at least one filename.'
return 0
- gen = Generator()
- gen.set_cpp_options(options.cpp_includes,
- options.cpp_defines,
- options.cpp_undefines)
+ ss = SourceScanner()
+ ss.set_cpp_options(options.cpp_includes,
+ options.cpp_defines,
+ options.cpp_undefines)
filenames = []
for arg in args:
if (arg.endswith('.c') or
@@ -237,11 +40,12 @@
filenames.append(arg)
for filename in filenames:
- gen.parse_file(filename)
- gen.parse_macros()
+ ss.parse_file(filename)
+ ss.parse_macros()
- builder = TreeBuilder(gen)
+ builder = TreeBuilder(ss)
import pprint
pprint.pprint(list(builder.get_nodes()))
+
sys.exit(main(sys.argv))
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]