1 | /*
|
---|
2 | * TOPPERS Software
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems
|
---|
4 | *
|
---|
5 | * Copyright (C) 2005-2008 by TAKAGI Nobuhisa
|
---|
6 | *
|
---|
7 | * ãLì ÒÍCȺÌ(1)`(4)Ìðð½·êÉÀèC{\tgEF
|
---|
8 | * Ai{\tgEFAðüϵ½àÌðÜÞDȺ¯¶jðgpE¡»Eü
|
---|
9 | * ÏEÄzziȺCpÆÄÔj·é±Æð³Åø·éD
|
---|
10 | * (1) {\tgEFAð\[XR[hÌ`Åp·éêÉÍCãLÌì
|
---|
11 | * \¦C±Ìpð¨æѺL̳ÛØKèªC»ÌÜÜÌ`Å\[
|
---|
12 | * XR[hÉÜÜêÄ¢é±ÆD
|
---|
13 | * (2) {\tgEFAðCCu`®ÈÇC¼Ì\tgEFAJÉg
|
---|
14 | * pÅ«é`ÅÄzz·éêÉÍCÄzzɺ¤hL
|
---|
15 | gip
|
---|
16 | * Ò}j
|
---|
17 | AÈÇjÉCãLÌì \¦C±Ìpð¨æѺL
|
---|
18 | * ̳ÛØKèðfÚ·é±ÆD
|
---|
19 | * (3) {\tgEFAðC@íÉgÝÞÈÇC¼Ì\tgEFAJÉg
|
---|
20 | * pÅ«È¢`ÅÄzz·éêÉÍCÌ¢¸ê©Ìðð½·±
|
---|
21 | * ÆD
|
---|
22 | * (a) Äzzɺ¤hL
|
---|
23 | gipÒ}j
|
---|
24 | AÈÇjÉCãLÌ
|
---|
25 | * ì \¦C±Ìpð¨æѺL̳ÛØKèðfÚ·é±ÆD
|
---|
26 | * (b) ÄzzÌ`ÔðCÊÉèßéû@ÉæÁÄCTOPPERSvWFNgÉ
|
---|
27 | * ñ·é±ÆD
|
---|
28 | * (4) {\tgEFAÌpÉæè¼ÚIܽÍÔÚIɶ¶é¢©Èé¹
|
---|
29 | * Q©çàCãLì Ò¨æÑTOPPERSvWFNgðÆÓ·é±ÆD
|
---|
30 | * ܽC{\tgEFAÌ[UܽÍGh[U©çÌ¢©Èé
|
---|
31 | * RÉîÿ©çàCãLì Ò¨æÑTOPPERSvWFNgð
|
---|
32 | * ÆÓ·é±ÆD
|
---|
33 | *
|
---|
34 | * {\tgEFAÍC³ÛØÅñ³êÄ¢éàÌÅ éDãLì Ò¨
|
---|
35 | * æÑTOPPERSvWFNgÍC{\tgEFAÉÖµÄCÁèÌgpÚI
|
---|
36 | * ÉηéK«àÜßÄC¢©ÈéÛØàsíÈ¢DܽC{\tgEF
|
---|
37 | * AÌpÉæè¼ÚIܽÍÔÚIɶ¶½¢©Èé¹QÉÖµÄàC»
|
---|
38 | * ÌÓCðíÈ¢D
|
---|
39 | *
|
---|
40 | */
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | * \file toppers/c_expr.hpp
|
---|
44 | * \brief C¾êÌ®Ì\¶ðÍÉÖ·éé¾è`
|
---|
45 | *
|
---|
46 | * ±Ìt@CÅè`³êéNX
|
---|
47 | * \code
|
---|
48 | * struct c_expr_parser_base< Derived >;
|
---|
49 | * struct c_expr_parser;
|
---|
50 | * struct c_const_expr_parser;
|
---|
51 | * \endcode
|
---|
52 | */
|
---|
53 | #ifndef TOPPERS_C_EXPR_HPP_
|
---|
54 | #define TOPPERS_C_EXPR_HPP_
|
---|
55 |
|
---|
56 | #include "toppers/c_parser.hpp"
|
---|
57 | #include <boost/cstdint.hpp>
|
---|
58 |
|
---|
59 | namespace toppers
|
---|
60 | {
|
---|
61 |
|
---|
62 | /*!
|
---|
63 | * \struct c_expr_parser_base c_expr.hpp "toppers/c_expr.hpp"
|
---|
64 | * \brief C¾êÌ®ð\¶ðÍ·é½ßÌîêNX
|
---|
65 | *
|
---|
66 | * ÀÛÉgp·éÛÍA c_expr_parser_base ¨æÑ c_expr_parser_base::definition
|
---|
67 | * NXðh¶·éKvª èÜ·B c_expr_parser_base::definition Ìh¶NXÅÍA
|
---|
68 | * start oÖðè`µÄA\¶ÌKvÈ[ðæèo·æ¤ÉµÄ¾³¢B
|
---|
69 | * ±¤·é±ÆÅAè®Ì¶@Aê®Ì¶@Æ¢Á½æ¤ÉAC¾ê̶@ÌTu
|
---|
70 | * ZbgðeÕÉìèo·±ÆªÅ«Ü·B
|
---|
71 | *
|
---|
72 | * \code
|
---|
73 | * // ê®ðæèo·á
|
---|
74 | * struct c_primary_expression : c_expr_parser_base< c_primary_expression >
|
---|
75 | * {
|
---|
76 | * template < class Scanner >
|
---|
77 | * struct definition : c_expr_parser_base< c_primary_expression >::definition
|
---|
78 | * {
|
---|
79 | * rule_t const& start() const { return primary_expression; }
|
---|
80 | * };
|
---|
81 | * };
|
---|
82 | * \endcode
|
---|
83 | */
|
---|
84 | template < class Derived >
|
---|
85 | struct c_expr_parser_base : boost::spirit::grammar< Derived >
|
---|
86 | {
|
---|
87 | public:
|
---|
88 | /*!
|
---|
89 | * \struct definition c_expr.hpp "toppers/c_expr.hpp"
|
---|
90 | * \brief ¶@è`
|
---|
91 | */
|
---|
92 | template < class Scanner >
|
---|
93 | struct definition
|
---|
94 | {
|
---|
95 | typedef boost::spirit::rule< Scanner > rule_t;
|
---|
96 | rule_t primary_expression,
|
---|
97 | expression,
|
---|
98 | constant_expression,
|
---|
99 | conditional_expression,
|
---|
100 | assignment_expression,
|
---|
101 | assignment_operator,
|
---|
102 | postfix_expression,
|
---|
103 | unary_expression,
|
---|
104 | unary_operator,
|
---|
105 | cast_expression,
|
---|
106 | multiplicative_expression,
|
---|
107 | additive_expression,
|
---|
108 | shift_expression,
|
---|
109 | relational_expression,
|
---|
110 | equality_expression,
|
---|
111 | AND_expression,
|
---|
112 | exclusive_OR_expression,
|
---|
113 | inclusive_OR_expression,
|
---|
114 | logical_AND_expression,
|
---|
115 | logical_OR_expression,
|
---|
116 | string_literal,
|
---|
117 | constant,
|
---|
118 | floating_constant,
|
---|
119 | decimal_floating_constant,
|
---|
120 | hexadecimal_floating_constant,
|
---|
121 | integer_constant,
|
---|
122 | character_constant,
|
---|
123 | declaration_specifiers,
|
---|
124 | type_name,
|
---|
125 | specifier_qualifier_list,
|
---|
126 | storage_class_specifier,
|
---|
127 | type_specifier,
|
---|
128 | type_qualifier,
|
---|
129 | declarator,
|
---|
130 | direct_declarator,
|
---|
131 | struct_or_union_specifier,
|
---|
132 | struct_declaration,
|
---|
133 | struct_declarator,
|
---|
134 | enum_specifier,
|
---|
135 | enumerator,
|
---|
136 | abstract_declarator,
|
---|
137 | pointer,
|
---|
138 | parameter_type_list,
|
---|
139 | parameter_list,
|
---|
140 | parameter_declaration,
|
---|
141 | direct_abstract_declarator;
|
---|
142 |
|
---|
143 | c_ident_parser_t identifier;
|
---|
144 | c_strlit_parser_t c_strlit_p;
|
---|
145 | c_chlit_parser_t c_chlit_p;
|
---|
146 |
|
---|
147 | /*!
|
---|
148 | * \brief RXgN^
|
---|
149 | * \param self ¶@NXi c_expr_parser_base< Derived > NX©çÌp³jÖÌQÆ
|
---|
150 | */
|
---|
151 | definition( Derived const& self )
|
---|
152 | : identifier( c_ident_parser( self.ucn_, self.c_plus_plus_ ) ),
|
---|
153 | c_strlit_p( c_strlit_parser( self.codeset_ ) ),
|
---|
154 | c_chlit_p( c_chlit_parser( self.codeset_ ) )
|
---|
155 | {
|
---|
156 | using namespace boost::spirit;
|
---|
157 | static functor_parser< detail::c_integer_constant_parse_functor< boost::uintmax_t > > const c_int_const_p;
|
---|
158 | static functor_parser< detail::c_integer_suffix_parse_functor > const c_int_suffix_p;
|
---|
159 |
|
---|
160 | primary_expression = // ¡e¢Î
|
---|
161 | identifier
|
---|
162 | | constant
|
---|
163 | | string_literal
|
---|
164 | | ( '(' >> expression >> ')' );
|
---|
165 | expression =
|
---|
166 | assignment_expression % ',';
|
---|
167 | constant_expression =
|
---|
168 | conditional_expression;
|
---|
169 | conditional_expression =
|
---|
170 | logical_OR_expression >> *( '\?' >> expression >> ':' >> logical_OR_expression );
|
---|
171 | assignment_expression =
|
---|
172 | *( unary_expression >> assignment_operator ) >> conditional_expression;
|
---|
173 | assignment_operator =
|
---|
174 | ch_p( '=' ) | "*=" | "/=" | "%=" | "+=" | "?=" | "<<=" | ">>=" | "&=" | "^=" | "|=";
|
---|
175 | postfix_expression =
|
---|
176 | primary_expression >>
|
---|
177 | *(
|
---|
178 | ( '[' >> expression >> ']' )
|
---|
179 | | ( '(' >> list_p( assignment_expression, ',' ) >> ')' )
|
---|
180 | | ( '.' >> identifier )
|
---|
181 | | ( "->" >> identifier )
|
---|
182 | | "++"
|
---|
183 | | "--"
|
---|
184 | );
|
---|
185 | unary_expression =
|
---|
186 | *( str_p( "++" ) || "--" ) >>
|
---|
187 | (
|
---|
188 | ( "sizeof" >> unary_expression )
|
---|
189 | | ( str_p( "sizeof" ) >> '(' >> type_name >> ')' )
|
---|
190 | | postfix_expression
|
---|
191 | | ( unary_operator >> cast_expression )
|
---|
192 | );
|
---|
193 | unary_operator =
|
---|
194 | chset<>( "&*~!+-" );
|
---|
195 | cast_expression =
|
---|
196 | *( '(' >> type_name >> ')' ) >> unary_expression
|
---|
197 | | +( '(' >> ( type_name | identifier ) >> ')' ); // \¶ðÍɸs·ésïÎô
|
---|
198 | multiplicative_expression =
|
---|
199 | cast_expression >>
|
---|
200 | *(
|
---|
201 | ( '*' >> cast_expression )
|
---|
202 | | ( '/' >> cast_expression )
|
---|
203 | | ( '%' >> cast_expression )
|
---|
204 | );
|
---|
205 | additive_expression =
|
---|
206 | multiplicative_expression >>
|
---|
207 | *(
|
---|
208 | ( '+' >> multiplicative_expression )
|
---|
209 | | ( '-' >> multiplicative_expression )
|
---|
210 | );
|
---|
211 | shift_expression =
|
---|
212 | additive_expression >>
|
---|
213 | *(
|
---|
214 | ( "<<" >> additive_expression )
|
---|
215 | | ( ">>" >> additive_expression )
|
---|
216 | );
|
---|
217 | relational_expression =
|
---|
218 | shift_expression >>
|
---|
219 | *(
|
---|
220 | ( '<' >> shift_expression )
|
---|
221 | | ( '>' >> shift_expression )
|
---|
222 | | ( "<=" >> shift_expression )
|
---|
223 | | ( ">=" >> shift_expression )
|
---|
224 | );
|
---|
225 | equality_expression =
|
---|
226 | relational_expression >>
|
---|
227 | *(
|
---|
228 | ( "==" >> relational_expression )
|
---|
229 | | ( "!=" >> relational_expression )
|
---|
230 | );
|
---|
231 | AND_expression =
|
---|
232 | equality_expression >> *( '&' >> equality_expression );
|
---|
233 | exclusive_OR_expression =
|
---|
234 | AND_expression >> *( '^' >> AND_expression );
|
---|
235 | inclusive_OR_expression =
|
---|
236 | exclusive_OR_expression >> *( '|' >> exclusive_OR_expression );
|
---|
237 | logical_AND_expression =
|
---|
238 | inclusive_OR_expression >> *( "&&" >> inclusive_OR_expression );
|
---|
239 | logical_OR_expression =
|
---|
240 | logical_AND_expression >> *( "||" >> logical_AND_expression );
|
---|
241 | string_literal =
|
---|
242 | c_strlit_p
|
---|
243 | | lexeme_d[ 'L' >> c_strlit_p ];
|
---|
244 | constant =
|
---|
245 | floating_constant
|
---|
246 | | integer_constant
|
---|
247 | | identifier // ñè
|
---|
248 | | character_constant;
|
---|
249 | floating_constant =
|
---|
250 | decimal_floating_constant
|
---|
251 | | hexadecimal_floating_constant;
|
---|
252 | decimal_floating_constant =
|
---|
253 | lexeme_d
|
---|
254 | [
|
---|
255 | as_lower_d
|
---|
256 | [
|
---|
257 | ( ( *digit_p >> '.' >> +digit_p ) | ( +digit_p >> '.' ) ) >>
|
---|
258 | 'e' >> !chset<>( "+-" ) >> +digit_p >>
|
---|
259 | !chset<>( "fl" )
|
---|
260 | ]
|
---|
261 | ];
|
---|
262 | hexadecimal_floating_constant =
|
---|
263 | lexeme_d
|
---|
264 | [
|
---|
265 | as_lower_d
|
---|
266 | [
|
---|
267 | "0x" >>
|
---|
268 | ( ( *xdigit_p >> '.' >> +xdigit_p ) | ( +xdigit_p >> '.' ) ) >>
|
---|
269 | 'p' >> !chset<>( "+-" ) >> +digit_p >>
|
---|
270 | !chset<>( "fl" )
|
---|
271 | ]
|
---|
272 | ];
|
---|
273 | integer_constant =
|
---|
274 | lexeme_d[ c_int_const_p >> !c_int_suffix_p ];
|
---|
275 | character_constant =
|
---|
276 | c_chlit_p
|
---|
277 | | lexeme_d[ 'L' >> c_chlit_p ];
|
---|
278 | declaration_specifiers =
|
---|
279 | +( storage_class_specifier | type_specifier | type_qualifier );
|
---|
280 | type_name =
|
---|
281 | specifier_qualifier_list >> !abstract_declarator;
|
---|
282 | specifier_qualifier_list =
|
---|
283 | +( type_specifier | type_qualifier );
|
---|
284 | storage_class_specifier =
|
---|
285 | str_p( "auto" )
|
---|
286 | | "register"
|
---|
287 | | "static"
|
---|
288 | | "extern"
|
---|
289 | | "typedef";
|
---|
290 | type_specifier =
|
---|
291 | str_p( "void" ) | "char" | "short" | "int" | "long" | "float" | "double"
|
---|
292 | | "signed" | "unsigned"
|
---|
293 | | identifier
|
---|
294 | | struct_or_union_specifier
|
---|
295 | | enum_specifier;
|
---|
296 | type_qualifier =
|
---|
297 | str_p( "const" ) | "volatile" | "restrict";
|
---|
298 | declarator =
|
---|
299 | !pointer >> direct_declarator;
|
---|
300 | direct_declarator =
|
---|
301 | ( identifier | ( '(' >> declarator >> ')' ) )
|
---|
302 | >>
|
---|
303 | *(
|
---|
304 | ( '[' >> !constant_expression >> ']' )
|
---|
305 | | ( '(' >> parameter_type_list >> ')' )
|
---|
306 | | ( '(' >> !( identifier % ',' ) >> ')' )
|
---|
307 | );
|
---|
308 | struct_or_union_specifier =
|
---|
309 | lexeme_d[ ( str_p( "struct" ) | "union" ) >> +space_p >> identifier ]
|
---|
310 | | ( lexeme_d[ ( str_p( "struct" ) | "union" ) >> +space_p >> !identifier ] >> '{' >> +struct_declaration >> '}' );
|
---|
311 | struct_declaration =
|
---|
312 | specifier_qualifier_list >> !list_p( struct_declarator, ',' ) >> ';';
|
---|
313 | // lisp_p( struct_declarator, ',' )ðȪÂ\ƵĢéÌÍA
|
---|
314 | // struct_declarator Ì identifier ð specifier_qualifier_list ª
|
---|
315 | // typedef ¼ÆÔᤱÆðñð·é½ß
|
---|
316 | struct_declarator =
|
---|
317 | ( !declarator >> ':' >> constant_expression ) // rbgtB[h
|
---|
318 | | declarator;
|
---|
319 | enum_specifier =
|
---|
320 | ( lexeme_d[ "enum" >> +space_p >> !identifier ] >> '{' >> list_p( enumerator, ',', ',' ) >> '}' ) // C99ÅÍöÌJ}ª ÁÄàæ¢
|
---|
321 | | lexeme_d[ "enum" >> +space_p >> identifier ];
|
---|
322 | enumerator =
|
---|
323 | identifier >> !( '=' >> constant_expression );
|
---|
324 | abstract_declarator =
|
---|
325 | ( !pointer >> direct_abstract_declarator )
|
---|
326 | | pointer;
|
---|
327 | pointer =
|
---|
328 | +( '*' >> *type_qualifier );
|
---|
329 | parameter_type_list =
|
---|
330 | parameter_list >> !( ch_p( ',' ) >> "..." ); // ÂÏÂø
|
---|
331 | parameter_list =
|
---|
332 | parameter_declaration % ',';
|
---|
333 | parameter_declaration =
|
---|
334 | ( declaration_specifiers >> declarator )
|
---|
335 | | ( declaration_specifiers >> !abstract_declarator );
|
---|
336 | direct_abstract_declarator =
|
---|
337 | (
|
---|
338 | !( '(' >> abstract_declarator >> ')' ) >>
|
---|
339 | +(
|
---|
340 | ( '[' >> !constant_expression >> ']' )
|
---|
341 | | ( '(' >> !parameter_type_list >> ')' )
|
---|
342 | )
|
---|
343 | )
|
---|
344 | | ( '(' >> abstract_declarator >> ')' );
|
---|
345 | }
|
---|
346 | };
|
---|
347 | bool ucn_;
|
---|
348 | codeset_t codeset_;
|
---|
349 | bool c_plus_plus_;
|
---|
350 |
|
---|
351 | /*!
|
---|
352 | * \brief RXgN^
|
---|
353 | * \param ucn Û¶¼ÉηéêÍ true ðwè·é
|
---|
354 | * \param codeset ¶R[h
|
---|
355 | * \param c_plus_plus C++ ÉηéêÍ true ðwè·é
|
---|
356 | */
|
---|
357 | explicit c_expr_parser_base( bool ucn = false, codeset_t codeset = ascii, bool c_plus_plus = false )
|
---|
358 | : ucn_( ucn ), codeset_( codeset ), c_plus_plus_( c_plus_plus )
|
---|
359 | {
|
---|
360 | }
|
---|
361 | };
|
---|
362 |
|
---|
363 | /*!
|
---|
364 | * \class c_expr_parser c_expr.hpp "toppers/c_expr.hpp"
|
---|
365 | * \brief C¾êÌ®Ì\¶ðÍNX
|
---|
366 | */
|
---|
367 | struct c_expr_parser : c_expr_parser_base< c_expr_parser >
|
---|
368 | {
|
---|
369 | typedef c_expr_parser_base< c_expr_parser > base_t;
|
---|
370 |
|
---|
371 | /*!
|
---|
372 | * \struct definition c_expr.hpp "toppers/c_expr.hpp"
|
---|
373 | * \brief ¶@è`
|
---|
374 | */
|
---|
375 | template < class Scanner >
|
---|
376 | struct definition : base_t::definition< Scanner >
|
---|
377 | {
|
---|
378 | typedef typename base_t::definition< Scanner >::rule_t rule_t;
|
---|
379 |
|
---|
380 | definition( c_expr_parser const& self ) : base_t::definition< Scanner >( self ) {}
|
---|
381 | rule_t const& start() const { return base_t::definition< Scanner >::expression; };
|
---|
382 | };
|
---|
383 |
|
---|
384 | /*!
|
---|
385 | * \brief RXgN^
|
---|
386 | * \param ucn Û¶¼ÉηéêÍ true ðwè·é
|
---|
387 | * \param codeset ¶R[h
|
---|
388 | */
|
---|
389 | explicit c_expr_parser( bool ucn = false, codeset_t codeset = ascii )
|
---|
390 | : c_expr_parser_base< c_expr_parser >( ucn, codeset )
|
---|
391 | {
|
---|
392 | }
|
---|
393 | };
|
---|
394 |
|
---|
395 | /*!
|
---|
396 | * \class c_const_expr_parser c_expr.hpp "toppers/c_expr.hpp"
|
---|
397 | * \brief C¾êÌè®Ì\¶ðÍNX
|
---|
398 | */
|
---|
399 | struct c_const_expr_parser : c_expr_parser_base< c_const_expr_parser >
|
---|
400 | {
|
---|
401 | typedef c_expr_parser_base< c_const_expr_parser > base_t;
|
---|
402 |
|
---|
403 | /*!
|
---|
404 | * \struct definition c_expr.hpp "toppers/c_expr.hpp"
|
---|
405 | * \brief ¶@è`
|
---|
406 | */
|
---|
407 | template < class Scanner >
|
---|
408 | struct definition : base_t::definition< Scanner >
|
---|
409 | {
|
---|
410 | typedef typename base_t::definition< Scanner >::rule_t rule_t;
|
---|
411 |
|
---|
412 | definition( c_const_expr_parser const& self ) : base_t::definition< Scanner >( self ) {}
|
---|
413 | rule_t const& start() const { return base_t::definition< Scanner >::constant_expression; };
|
---|
414 | };
|
---|
415 |
|
---|
416 | /*!
|
---|
417 | * \brief RXgN^
|
---|
418 | * \param ucn Û¶¼ÉηéêÍ true ðwè·é
|
---|
419 | * \param codeset ¶R[h
|
---|
420 | */
|
---|
421 | explicit c_const_expr_parser( bool ucn = false, codeset_t codeset = ascii )
|
---|
422 | : c_expr_parser_base< c_const_expr_parser >( ucn, codeset )
|
---|
423 | {
|
---|
424 | }
|
---|
425 | };
|
---|
426 |
|
---|
427 | }
|
---|
428 |
|
---|
429 | #endif // ! TOPPERS_C_EXPR_HPP_
|
---|