3 Copyright 2007 Antoine Chavasse <a.chavasse@gmail.com>
5 This file is part of Fail.
7 Fail is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 3
9 as published by the Free Software Foundation.
11 Fail is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "core/idlparser/parser.h"
24 #include <boost/wave.hpp>
25 #include <boost/wave/cpplexer/cpp_lex_token.hpp>
26 #include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
29 #include "astbuilder.h"
32 using namespace fail::idlparser
;
33 using namespace boost::spirit
;
35 fail::Pointer
< fail::idlast::AST
> fail::idlparser::Parse( const char* pFilename_
)
39 std::ifstream
file( pFilename_
);
44 std::string
loadedfile( std::istreambuf_iterator
< char >( file
.rdbuf() ),
45 std::istreambuf_iterator
< char >() );
47 loadedfile
.append( "\n" );
49 // Handy typedefs to setup the wave context.
50 typedef boost::wave::cpplexer::lex_token
<> token_type
;
51 typedef boost::wave::cpplexer::lex_iterator
< token_type
> lex_iterator_type
;
52 typedef boost::wave::context
< std::string::iterator
, lex_iterator_type
> context_type
;
54 context_type
wavectx( loadedfile
.begin(), loadedfile
.end(), pFilename_
);
55 TokTransIterator
< context_type::iterator_type
, token_type
>
56 tok_begin( wavectx
.begin() ), tok_end( wavectx
.end() );
58 Pointer
< idlast::AST
> pAST
= new idlast::AST
;
59 ASTBuilder
Builder( pAST
);
61 idlgrammar
< ASTBuilder
> Parser( Builder
);
63 parse_info
< TokTransIterator
< context_type::iterator_type
, token_type
> > info
= parse(
68 // Inline definition of the skip parser. Basically skip
69 // everything considered whitespace, plus the lines starting with
70 // #line that are inserted by wave (we don't need them
71 // because the tokens returned by wave contain location
72 // informations anyway)
78 |( ch_p( T_PP_LINE
) >> *( ~ch_p( T_NEWLINE
) ) )
88 catch( boost::wave::cpp_exception
const& e
)
90 std::stringstream err
;
91 err
<< e
.file_name() << ':' << e
.line_no() << ": " << e
.description();
92 throw std::runtime_error( err
.str().c_str() );