6 #include <boost/wave.hpp>
7 #include <boost/wave/cpplexer/cpp_lex_token.hpp>
8 #include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
11 #include "astbuilder.h"
14 using namespace awful::idlparser
;
15 using namespace boost::spirit
;
17 awful::Pointer
< awful::idlast::AST
> awful::idlparser::Parse( const char* pFilename_
)
21 std::ifstream
file( pFilename_
);
26 std::string
loadedfile( std::istreambuf_iterator
< char >( file
.rdbuf() ),
27 std::istreambuf_iterator
< char >() );
29 loadedfile
.append( "\n" );
31 // Handy typedefs to setup the wave context.
32 typedef boost::wave::cpplexer::lex_token
<> token_type
;
33 typedef boost::wave::cpplexer::lex_iterator
< token_type
> lex_iterator_type
;
34 typedef boost::wave::context
< std::string::iterator
, lex_iterator_type
> context_type
;
36 context_type
wavectx( loadedfile
.begin(), loadedfile
.end(), pFilename_
);
37 TokTransIterator
< context_type::iterator_type
, token_type
>
38 tok_begin( wavectx
.begin() ), tok_end( wavectx
.end() );
40 Pointer
< idlast::AST
> pAST
= new idlast::AST
;
41 ASTBuilder
Builder( pAST
);
43 idlgrammar
< ASTBuilder
> Parser( Builder
);
45 parse_info
< TokTransIterator
< context_type::iterator_type
, token_type
> > info
= parse(
50 // Inline definition of the skip parser. Basically skip
51 // everything considered whitespace, plus the lines starting with
52 // #line that are inserted by wave (we don't need them
53 // because the tokens returned by wave contain location
54 // informations anyway)
60 |( ch_p( T_PP_LINE
) >> *( ~ch_p( T_NEWLINE
) ) )
70 catch( boost::wave::cpp_exception
const& e
)
72 std::stringstream err
;
73 err
<< e
.file_name() << ':' << e
.line_no() << ": " << e
.description();
74 throw std::runtime_error( err
.str().c_str() );