[54] | 1 | /*
|
---|
| 2 | * TOPPERS Software
|
---|
| 3 | * Toyohashi Open Platform for Embedded Real-Time Systems
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2005-2010 by TAKAGI Nobuhisa
|
---|
| 6 | *
|
---|
| 7 | * 上記著作権者は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
| 8 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
| 9 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
| 10 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
| 11 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
| 12 | * スコード中に含まれていること.
|
---|
| 13 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
| 14 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
| 15 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
| 16 | * の無保証規定を掲載すること.
|
---|
| 17 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
| 18 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
| 19 | * と.
|
---|
| 20 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
| 21 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
| 22 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
| 23 | * 報告すること.
|
---|
| 24 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
| 25 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
| 26 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
| 27 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
| 28 | * 免責すること.
|
---|
| 29 | *
|
---|
| 30 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
| 31 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
| 32 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
| 33 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
| 34 | * の責任を負わない.
|
---|
| 35 | *
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | /*!
|
---|
| 39 | * \file toppers/c_pp_line.hpp
|
---|
| 40 | * \brief \#line指令に関する宣言定義
|
---|
| 41 | */
|
---|
| 42 | #ifndef TOPPERS_C_PP_LINE_HPP_
|
---|
| 43 | #define TOPPERS_C_PP_LINE_HPP_
|
---|
| 44 |
|
---|
| 45 | #include "toppers/text_line.hpp"
|
---|
| 46 | #include "toppers/c_parser.hpp"
|
---|
| 47 | #include "toppers/workaround.hpp"
|
---|
| 48 | #include <vector>
|
---|
| 49 | #include <functional>
|
---|
| 50 |
|
---|
| 51 | namespace toppers
|
---|
| 52 | {
|
---|
| 53 |
|
---|
| 54 | namespace detail
|
---|
| 55 | {
|
---|
| 56 |
|
---|
| 57 | //! \#line 指令の構文解析
|
---|
| 58 | struct c_pp_line_parser : boost::spirit::grammar< c_pp_line_parser >
|
---|
| 59 | {
|
---|
| 60 | template < class Scanner >
|
---|
| 61 | struct definition
|
---|
| 62 | {
|
---|
| 63 | typedef boost::spirit::rule< Scanner > rule_t;
|
---|
| 64 | rule_t r;
|
---|
| 65 | definition( c_pp_line_parser const& self )
|
---|
| 66 | {
|
---|
| 67 | using namespace boost::spirit;
|
---|
| 68 | r = (
|
---|
| 69 | '#' >> lexeme_d[ str_p( "line" ) >> space_p >> uint_p[ assign_a( self.line_ ) ] ] >>
|
---|
| 70 | c_strlit_parser( self.codeset_ )[ assign_a( self.file_ ) ]
|
---|
| 71 | )
|
---|
| 72 | | (
|
---|
| 73 | '#' >>
|
---|
| 74 | uint_p[ assign_a( self.line_ ) ] >>
|
---|
| 75 | c_strlit_parser( self.codeset_ )[ assign_a( self.file_ ) ] >>
|
---|
| 76 | *anychar_p
|
---|
| 77 | );
|
---|
| 78 | }
|
---|
| 79 | rule_t const& start() const { return r; }
|
---|
| 80 | };
|
---|
| 81 |
|
---|
| 82 | c_pp_line_parser( long& line, std::string& file, codeset_t codeset = ascii )
|
---|
| 83 | : line_( line ), file_( file ), codeset_( codeset )
|
---|
| 84 | {
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | long& line_;
|
---|
| 88 | std::string& file_;
|
---|
| 89 | codeset_t codeset_;
|
---|
| 90 | };
|
---|
| 91 |
|
---|
| 92 | //! \#pragma 指令の構文解析
|
---|
| 93 | struct c_pp_pragma_parser : boost::spirit::grammar< c_pp_pragma_parser >
|
---|
| 94 | {
|
---|
| 95 | template < class Scanner >
|
---|
| 96 | struct definition
|
---|
| 97 | {
|
---|
| 98 | typedef boost::spirit::rule< Scanner > rule_t;
|
---|
| 99 | rule_t r;
|
---|
| 100 | definition( c_pp_pragma_parser const& self )
|
---|
| 101 | {
|
---|
| 102 | using namespace boost::spirit;
|
---|
| 103 | r = '#' >> lexeme_d[ str_p( "pragma" ) >> space_p >> ( +anychar_p )[ assign_a( self.parameter_ ) ] ];
|
---|
| 104 | }
|
---|
| 105 | rule_t const& start() const { return r; }
|
---|
| 106 | };
|
---|
| 107 |
|
---|
| 108 | c_pp_pragma_parser( std::string& parameter ) : parameter_( parameter )
|
---|
| 109 | {
|
---|
| 110 | }
|
---|
| 111 | std::string& parameter_;
|
---|
| 112 | };
|
---|
| 113 |
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /*!
|
---|
| 117 | * \class c_pp_line c_pp_line.hpp "toppers/c_pp_line.hpp"
|
---|
| 118 | * \brief \#line指令を処理させるためのファンクタクラス
|
---|
| 119 | *
|
---|
| 120 | * このクラスは basic_text クラスと組み合わせて使用します。
|
---|
| 121 | */
|
---|
| 122 | template < class Container >
|
---|
| 123 | class c_pp_line : public std::binary_function< Container, line_buf, void >
|
---|
| 124 | {
|
---|
| 125 | public:
|
---|
| 126 | typedef Container conatiner;
|
---|
| 127 |
|
---|
| 128 | /*!
|
---|
| 129 | * \brief コンストラクタ
|
---|
| 130 | * \param codeset 文字コード指定
|
---|
| 131 | */
|
---|
| 132 | explicit c_pp_line( codeset_t codeset = ascii )
|
---|
| 133 | : codeset_( codeset ), pragmas_( new std::vector< line_buf > )
|
---|
| 134 | {
|
---|
| 135 | }
|
---|
| 136 | /*!
|
---|
| 137 | * \brief 括弧演算子
|
---|
| 138 | * \param cont line_buf を要素とするコンテナ
|
---|
| 139 | * \param buf 1行バッファ
|
---|
| 140 | */
|
---|
| 141 | void operator()( conatiner& cont, line_buf& buf )
|
---|
| 142 | {
|
---|
| 143 | using namespace boost::spirit;
|
---|
| 144 | long line;
|
---|
| 145 | std::string file;
|
---|
| 146 | detail::c_pp_line_parser c_pp_line_p( line, file, codeset_ );
|
---|
| 147 |
|
---|
| 148 | if ( parse( buf.buf.begin(), buf.buf.end(), c_pp_line_p, space_p ).full ) // #line指令の処理
|
---|
| 149 | {
|
---|
| 150 | buf.line.line = line;
|
---|
| 151 | assert( file.size() >= 2 );
|
---|
| 152 | buf.line.file = file.substr( 1, file.size()-2 );
|
---|
| 153 | }
|
---|
| 154 | else
|
---|
| 155 | {
|
---|
| 156 | std::string param;
|
---|
| 157 | detail::c_pp_pragma_parser c_pp_pragma_p( param );
|
---|
| 158 |
|
---|
| 159 | if ( parse( buf.buf.begin(), buf.buf.end(), c_pp_pragma_p, space_p ).full ) // #pragma指令の処理
|
---|
| 160 | {
|
---|
| 161 | line_buf t( buf );
|
---|
| 162 | t.buf = param;
|
---|
| 163 | pragmas_->push_back( t );
|
---|
| 164 | }
|
---|
| 165 | else
|
---|
| 166 | {
|
---|
| 167 | std::string::size_type pos = buf.buf.find_first_not_of( " \t" );
|
---|
| 168 | if ( pos == std::string::npos || buf.buf[pos] != '#' )
|
---|
| 169 | {
|
---|
| 170 | cont.push_back( buf );
|
---|
| 171 | }
|
---|
| 172 | ++buf.line.line;
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 | buf.buf.clear();
|
---|
| 176 | }
|
---|
| 177 | /*!
|
---|
| 178 | * \brief \#pragma指令リストの取得
|
---|
| 179 | * \return \#pragma指令リストを返す
|
---|
| 180 | */
|
---|
| 181 | std::vector< line_buf > const& pragmas() const { return *pragmas_; }
|
---|
| 182 | private:
|
---|
| 183 | codeset_t codeset_;
|
---|
| 184 | std::tr1::shared_ptr< std::vector< line_buf > > pragmas_;
|
---|
| 185 | };
|
---|
| 186 |
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | #endif // ! TOPPERS_C_PP_LINE_HPP_
|
---|