Reshuffling directories to separate include and sources, part one.
[fail.git] / include / core / idlparser / parser.cpp
blob3b3c06caea97a7c37a5cf5555fb2d09b92f4f02f
1 #include "parser.h"
2 #include <iostream>
3 #include <fstream>
4 #include <iterator>
6 #include <boost/wave.hpp>
7 #include <boost/wave/cpplexer/cpp_lex_token.hpp>
8 #include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
10 #include "grammar.h"
11 #include "astbuilder.h"
12 #include "toktrans.h"
14 using namespace awful::idlparser;
15 using namespace boost::spirit;
17 awful::Pointer< awful::idlast::AST > awful::idlparser::Parse( const char* pFilename_ )
19 try
21 std::ifstream file( pFilename_ );
23 if( !file.good() )
24 return 0;
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(
46 tok_begin,
47 tok_end,
48 Parser,
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)
55 +( ch_p( T_CCOMMENT )
56 |ch_p( T_CPPCOMMENT )
57 |ch_p( T_SPACE )
58 |ch_p( T_SPACE2 )
59 |ch_p( T_NEWLINE )
60 |( ch_p( T_PP_LINE ) >> *( ~ch_p( T_NEWLINE ) ) )
64 if( !info.full )
65 return NULL;
67 pAST->resolveRefs();
68 return pAST;
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() );