Added GPLv3 headers all over the place.
[fail.git] / src / core / idlparser / parser.cpp
bloba587ef9573fc004c2a88729172ea289608f490d1
1 /*
2 Fail game engine
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"
20 #include <iostream>
21 #include <fstream>
22 #include <iterator>
24 #include <boost/wave.hpp>
25 #include <boost/wave/cpplexer/cpp_lex_token.hpp>
26 #include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
28 #include "grammar.h"
29 #include "astbuilder.h"
30 #include "toktrans.h"
32 using namespace fail::idlparser;
33 using namespace boost::spirit;
35 fail::Pointer< fail::idlast::AST > fail::idlparser::Parse( const char* pFilename_ )
37 try
39 std::ifstream file( pFilename_ );
41 if( !file.good() )
42 return 0;
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(
64 tok_begin,
65 tok_end,
66 Parser,
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)
73 +( ch_p( T_CCOMMENT )
74 |ch_p( T_CPPCOMMENT )
75 |ch_p( T_SPACE )
76 |ch_p( T_SPACE2 )
77 |ch_p( T_NEWLINE )
78 |( ch_p( T_PP_LINE ) >> *( ~ch_p( T_NEWLINE ) ) )
82 if( !info.full )
83 return NULL;
85 pAST->resolveRefs();
86 return pAST;
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() );