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{\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 | * \file toppers/macro_processor.hpp
|
---|
43 | * \brief }N̽ßÌé¾è`
|
---|
44 | *
|
---|
45 | * ±Ìt@CÅè`³êéNX
|
---|
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 |
|
---|
61 | namespace 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Ågp·éÏÌl
|
---|
72 | *
|
---|
73 | * ³mÉÍAÏÌlÍíÉt«XgƵĵíêÜ·Belement Ít«XgÌ 1 vfð\µÜ·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Ågp·éÏð\»·é^
|
---|
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 ³XPÈ\¢ÌÅ Á½½ßAÝ·«Ì½ßAW¬ÌÆÁ«ðÛ³¹Ä¢é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_
|
---|