1 | /*
|
---|
2 | * TOPPERS Software
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems
|
---|
4 | *
|
---|
5 | * Copyright (C) 2007-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 | #include <cctype>
|
---|
38 | #include <sstream>
|
---|
39 | #include "toppers/diagnostics.hpp"
|
---|
40 | #include "toppers/itronx/static_api.hpp"
|
---|
41 | #include "toppers/itronx/static_api_parser.hpp"
|
---|
42 | #include <boost/spirit/include/classic.hpp>
|
---|
43 | #include <boost/lexical_cast.hpp>
|
---|
44 |
|
---|
45 | namespace toppers
|
---|
46 | {
|
---|
47 | namespace itronx
|
---|
48 | {
|
---|
49 | namespace
|
---|
50 | {
|
---|
51 | class find_api_predicate
|
---|
52 | {
|
---|
53 | public:
|
---|
54 | explicit find_api_predicate( std::string const& api_name ) : api_name_( api_name ) {}
|
---|
55 | bool operator()( static_api::info const& info ) const
|
---|
56 | {
|
---|
57 | return info.api_name == api_name_;
|
---|
58 | }
|
---|
59 | private:
|
---|
60 | std::string api_name_;
|
---|
61 | };
|
---|
62 | }
|
---|
63 |
|
---|
64 | /*!
|
---|
65 | * \brief 静的APIの構文解析
|
---|
66 | * \param[in,out] next テキストの読み込み位置、兼構文解析後の次の読み込み位置の格納先
|
---|
67 | * \param[in] last テキストの終端位置
|
---|
68 | * \param[in] info_map 静的API情報マップ
|
---|
69 | * \param[in] ucn 国際文字名を有効にする場合は true を指定する。
|
---|
70 | * \param[in] codeset 文字コード
|
---|
71 | * \retval true 成功
|
---|
72 | * \retval false 失敗
|
---|
73 | */
|
---|
74 | bool static_api::parse( text::const_iterator& next, text::const_iterator last,
|
---|
75 | std::map< std::string, info > const& info_map,
|
---|
76 | bool ucn, codeset_t codeset )
|
---|
77 | {
|
---|
78 | boost::spirit::classic::parse_info< text::const_iterator > pi;
|
---|
79 | std::vector< std::string > tokens;
|
---|
80 | c_const_expr_parser cexpr_p( ucn, codeset );
|
---|
81 | static_api_parser parser( tokens, cexpr_p );
|
---|
82 | static_api temp;
|
---|
83 |
|
---|
84 | text::const_iterator next_temp;
|
---|
85 | for ( next_temp = next; next_temp != last; ++next_temp )
|
---|
86 | {
|
---|
87 | if ( !std::isspace( static_cast< unsigned char >( *next_temp ) ) )
|
---|
88 | {
|
---|
89 | break;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | if ( next_temp == last )
|
---|
93 | {
|
---|
94 | return false;
|
---|
95 | }
|
---|
96 |
|
---|
97 | // エラーメッセージ用の行番号
|
---|
98 | // この行番号は、静的APIの開始位置のものであるため、エラー発生箇所そのものズバリを指すことはできない。
|
---|
99 | temp.line_ = next_temp.line();
|
---|
100 |
|
---|
101 | pi = boost::spirit::classic::parse( next_temp, last, parser, boost::spirit::classic::space_p );
|
---|
102 | if ( !pi.hit )
|
---|
103 | {
|
---|
104 | return false;
|
---|
105 | }
|
---|
106 |
|
---|
107 | // 静的APIが存在するかどうかの判定およびシグニチャの取得
|
---|
108 | std::string api_name( tokens.front() );
|
---|
109 | std::map< std::string, info >::const_iterator info_iter = info_map.find( api_name );
|
---|
110 | info const* pinfo = 0;
|
---|
111 | if ( info_iter != info_map.end() )
|
---|
112 | {
|
---|
113 | pinfo = &info_iter->second;
|
---|
114 | temp.pinfo_ = pinfo;
|
---|
115 | }
|
---|
116 | else
|
---|
117 | {
|
---|
118 | error( temp.line_, _( "static API `%1%\' is unknown" ), api_name );
|
---|
119 | return false;
|
---|
120 | }
|
---|
121 |
|
---|
122 | // 各パラメータの解析
|
---|
123 | std::istringstream iss( pinfo->params );
|
---|
124 | int order = 0; // パラメータリスト内の順序
|
---|
125 | bool param_list = false; // パラメータリスト解析中フラグ
|
---|
126 | std::string symbol;
|
---|
127 | bool skip = false;
|
---|
128 |
|
---|
129 | for ( std::vector< std::string >::const_iterator iter( tokens.begin() + 1 ), last( tokens.end() );
|
---|
130 | iter != last;
|
---|
131 | ++iter )
|
---|
132 | {
|
---|
133 | if ( !param_list )
|
---|
134 | {
|
---|
135 | iss >> symbol;
|
---|
136 | if ( symbol.size() > 3 && symbol.substr( symbol.size() - 3 ) == "..." )
|
---|
137 | {
|
---|
138 | param_list = true;
|
---|
139 | order = 0;
|
---|
140 | //symbol.resize( symbol.size() - 3 );
|
---|
141 | }
|
---|
142 | }
|
---|
143 | if ( symbol == "{" || symbol == "}" )
|
---|
144 | {
|
---|
145 | if ( symbol != *iter )
|
---|
146 | {
|
---|
147 | error( temp.line_, _( "missing token `%1%\'" ), symbol );
|
---|
148 | --iter;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | else if ( *iter == "{" || *iter == "}" )
|
---|
152 | {
|
---|
153 | if ( param_list && *iter == "}" )
|
---|
154 | {
|
---|
155 | iss >> symbol;
|
---|
156 | if ( symbol != "}" )
|
---|
157 | {
|
---|
158 | error( temp.line_, _( "illegal token `%1%\'" ), *iter );
|
---|
159 | }
|
---|
160 | param_list = false;
|
---|
161 | }
|
---|
162 | else if ( *symbol.rbegin() == '\?' ) // 省略可能パラメータのスキップ
|
---|
163 | {
|
---|
164 | skip = true;
|
---|
165 | --iter;
|
---|
166 | }
|
---|
167 | else
|
---|
168 | {
|
---|
169 | error( temp.line_, _( "illegal token `%1%\'" ), *iter );
|
---|
170 | }
|
---|
171 | }
|
---|
172 | else
|
---|
173 | {
|
---|
174 | parameter value;
|
---|
175 | value.symbol = symbol;
|
---|
176 | if ( param_list )
|
---|
177 | {
|
---|
178 | //value.symbol += boost::lexical_cast< std::string >( order++ );
|
---|
179 | value.order = order++;
|
---|
180 | }
|
---|
181 | value.text = *iter;
|
---|
182 | value.value = 0;
|
---|
183 | temp.params_.push_back( value );
|
---|
184 | }
|
---|
185 | }
|
---|
186 | if ( !iss.eof() )
|
---|
187 | {
|
---|
188 | iss >> symbol;
|
---|
189 | if ( symbol != "}" )
|
---|
190 | {
|
---|
191 | error( temp.line_, _( "few parameters for static API `%s\'" ), api_name );
|
---|
192 | }
|
---|
193 | else if ( !skip )
|
---|
194 | {
|
---|
195 | error( temp.line_, _( "missing token `%1%\'" ), symbol );
|
---|
196 | }
|
---|
197 | else
|
---|
198 | {
|
---|
199 | skip = false;
|
---|
200 | }
|
---|
201 | }
|
---|
202 | next = pi.stop;
|
---|
203 | swap( temp );
|
---|
204 | return true;
|
---|
205 | }
|
---|
206 |
|
---|
207 | bool static_api::set_block( char const* type, std::string const& id )
|
---|
208 | {
|
---|
209 | for ( std::vector< parameter >::const_iterator iter( params_.begin() ), last( params_.end() );
|
---|
210 | iter != last;
|
---|
211 | ++iter )
|
---|
212 | {
|
---|
213 | if ( iter->symbol == type )
|
---|
214 | {
|
---|
215 | return false;
|
---|
216 | }
|
---|
217 | }
|
---|
218 | parameter value;
|
---|
219 | value.symbol = type;
|
---|
220 | value.text = id;
|
---|
221 | value.value = 0;
|
---|
222 | params_.push_back( value );
|
---|
223 | return true;
|
---|
224 | }
|
---|
225 |
|
---|
226 | static_api::size_type static_api::count_integer_params() const
|
---|
227 | {
|
---|
228 | size_type result = 0;
|
---|
229 | for ( std::vector< parameter >::const_iterator iter( params_.begin() ), last( params_.end() );
|
---|
230 | iter != last;
|
---|
231 | ++iter )
|
---|
232 | {
|
---|
233 | if ( !iter->symbol.empty()
|
---|
234 | && ( ( iter->symbol[0] == '.' ) || ( iter->symbol[0] == '+' ) ) )
|
---|
235 | {
|
---|
236 | ++result;
|
---|
237 | }
|
---|
238 | }
|
---|
239 | return result;
|
---|
240 | }
|
---|
241 |
|
---|
242 | }
|
---|
243 | }
|
---|