static_api_parser.hpp

説明を見る。
00001 /*
00002  *  TOPPERS/FDMP Kernel
00003  *      Toyohashi Open Platform for Embedded Real-Time Systems/
00004  *      Function Distributed Multiprocessor Kernel
00005  *
00006  *  Copyright (C) 2005 by Takagi Nobuhisa
00007  * 
00008  *  上記著作権者は,以下の (1)〜(4) の条件か,Free Software Foundation 
00009  *  によって公表されている GNU General Public License の Version 2 に記
00010  *  述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
00011  *  を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
00012  *  利用と呼ぶ)することを無償で許諾する.
00013  *  (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
00014  *      権表示,この利用条件および下記の無保証規定が,そのままの形でソー
00015  *      スコード中に含まれていること.
00016  *  (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
00017  *      用できる形で再配布する場合には,再配布に伴うドキュメント(利用
00018  *      者マニュアルなど)に,上記の著作権表示,この利用条件および下記
00019  *      の無保証規定を掲載すること.
00020  *  (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
00021  *      用できない形で再配布する場合には,次のいずれかの条件を満たすこ
00022  *      と.
00023  *    (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
00024  *        作権表示,この利用条件および下記の無保証規定を掲載すること.
00025  *    (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
00026  *        報告すること.
00027  *  (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
00028  *      害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
00029  * 
00030  *  本ソフトウェアは,無保証で提供されているものである.上記著作権者お
00031  *  よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
00032  *  含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
00033  *  接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
00034  * 
00035  */
00036 
00048 #ifndef TOPPERS_STATIC_API_PARSER_HPP_
00049 #define TOPPERS_STATIC_API_PARSER_HPP_
00050 
00051 #include "toppers/itron/static_api.hpp"
00052 #include "toppers/c_expr.hpp"
00053 #include <boost/spirit/error_handling.hpp>
00054 
00055 namespace toppers
00056 {
00057   namespace itron
00058   {
00059 
00064     struct static_api_parser : boost::spirit::grammar<static_api_parser>
00065     {
00066       enum rule_id_t
00067       {
00068         id_top = 1, id_api_name, id_parameter_list, id_parameter, id_packet, id_cexpr
00069       };
00070       enum expected_t
00071       {
00072         open_paren_expected, close_paren_expected, open_brace_expected, close_brace_expected,
00073         comma_expected, semicolon_expected
00074       };
00075 
00080       struct error_handler
00081       {
00082         template <class Scanner, class Error>
00083         boost::spirit::error_status<> operator()( const Scanner& scan, const Error& error ) const
00084         {
00085           typename Error::iterator_t iter( error.where );
00086           while ( *iter != '\0' && *iter != '\n' )
00087           {
00088             ++iter;
00089           }
00090           std::string str( error.where, iter );
00091           str = '\"' + str + '\"';
00092 
00093           text_line ln( get_text_line( error.where ) );
00094           switch ( error.descriptor )
00095           {
00096           case open_paren_expected:
00097             toppers::error( ln, _( "missing `%1%\' before %2%" ) % '(' % str );
00098             break;
00099           case close_paren_expected:
00100             toppers::error( ln, _( "missing `%1%\' before %2%" ) % ')' % str );
00101             break;
00102           case close_brace_expected:
00103             toppers::error( ln, _( "missing `%1%\' before %2%" ) % '}' % str );
00104             break;
00105           case semicolon_expected:
00106             toppers::error( ln, _( "missing `%1%\' before %2%" ) % ';' % str );
00107             break;
00108           }
00109           return  boost::spirit::error_status<>( boost::spirit::error_status<>::fail );
00110         }
00111       };
00112 
00117       template <class Scanner>
00118       struct definition
00119       {
00120         typedef boost::spirit::rule<Scanner, boost::spirit::dynamic_parser_tag> rule_t;
00121         typedef boost::spirit::guard<expected_t> guard_t;
00122         typedef boost::spirit::assertion<expected_t> assertion_t;
00123 
00124         const c_strlit_parser_t c_strlit_p;
00125         const c_ident_parser_t c_ident_p;
00126 
00127         rule_t  top, api_name, parameter_list, parameter, packet, cexpr;
00128         guard_t guard_api, guard_packet;
00129         assertion_t expect_open_paren,
00130                     expect_close_paren,
00131                     expect_close_brace,
00132                     expect_comma, expect_semicolon;
00133 
00138         definition( const static_api_parser& self )
00139           : c_strlit_p( c_strlit_parser( self.cexpr_p_.codeset_ ) ),
00140             c_ident_p( c_ident_parser( self.cexpr_p_.ucn_, self.cexpr_p_.codeset_ ) ),
00141             expect_open_paren( open_paren_expected ),
00142             expect_close_paren( close_paren_expected ),
00143             expect_close_brace( close_brace_expected ),
00144             expect_comma( comma_expected ),
00145             expect_semicolon( semicolon_expected )
00146         {
00147           using namespace boost::spirit;
00148           set_id();
00149           top =
00150               guard_api
00151               (
00152                 api_name >> 
00153 //                  expect_open_paren( str_p( "(" ) ) >>  // local_class指定子でエラーになるので判定を除去
00154                   str_p( "(" ) >>
00155                   parameter_list >>
00156                   expect_close_paren( str_p( ")" ) ) >>
00157                   expect_semicolon( ch_p( ';' ) )
00158               )
00159               [
00160                 error_handler()
00161               ];
00162           api_name =
00163               c_ident_p[ push_back_a( self.tokens_ ) ];
00164           parameter_list =
00165               parameter % ',';
00166           parameter =
00167               packet | cexpr;
00168           packet =
00169               guard_packet
00170               (
00171                 str_p( "{" )[ push_back_a( self.tokens_ ) ] >>
00172                 parameter_list >>
00173                 expect_close_brace( str_p( "}" )[ push_back_a( self.tokens_ ) ] ) 
00174               )
00175               [
00176                 error_handler()
00177               ];
00178           cexpr =
00179               self.cexpr_p_[ push_back_a( self.tokens_ ) ];
00180         }
00181         void set_id()
00182         {
00183           top.set_id( id_top );
00184           api_name.set_id( id_api_name );
00185           parameter_list.set_id( id_parameter_list );
00186           parameter.set_id( id_parameter );
00187           packet.set_id( id_packet );
00188           cexpr.set_id( id_cexpr );
00189         }
00190         const rule_t& start() const { return top; }
00191       };
00197       explicit static_api_parser( std::vector<std::string>& tokens, const c_const_expr_parser& cexpr_p )
00198         : tokens_( tokens ), cexpr_p_( cexpr_p )
00199       {
00200       }
00201 
00202       std::vector<std::string>& tokens_;
00203       const c_const_expr_parser& cexpr_p_;
00204     };
00205 
00206   }
00207 }
00208 
00209 #endif  // ! TOPPERS_STATIC_API_PARSER_HPP_

Copyright © 2006 by TAKAGI Nobuhisa.
このページは Wed Apr 12 16:31:57 2006 に Doxygen によって生成されました。