From 772c327da78e4c5eb34d8c34d910e6de77f57dc5 Mon Sep 17 00:00:00 2001 From: Johan Dahlin Date: Sat, 3 May 2008 15:09:38 +0000 Subject: [PATCH] Refactor and comment scanner frontend svn path=/trunk/; revision=264 --- tools/g-ir-scanner | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/tools/g-ir-scanner b/tools/g-ir-scanner index 353e985..760c9d3 100755 --- a/tools/g-ir-scanner +++ b/tools/g-ir-scanner @@ -30,7 +30,7 @@ from giscanner.sourcescanner import SourceScanner from giscanner.transformer import Transformer -def main(args): +def _get_option_parser(): parser = optparse.OptionParser('%prog [options] sources') parser.add_option("", "--format", action="store", dest="format", @@ -71,15 +71,27 @@ def main(args): group.add_option("-p", dest="", help="Ignored") parser.add_option_group(group) + return parser + +def _error(msg): + raise SystemExit('ERROR: %s' % (msg,)) + +def main(args): + parser = _get_option_parser() (options, args) = parser.parse_args(args) if len(args) <= 1: - print 'ERROR: Need at least one filename.' - return 1 + _error('Need at least one filename') if not options.namespace: - print 'ERROR: Namespace missing.' - return 1 + _error('Namespace missing') + + if options.format == 'gir': + from giscanner.girwriter import GIRWriter as Writer + elif options.format == 'gidl': + from giscanner.gidlwriter import GIDLWriter as Writer + else: + _error("Unknown format: %s" % (options.format,)) for package in options.packages: output = commands.getoutput('pkg-config --cflags %s' % (package,)) @@ -88,25 +100,29 @@ def main(args): options.cpp_defines.extend(pkg_options.cpp_defines) options.cpp_undefines.extend(pkg_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 arg.endswith('.h')): if not os.path.exists(arg): - print 'ERROR: %s: no such a file or directory' % (arg,) - return 1 + _error('%s: no such a file or directory' % (arg,)) filenames.append(arg) + # Run the preprocessor, tokenize and construct simple + # objects representing the raw C symbols + ss = SourceScanner() + ss.set_cpp_options(options.cpp_includes, + options.cpp_defines, + options.cpp_undefines) ss.parse_files(filenames) ss.parse_macros() + # Transform the C symbols into AST nodes transformer = Transformer(ss) transformer.set_strip_prefix(options.strip_prefix) + # Transform the C AST nodes into higher level + # GLib/GObject nodes glibtransformer = GLibTransformer(options.namespace) if options.library: glibtransformer.load_library(options.library) @@ -117,13 +133,7 @@ def main(args): nodes = transformer.get_nodes() glibtransformer.parse(nodes) - if options.format == 'gir': - from giscanner.girwriter import GIRWriter as Writer - elif options.format == 'gidl': - from giscanner.gidlwriter import GIDLWriter as Writer - else: - raise SystemExit("Unknown format: %s" % (options.format,)) - + # Write out AST writer = Writer(options.namespace, glibtransformer.get_nodes()) data = writer.get_xml() if options.output: @@ -132,4 +142,6 @@ def main(args): else: print data + return 0 + sys.exit(main(sys.argv)) -- 2.7.4