Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-core-dev] How to parse a C++ header file


To use the parser, all you need to do is put cdtparser.jar in your class path and code away.  

As an example, here is how the CDT Core Model instantiates and uses an IParser to build its model.  

                IProject currentProject = null;
                boolean hasCppNature = true;
                String code = ""; //$NON-NLS-1$
               
                // get the current project
                if (translationUnit != null && translationUnit.getCProject() != null) {
                        currentProject = translationUnit.getCProject().getProject();
                }
                // check the project's nature
                if( currentProject != null )
                {
                        hasCppNature = CoreModel.hasCCNature(currentProject);
                }
                // get the code to parse
                try{
                        code = translationUnit.getBuffer().getContents();
                } catch (CModelException e) {
                       
                }
                // use quick or structural parse mode
                ParserMode mode = quickParseMode ? ParserMode.QUICK_PARSE : ParserMode.STRUCTURAL_PARSE;
                if(quickParseMode)
                        quickParseCallback = ParserFactory.createQuickParseCallback();
                else
                        quickParseCallback = ParserFactory.createStructuralParseCallback();

                // pick the language
                ParserLanguage language = hasCppNature ? ParserLanguage.CPP : ParserLanguage.C;
               
                // create the parser
                IParser parser = null;
                try
                {
                       
                        IScannerInfo scanInfo = new ScannerInfo();
                        IScannerInfoProvider provider = CCorePlugin.getDefault().getScannerInfoProvider(currentProject);
                        if (provider != null){
                                IScannerInfo buildScanInfo = provider.getScannerInformation(currentProject);
                                if (buildScanInfo != null){
                                        scanInfo = new ScannerInfo(buildScanInfo.getDefinedSymbols(), buildScanInfo.getIncludePaths());
                                }
                        }
                       
                        parser = ParserFactory.createParser(
                                ParserFactory.createScanner(
                                        new StringReader( code ),
                                        (translationUnit.getUnderlyingResource() != null ?
                                         translationUnit.getUnderlyingResource().getLocation().toOSString() :
                                         ""), //$NON-NLS-1$
                                        scanInfo,
                                        mode,
                                        language,
                                        quickParseCallback,
                                        quickParseMode ? new NullLogService() : ParserUtil.getScannerLogService(), null)
                                ,quickParseCallback,
                                mode,
                                language,
                                ParserUtil.getParserLogService() );
                }
                catch( ParserFactoryError pfe )
                {
                        throw new ParserException( CCorePlugin.getResourceString("CModelBuilder.Parser_Construction_Failure")); //$NON-NLS-1$
                }
                // call parse
                hasNoErrors = parser.parse();
                if( (!hasNoErrors)  && throwExceptionOnError )
                        throw new ParserException(CCorePlugin.getResourceString("CModelBuilder.Parse_Failure")); //$NON-NLS-1$
                return quickParseCallback.getCompilationUnit();

Here's a quick description of the ParserFactory interface methods you require:

    /**
     * @param scanner                tokenizer to retrieve C/C++ tokens
     * @param callback                the callback that reports results to the client
     * @param mode                        the parser mode you wish to use
     * @param language                C or C++
     * @param log                        a log utility to output errors
     * @return
     * @throws ParserFactoryError - erroneous input provided
     */
    public static IParser createParser( IScanner scanner, ISourceElementRequestor callback, ParserMode mode, ParserLanguage language, IParserLogService log ) throws ParserFactoryError;
                 
    /**
     * @param input                        the java.io.Reader that reads the source-code input you want parsed
     * @param fileName                the absolute path of the file you are parsing (necessary for determining location of local inclusions)
     * @param config                represents the include-paths and preprocessor definitions you wish to initialize the scanner with
     * @param mode                        the parser mode you wish to use
     * @param language                C or C++
     * @param requestor                the callback that reports results to the client
     * @param log                        a log utility to output errors
     * @param workingCopies        a java.util.List of IWorkingCopy buffers if you wish for include files to use CDT Working Copies rather than saved files
     * @return
     * @throws ParserFactoryError - erroneous input provided
     */
    public static IScanner createScanner( Reader input, String fileName, IScannerInfo config, ParserMode mode, ParserLanguage language, ISourceElementRequestor requestor, IParserLogService log, List workingCopies ) throws ParserFactoryError;

For other information you can perhaps attach to the CVS repository @ dev.eclipse.org in order to see the rest of the code.  The repository path is /home/tools and you can attach anonymously to get the source.  

Thanks,
JohnC
www.eclipse.org/cdt


cdt-core-dev-admin@xxxxxxxxxxx wrote on 06/16/2004 10:43:46 PM:

> Hi,
>
> I want to parse the header contents.
>
> Thanks
> Regards,
> Chatura
>
> > want to parse the header contents...or what is coming in the console
> >
> > Chatura Gunawardana <chatura@xxxxxxxxxxxxx> wrote:Hi All,
> >
> > I want to know the way (procedure) of parsing a C++ header file by using
> > CDT C/C++ parser.
> >
> > Another thing i want to know in advance,
> > Is it possible to parse a c++ header file by calling parser externally and
> > if so which way it can do? and
> >
> > How it should parse by loading c++ header on to eclipse?
> >
> > Thanx in advance.
> >
> > Regards,
> > Chatura (Sri Lanka)
> > _______________________________________________
> > cdt-core-dev mailing list
> > cdt-core-dev@xxxxxxxxxxx
> > http://dev.eclipse.org/mailman/listinfo/cdt-core-dev
> >
> >
> >
> > ---------------------------------
> > Do you Yahoo!?
> > Yahoo! Mail is new and improved - Check it out!
>
> _______________________________________________
> cdt-core-dev mailing list
> cdt-core-dev@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/cdt-core-dev

Back to the top