source: cfg_oil/trunk/toppers/macro_processor.hpp@ 8

Last change on this file since 8 was 8, checked in by msugi, 14 years ago

ディレクトリ構造の作成,ソース一式とReleaseビルドのコミット.

File size: 5.6 KB
Line 
1/*
2 * TOPPERS Software
3 * Toyohashi Open Platform for Embedded Real-Time Systems
4 *
5 * Copyright (C) 2007-2009 by TAKAGI Nobuhisa
6 *
7 * ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì(1)`(4)‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
8 * ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü
9 * •ÏEÄ”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
10 * (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ì
11 * Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[
12 * ƒXƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
13 * (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
14 * —p‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ…
15ƒƒ“ƒgi—˜—p
16 * ŽÒƒ}ƒjƒ…
17ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L
18 * ‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
19 * (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
20 * —p‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±
21 * ‚ƁD
22 * (a) Ä”z•z‚É”º‚¤ƒhƒLƒ…
23ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ…
24ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜
25 * ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
26 * (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É
27 * •ñ‚·‚邱‚ƁD
28 * (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹
29 * ŠQ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD
30 * ‚Ü‚½C–{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝
31 * —R‚ÉŠî‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð
32 * –Ɛӂ·‚邱‚ƁD
33 *
34 * –{ƒ\ƒtƒgƒEƒFƒA‚́C–³•ÛØ‚Å’ñ‹Ÿ‚³‚ê‚Ä‚¢‚é‚à‚Ì‚Å‚ ‚éDã‹L’˜ìŒ ŽÒ‚¨
35 * ‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚́C–{ƒ\ƒtƒgƒEƒFƒA‚ÉŠÖ‚µ‚āC“Á’è‚ÌŽg—p–Ú“I
36 * ‚ɑ΂·‚é“K‡«‚àŠÜ‚߂āC‚¢‚©‚È‚é•ÛØ‚às‚í‚È‚¢D‚Ü‚½C–{ƒ\ƒtƒgƒEƒF
37 * ƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚½‚¢‚©‚Ȃ鑹ŠQ‚ÉŠÖ‚µ‚Ä‚àC‚»
38 * ‚̐ӔC‚𕉂í‚È‚¢D
39 *
40 */
41/*!
42 * \file toppers/macro_processor.hpp
43 * \brief ƒ}ƒNƒˆ—‚Ì‚½‚߂̐錾’è‹`
44 *
45 * ‚±‚̃tƒ@ƒCƒ‹‚Å’è‹`‚³‚ê‚éƒNƒ‰ƒX
46 * \code
47 * class macro_processor;
48 * \endcode
49 */
50#ifndef TOPPERS_MACRO_PROCESSOR_HPP_
51#define TOPPERS_MACRO_PROCESSOR_HPP_
52
53#include <string>
54#include <vector>
55#include <stack>
56#include <map>
57#include "toppers/workaround.hpp"
58#include "toppers/output_file.hpp"
59#include "toppers/text.hpp"
60
61namespace toppers
62{
63
64 class macro_processor
65 {
66 public:
67 struct context;
68
69 /*!
70 * \struct element macro_processor.hpp "toppers/macro_processor.hpp"
71 * \brief ƒ}ƒNƒ’†‚ÅŽg—p‚·‚é•Ï”‚Ì’l
72 *
73 * ³Šm‚ɂ́A•Ï”‚Ì’l‚͏í‚ɏ‡˜•t‚«ƒŠƒXƒg‚Æ‚µ‚Ĉµ‚í‚ê‚Ü‚·Belement ‚͏‡˜•t‚«ƒŠƒXƒg‚Ì 1 —v‘f‚ð•\‚µ‚Ü‚·B
74 */
75 struct element
76 {
77 boost::optional< std::tr1::int64_t > i;
78 std::string v;
79 std::string s;
80 };
81 /*!
82 * \brief ƒ}ƒNƒ’†‚ÅŽg—p‚·‚é•Ï”‚ð•\Œ»‚·‚éŒ^
83 */
84 typedef std::vector< element > var_t;
85
86 /*!
87 * \struct func_t macro_processor.hpp "toppers/macro_processor.hpp"
88 * \brief ƒ}ƒNƒ“à‚ňµ‚¤ŠÖ”‚ð•\Œ»‚·‚éŒ^
89 * \note Œ³X’Pƒ‚ȍ\‘¢‘Ì‚Å‚ ‚Á‚½‚½‚߁AŒÝŠ·«‚Ì‚½‚߁AW¬‘Ì‚Æ“Á«‚ðˆÛŽ‚³‚¹‚Ä‚¢‚éB
90 * \attention node ‚̏‰Šú‰»–Y‚ê‚É—v’ˆÓ
91 */
92 struct func_t
93 {
94 std::string name; //!< ŠÖ”–¼
95 var_t ( * f )( text_line const& line, std::vector< var_t > const&, context* ); //!< ˆ—“à—e
96 void const* node;
97 };
98
99 typedef void ( * hook_t )( text_line const&, std::string const&, var_t const&, var_t const&, context* );
100
101 struct context
102 {
103 std::stack< var_t > stack;
104 std::map< std::string, var_t > var_map;
105 std::map< std::string, func_t > func_map;
106 bool in_function;
107 output_file target_file;
108 hook_t hook_on_assign;
109 text_line line;
110 explicit context( hook_t hoa = 0 ) : hook_on_assign( hoa ), in_function( false ) {}
111 };
112
113 explicit macro_processor( hook_t hook_on_assign = 0 );
114 macro_processor( macro_processor const& other );
115 explicit macro_processor( text const& in, hook_t hook_on_assign = 0 );
116 ~macro_processor();
117 macro_processor& operator=( macro_processor const& other );
118 void swap( macro_processor& other );
119 void evaluate( text const& in );
120 void set_var( std::string const& name, var_t const& value );
121 void set_var( std::string const& name, long name2, var_t const& value );
122 var_t const& get_var( std::string const& name ) const;
123 var_t const& get_var( std::string const& name, long name2 ) const;
124 void add_builtin_function( func_t const& f );
125
126 static void remove_comment( text const& in, text& out );
127 static void expand_include( text const& in, text& out );
128 static void preprocess( text const& in, text& out );
129 static std::tr1::int64_t to_integer( var_t const& var, context const* p_ctx );
130 static std::string to_string( var_t const& var, context const* p_ctx );
131 static bool check_arity( text_line const& line, std::size_t arity, std::size_t valid, char const* function_name );
132 static var_t call_user_function( text_line const& line, std::vector< var_t > const& arg_list, context* p_ctx );
133 private:
134 context* p_ctx_;
135 static func_t const builtin_function_table[];
136 };
137
138 inline bool operator == ( macro_processor::element const& lhs, macro_processor::element const& rhs )
139 {
140 return lhs.i == rhs.i;
141 }
142
143 inline bool operator < ( macro_processor::element const& lhs, macro_processor::element const& rhs )
144 {
145 return lhs.i < rhs.i;
146 }
147
148 struct element_eq
149 {
150 explicit element_eq( std::tr1::int64_t value ) : value_( value ) {}
151 bool operator()( toppers::macro_processor::element const& e ) const
152 {
153 return e.i && ( e.i.get() == value_ );
154 }
155 std::tr1::int64_t value_;
156 };
157
158}
159
160#endif // ! TOPPERS_MACRO_PROCESSOR_HPP_
Note: See TracBrowser for help on using the repository browser.