[54] | 1 | /*
|
---|
| 2 | * TOPPERS Software
|
---|
| 3 | * Toyohashi Open Platform for Embedded Real-Time Systems
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2007-2009 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 | * \file toppers/macro_processor.hpp
|
---|
| 39 | * \brief マクロ処理のための宣言定義
|
---|
| 40 | *
|
---|
| 41 | * このファイルで定義されるクラス
|
---|
| 42 | * \code
|
---|
| 43 | * class macro_processor;
|
---|
| 44 | * \endcode
|
---|
| 45 | */
|
---|
| 46 | #ifndef TOPPERS_MACRO_PROCESSOR_HPP_
|
---|
| 47 | #define TOPPERS_MACRO_PROCESSOR_HPP_
|
---|
| 48 |
|
---|
| 49 | #include <string>
|
---|
| 50 | #include <vector>
|
---|
| 51 | #include <stack>
|
---|
| 52 | #include <map>
|
---|
| 53 | #include "toppers/workaround.hpp"
|
---|
| 54 | #include "toppers/output_file.hpp"
|
---|
| 55 | #include "toppers/text.hpp"
|
---|
| 56 |
|
---|
| 57 | namespace toppers
|
---|
| 58 | {
|
---|
| 59 |
|
---|
| 60 | class macro_processor
|
---|
| 61 | {
|
---|
| 62 | public:
|
---|
| 63 | struct context;
|
---|
| 64 |
|
---|
| 65 | /*!
|
---|
| 66 | * \struct element macro_processor.hpp "toppers/macro_processor.hpp"
|
---|
| 67 | * \brief マクロ中で使用する変数の値
|
---|
| 68 | *
|
---|
| 69 | * 正確には、変数の値は常に順序付きリストとして扱われます。element は順序付きリストの 1 要素を表します。
|
---|
| 70 | */
|
---|
| 71 | struct element
|
---|
| 72 | {
|
---|
| 73 | boost::optional< std::tr1::int64_t > i;
|
---|
| 74 | std::string v;
|
---|
| 75 | std::string s;
|
---|
| 76 | };
|
---|
| 77 | /*!
|
---|
| 78 | * \brief マクロ中で使用する変数を表現する型
|
---|
| 79 | */
|
---|
| 80 | typedef std::vector< element > var_t;
|
---|
| 81 |
|
---|
| 82 | /*!
|
---|
| 83 | * \struct func_t macro_processor.hpp "toppers/macro_processor.hpp"
|
---|
| 84 | * \brief マクロ内で扱う関数を表現する型
|
---|
| 85 | * \note 元々単純な構造体であったため、互換性のため、集成体と特性を維持させている。
|
---|
| 86 | * \attention node の初期化忘れに要注意
|
---|
| 87 | */
|
---|
| 88 | struct func_t
|
---|
| 89 | {
|
---|
| 90 | std::string name; //!< 関数名
|
---|
| 91 | var_t ( * f )( text_line const& line, std::vector< var_t > const&, context* ); //!< 処理内容
|
---|
| 92 | void const* node;
|
---|
| 93 | };
|
---|
| 94 |
|
---|
| 95 | // modified by takuya 110823
|
---|
| 96 | typedef void ( * hook_t )( text_line const&, std::string const&, var_t const&, var_t const&, context* );
|
---|
| 97 |
|
---|
| 98 | struct context
|
---|
| 99 | {
|
---|
| 100 | std::stack< var_t > stack;
|
---|
| 101 | std::map< std::string, var_t > var_map;
|
---|
| 102 | std::map< std::string, func_t > func_map;
|
---|
| 103 | bool in_function;
|
---|
| 104 | output_file target_file;
|
---|
| 105 | hook_t hook_on_assign;
|
---|
| 106 | text_line line;
|
---|
| 107 | // modified by takuya 110823
|
---|
| 108 | // hoaは省略可能引数なので,問題はないはず
|
---|
| 109 | // ここで,下記2つのコンストラクタを定義すると,
|
---|
| 110 | // 引数なしのコンストラクタを呼んだ場合にどちらを
|
---|
| 111 | // 適用するか判断できず,コンパイルエラーとなるため,
|
---|
| 112 | // context()を削除した.
|
---|
| 113 | // context() : in_function( false ) {}
|
---|
| 114 | explicit context( hook_t hoa = 0 ) : hook_on_assign( hoa ), in_function( false ) {}
|
---|
| 115 | };
|
---|
| 116 |
|
---|
| 117 | struct die_terminate {};
|
---|
| 118 |
|
---|
| 119 | // modified by takuya 110823
|
---|
| 120 | // 省略可能引数を追加しただけなので,問題はないはず
|
---|
| 121 | // ここで,省略可能な引数が追加されたコンストラクタを定義すると,
|
---|
| 122 | // 引数なしのコンストラクタを呼んだ場合に追加する前後のどちらを
|
---|
| 123 | // 適用するか判断できず,コンパイルエラーとなるため,
|
---|
| 124 | // 追加する前のコンストラクタを削除した.
|
---|
| 125 | // しかし,そのようにしたところ,macro_processor.cppでの定義と
|
---|
| 126 | // 矛盾が生じたため,省略可能引数は使わないようにした.
|
---|
| 127 | macro_processor();
|
---|
| 128 | //explicit macro_processor( hook_t hook_on_assign = 0 );
|
---|
| 129 | explicit macro_processor( hook_t hook_on_assign );
|
---|
| 130 | macro_processor( macro_processor const& other );
|
---|
| 131 | explicit macro_processor( text const& in );
|
---|
| 132 | //explicit macro_processor( text const& in, hook_t hook_on_assign = 0 );
|
---|
| 133 | explicit macro_processor( text const& in, hook_t hook_on_assign );
|
---|
| 134 |
|
---|
| 135 | ~macro_processor();
|
---|
| 136 | macro_processor& operator=( macro_processor const& other );
|
---|
| 137 | void swap( macro_processor& other );
|
---|
| 138 | void evaluate( text const& in );
|
---|
| 139 | void set_var( std::string const& name, var_t const& value );
|
---|
| 140 | void set_var( std::string const& name, long name2, var_t const& value );
|
---|
| 141 | var_t const& get_var( std::string const& name ) const;
|
---|
| 142 | var_t const& get_var( std::string const& name, long name2 ) const;
|
---|
| 143 | void add_builtin_function( func_t const& f );
|
---|
| 144 |
|
---|
| 145 | static void remove_comment( text const& in, text& out );
|
---|
| 146 | static void expand_include( text const& in, text& out );
|
---|
| 147 | static void preprocess( text const& in, text& out );
|
---|
| 148 | static std::tr1::int64_t to_integer( var_t const& var, context const* p_ctx );
|
---|
| 149 | static std::string to_string( var_t const& var, context const* p_ctx );
|
---|
| 150 | static bool check_arity( text_line const& line, std::size_t arity, std::size_t valid, char const* function_name );
|
---|
| 151 | static var_t call_user_function( text_line const& line, std::vector< var_t > const& arg_list, context* p_ctx );
|
---|
| 152 | private:
|
---|
| 153 | context* p_ctx_;
|
---|
| 154 | static func_t const builtin_function_table[];
|
---|
| 155 | };
|
---|
| 156 |
|
---|
| 157 | inline bool operator == ( macro_processor::element const& lhs, macro_processor::element const& rhs )
|
---|
| 158 | {
|
---|
| 159 | return lhs.i == rhs.i;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | inline bool operator < ( macro_processor::element const& lhs, macro_processor::element const& rhs )
|
---|
| 163 | {
|
---|
| 164 | return lhs.i < rhs.i;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | struct element_eq
|
---|
| 168 | {
|
---|
| 169 | explicit element_eq( std::tr1::int64_t value ) : value_( value ) {}
|
---|
| 170 | bool operator()( toppers::macro_processor::element const& e ) const
|
---|
| 171 | {
|
---|
| 172 | return e.i && ( e.i.get() == value_ );
|
---|
| 173 | }
|
---|
| 174 | std::tr1::int64_t value_;
|
---|
| 175 | };
|
---|
| 176 |
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | #endif // ! TOPPERS_MACRO_PROCESSOR_HPP_
|
---|