[pygccxml-commit] SF.net SVN: pygccxml: [1046] pygccxml_dev/pygccxml/parser
Brought to you by:
mbaas,
roman_yakovenko
|
From: <rom...@us...> - 2007-05-27 20:21:21
|
Revision: 1046
http://svn.sourceforge.net/pygccxml/?rev=1046&view=rev
Author: roman_yakovenko
Date: 2007-05-27 13:21:17 -0700 (Sun, 27 May 2007)
Log Message:
-----------
adding sceleton of synopsis parser
Modified Paths:
--------------
pygccxml_dev/pygccxml/parser/__init__.py
pygccxml_dev/pygccxml/parser/source_reader.py
Added Paths:
-----------
pygccxml_dev/pygccxml/parser/synopsis_reader.py
pygccxml_dev/pygccxml/parser/synopsis_scanner.py
Modified: pygccxml_dev/pygccxml/parser/__init__.py
===================================================================
--- pygccxml_dev/pygccxml/parser/__init__.py 2007-05-27 19:16:22 UTC (rev 1045)
+++ pygccxml_dev/pygccxml/parser/__init__.py 2007-05-27 20:21:17 UTC (rev 1046)
@@ -7,6 +7,10 @@
"""
from config import config_t
+from config import gccxml_configuration_t
+from config import synopsis_configuration_t
+
+
from project_reader import COMPILATION_MODE
from project_reader import project_reader_t
from project_reader import file_configuration_t
@@ -56,4 +60,4 @@
def parse_xml_file( content, config=None ):
parser = source_reader_t( config )
- return parser.read_xml_file( content )
\ No newline at end of file
+ return parser.read_xml_file( content )
Modified: pygccxml_dev/pygccxml/parser/source_reader.py
===================================================================
--- pygccxml_dev/pygccxml/parser/source_reader.py 2007-05-27 19:16:22 UTC (rev 1045)
+++ pygccxml_dev/pygccxml/parser/source_reader.py 2007-05-27 20:21:17 UTC (rev 1046)
@@ -5,9 +5,10 @@
import os
import sys
+import linker
import config
+import patcher
import pygccxml.utils
-import linker
try: #select faster xml parser
from etree_scanner import etree_scanner_t as scanner_t
@@ -15,9 +16,8 @@
from scanner import scanner_t
import declarations_cache
-import patcher
-from pygccxml.declarations import *
from pygccxml import utils
+from pygccxml.declarations import *
class gccxml_runtime_error_t( RuntimeError ):
def __init__( self, msg ):
@@ -77,7 +77,7 @@
@param decl_factory: declarations factory, if not given default
declarations factory L{decl_factory_t} will be used
"""
- self.logger = utils.loggers.gccxml
+ self.logger = utils.loggers.cxx_parser
self.__search_directories = []
self.__config = config
self.__search_directories.append( config.working_directory )
@@ -85,44 +85,11 @@
if not cache:
cache = declarations_cache.dummy_cache_t()
self.__dcache = cache
- self.__raise_on_wrong_settings()
+ self.__config.raise_on_wrong_settings()
self.__decl_factory = decl_factory
if not decl_factory:
self.__decl_factory = decl_factory_t()
- def __raise_on_wrong_settings(self):
- if not os.path.isfile( self.__config.gccxml_path ):
- if sys.platform == 'win32':
- gccxml_name = 'gccxml' + '.exe'
- environment_var_delimiter = ';'
- elif sys.platform == 'linux2' or sys.platform == 'darwin':
- gccxml_name = 'gccxml'
- environment_var_delimiter = ':'
- else:
- raise RuntimeError( 'unable to find out location of gccxml' )
- may_be_gccxml = os.path.join( self.__config.gccxml_path, gccxml_name )
- if os.path.isfile( may_be_gccxml ):
- self.__config.gccxml_path = may_be_gccxml
- else:
- for path in os.environ['PATH'].split( environment_var_delimiter ):
- gccxml_path = os.path.join( path, gccxml_name )
- if os.path.isfile( gccxml_path ):
- self.__config.gccxml_path = gccxml_path
- break
- else:
- msg = 'gccxml_path("%s") should exists or to be a valid file name.' \
- % self.__config.gccxml_path
- raise RuntimeError( msg )
- if not os.path.isdir( self.__config.working_directory ):
- msg = 'working_directory("%s") should exists or to be a valid directory name.' \
- % self.__config.working_directory
- raise RuntimeError( msg )
- for include_path in self.__config.include_paths:
- if not os.path.isdir( include_path ):
- msg = 'include path "%s" should exists or to be a valid directory name.' \
- % include_path
- raise RuntimeError( msg )
-
def __create_command_line(self, file, xmlfile):
assert isinstance( self.__config, config.config_t )
#returns
@@ -222,8 +189,14 @@
finally:
pygccxml.utils.remove_file_no_raise( header_file )
return gccxml_file
-
- def read_file(self, source_file):
+
+ def read_file( self, source_file ):
+ if isinstance( self.__config, config.gccxml_configuration_t ):
+ return self.read_gccxml_file( source_file )
+ else:
+ return self.read_synopsis_file( source_file )
+
+ def read_gccxml_file(self, source_file):
"""
Reads C++ source file and returns declarations tree
@@ -342,4 +315,28 @@
decls = filter( lambda inst: isinstance( inst, namespace_t ) and not inst.parent
, decls.itervalues() )
return ( decls, files.values() )
+
+ def read_synopsis_file( self, source_file ):
+ import synopsis_scanner
+ from Synopsis import AST
+ from Synopsis.Parsers import Cxx
+ ffname = self.__file_full_name(source_file)
+
+ cppflags = []
+ map( lambda dpath: cppflags.append( '-I %s' % dpath )
+ , self.__config.include_paths )
+ map( lambda define: cppflags.append( '-D %s' % define )
+ , self.__config.define_symbols )
+ map( lambda define: cppflags.append( '-U %s' % define )
+ , self.__config.undefine_symbols )
+
+ cxx = Cxx.Parser( preprocess=True, cppflags=cppflags )
+ ast = AST.AST()
+ cxx.process( ast, input=[source_file] )
+ scanner = synopsis_scanner.scanner_t( ast, self.__decl_factory )
+ scanner.visitAST( ast )
+ declarations = [scanner.global_ns]
+ self.__dcache.update( ffname, self.__config, declarations, [] )
+ return declarations
+
Added: pygccxml_dev/pygccxml/parser/synopsis_reader.py
===================================================================
--- pygccxml_dev/pygccxml/parser/synopsis_reader.py (rev 0)
+++ pygccxml_dev/pygccxml/parser/synopsis_reader.py 2007-05-27 20:21:17 UTC (rev 1046)
@@ -0,0 +1,36 @@
+import os
+import sys
+
+from Synopsis import AST
+from Synopsis.Parsers import Cxx
+
+headers_dir = '/home/roman/language-binding/sources/pygccxml_dev/unittests/data'
+
+offset = 0
+def print_decls( d ):
+ global offset
+ print offset * ' ', d.name(), d.__class__.__name__
+ if hasattr( d, 'declarations' ):
+ offset += 1
+ for d1 in d.declarations():
+ print_decls( d1 )
+ offset -= 1
+
+def parse( f ):
+ global offset, headers_dir
+ print 'file ', f
+ cxx = Cxx.Parser(
+ preprocess=True
+ , cppflags=['-I %s' % headers_dir ] )
+
+ ast = AST.AST()
+ cxx.process( ast, input=[os.path.join(headers_dir, f )] )
+
+ offset = 0
+ for d in ast.declarations():
+ print_decls( d )
+
+parse( 'declarations_enums.hpp' )
+#for x in os.listdir( headers_dir ):
+ #if x.endswith( 'hpp' ):
+ #parse( x )
Added: pygccxml_dev/pygccxml/parser/synopsis_scanner.py
===================================================================
--- pygccxml_dev/pygccxml/parser/synopsis_scanner.py (rev 0)
+++ pygccxml_dev/pygccxml/parser/synopsis_scanner.py 2007-05-27 20:21:17 UTC (rev 1046)
@@ -0,0 +1,46 @@
+# Copyright 2004 Roman Yakovenko.
+# Distributed under the Boost Software License, Version 1.0. (See
+# accompanying file LICENSE_1_0.txt or copy at
+# http://www.boost.org/LICENSE_1_0.txt)
+
+import os
+import types
+import pprint
+import warnings
+from Synopsis import AST
+from pygccxml import utils
+from pygccxml.declarations import *
+
+class scanner_t( AST.Visitor, object ):
+ def __init__(self, ast, decl_factory ):
+ self.logger = utils.loggers.cxx_parser
+ self.ast = ast
+
+ assert isinstance( decl_factory, decl_factory_t )
+ self.__decl_factory = decl_factory
+
+ #mapping from id -> decl
+ self.__decl = self.__decl_factory.create_namespace( name='::' )
+ self.global_ns = self.__decl
+
+
+ def read_deaclaration( self, node, decl ):
+ #this function should not be called for namespace
+ decl.name = node.name()
+ decl.location = location_t( file_name=node.file(), line=node.line() )
+
+ def visitModule( self, node ):
+ ns = self.__decl_factory.create_namespace( name=node.name() )
+ self.__decl.adopt_declaration( ns )
+ self.__decl = ns
+ super( scanner_t, self ).visitModule( node )
+
+ def visitEnum( self, node ):
+ values = []
+ for enumerator in node.enumerators():
+ print enumerator.name(), ':', enumerator.value()
+ values.append( ( enumerator.name(), enumerator.value() ) )
+ enum = self.__decl_factory.create_enumeration( values=values )
+ self.read_deaclaration( node, enum )
+ self.__decl.adopt_declaration( enum )
+ super( scanner_t, self ).visitEnum( node )
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|