00001 #if !defined(_ArithmeticParser_H_)
00002 #define _ArithmeticParser_H_
00003
00004 #include <ostream>
00005 #include <string>
00006 #include <vector>
00007
00008
00009 #line 11 "xrb_arithmeticparser.trison"
00010
00011 #include "xrb.hpp"
00012
00013 namespace Xrb
00014 {
00015
00016 class ArithmeticScanner;
00017
00018 #line 19 "xrb_arithmeticparser.h"
00019
00020 class ArithmeticParser
00021
00022 {
00023 public:
00024
00025 struct Token
00026 {
00027 enum Type
00028 {
00029
00030 BAD_TOKEN = 0x100,
00031 NUMERIC,
00032
00033
00034 END_,
00035
00036
00037 exp__,
00038
00039
00040 START_,
00041
00042
00043 ERROR_,
00044 DEFAULT_,
00045 INVALID_
00046 };
00047 };
00048
00049 ArithmeticParser ();
00050 ~ArithmeticParser ();
00051
00052 inline Float const &GetAcceptedToken () const { return m_reduction_token; }
00053 inline void ClearAcceptedToken () { m_reduction_token = 0.0f; }
00054
00055 inline unsigned int DebugSpewLevel () const { return m_debug_spew_level; }
00056 inline void SetDebugSpewLevel (unsigned int debug_spew_level) { m_debug_spew_level = debug_spew_level; }
00057
00058 static void CheckStateConsistency ();
00059
00060 private:
00061
00062 enum ParserReturnCode
00063 {
00064 PRC_SUCCESS = 0,
00065 PRC_UNHANDLED_PARSE_ERROR = 1
00066 };
00067
00068 ParserReturnCode Parse ();
00069
00070 public:
00071
00072
00073 #line 24 "xrb_arithmeticparser.trison"
00074
00075 Float Parse (std::string const &input_string);
00076
00077 private:
00078
00079 Token::Type Scan ();
00080
00081 ArithmeticScanner *m_scanner;
00082
00083 #line 84 "xrb_arithmeticparser.h"
00084
00085 private:
00086
00087 typedef unsigned int StateNumber;
00088
00089 enum TransitionAction
00090 {
00091 TA_SHIFT_AND_PUSH_STATE = 0,
00092 TA_PUSH_STATE,
00093 TA_REDUCE_USING_RULE,
00094 TA_REDUCE_AND_ACCEPT_USING_RULE,
00095 TA_THROW_AWAY_LOOKAHEAD_TOKEN,
00096
00097 TA_COUNT
00098 };
00099
00100 enum ActionReturnCode
00101 {
00102 ARC_CONTINUE_PARSING = 0,
00103 ARC_ACCEPT_AND_RETURN
00104 };
00105
00106 struct ReductionRule
00107 {
00108 typedef Float (ArithmeticParser::*ReductionRuleHandler)();
00109
00110 Token::Type m_non_terminal_to_reduce_to;
00111 unsigned int m_number_of_tokens_to_reduce_by;
00112 ReductionRuleHandler m_handler;
00113 std::string m_description;
00114 };
00115
00116 struct Action
00117 {
00118 TransitionAction m_transition_action;
00119 unsigned int m_data;
00120 };
00121
00122 struct StateTransition
00123 {
00124 Token::Type m_token_type;
00125 Action m_action;
00126 };
00127
00128 struct State
00129 {
00130 unsigned int m_lookahead_transition_offset;
00131 unsigned int m_lookahead_transition_count;
00132 unsigned int m_default_action_offset;
00133 unsigned int m_non_terminal_transition_offset;
00134 unsigned int m_non_terminal_transition_count;
00135 };
00136
00137 inline void NewLookaheadToken ()
00138 {
00139 if (m_is_new_lookahead_token_required)
00140 {
00141 m_is_new_lookahead_token_required = false;
00142 if (m_get_new_lookahead_token_type_from_saved)
00143 {
00144 m_get_new_lookahead_token_type_from_saved = false;
00145 m_lookahead_token_type = m_saved_lookahead_token_type;
00146 }
00147 else
00148 ScanANewLookaheadToken();
00149 }
00150 }
00151 inline Token::Type LookaheadTokenType ()
00152 {
00153 NewLookaheadToken();
00154 return m_lookahead_token_type;
00155 }
00156 inline Float const &LookaheadToken ()
00157 {
00158 NewLookaheadToken();
00159 return m_lookahead_token;
00160 }
00161 bool DoesStateAcceptErrorToken (StateNumber state_number) const;
00162
00163 ParserReturnCode PrivateParse ();
00164
00165 ActionReturnCode ProcessAction (Action const &action);
00166 void ShiftLookaheadToken ();
00167 void PushState (StateNumber state_number);
00168 void ReduceUsingRule (ReductionRule const &reduction_rule, bool and_accept);
00169 void PopStates (unsigned int number_of_states_to_pop, bool print_state_stack = true);
00170 void PrintStateStack () const;
00171 void PrintTokenStack () const;
00172 void PrintStateTransition (unsigned int state_transition_number) const;
00173 void ScanANewLookaheadToken ();
00174 void ThrowAwayToken (Float token);
00175 void ThrowAwayTokenStack ();
00176
00177 typedef std::vector<StateNumber> StateStack;
00178 typedef std::vector<Float> TokenStack;
00179
00180 unsigned int m_debug_spew_level;
00181
00182 StateStack m_state_stack;
00183 TokenStack m_token_stack;
00184
00185 Token::Type m_lookahead_token_type;
00186 Float m_lookahead_token;
00187 bool m_is_new_lookahead_token_required;
00188
00189 Token::Type m_saved_lookahead_token_type;
00190 bool m_get_new_lookahead_token_type_from_saved;
00191 bool m_previous_transition_accepted_error_token;
00192
00193 bool m_is_returning_with_non_terminal;
00194 Token::Type m_returning_with_this_non_terminal;
00195
00196 Float m_reduction_token;
00197 unsigned int m_reduction_rule_token_count;
00198
00199 static State const ms_state[];
00200 static unsigned int const ms_state_count;
00201 static ReductionRule const ms_reduction_rule[];
00202 static unsigned int const ms_reduction_rule_count;
00203 static StateTransition const ms_state_transition[];
00204 static unsigned int const ms_state_transition_count;
00205
00206 Float ReductionRuleHandler0000 ();
00207 Float ReductionRuleHandler0001 ();
00208 Float ReductionRuleHandler0002 ();
00209 Float ReductionRuleHandler0003 ();
00210 Float ReductionRuleHandler0004 ();
00211 Float ReductionRuleHandler0005 ();
00212 Float ReductionRuleHandler0006 ();
00213 Float ReductionRuleHandler0007 ();
00214 Float ReductionRuleHandler0008 ();
00215 Float ReductionRuleHandler0009 ();
00216
00217 };
00218
00219 std::ostream &operator << (std::ostream &stream, ArithmeticParser::Token::Type token_type);
00220
00221
00222 #line 34 "xrb_arithmeticparser.trison"
00223
00224 }
00225
00226 #line 230 "xrb_arithmeticparser.h"
00227
00228 #endif // !defined(_ArithmeticParser_H_)
00229