00001 #if !defined(_DataFileParser_H_)
00002 #define _DataFileParser_H_
00003
00004 #include <ostream>
00005 #include <string>
00006 #include <vector>
00007
00008
00009 #line 11 "xrb_datafileparser.trison"
00010
00011 #include "xrb.hpp"
00012
00013 #include <string>
00014
00015 namespace Xrb
00016 {
00017
00018 class DataFileLocation;
00019 class DataFileScanner;
00020 class DataFileStructure;
00021 class DataFileValue;
00022
00023 #line 24 "xrb_datafileparser.h"
00024
00025 class DataFileParser
00026
00027 {
00028 public:
00029
00030 struct Token
00031 {
00032 enum Type
00033 {
00034
00035 BAD_TOKEN = 0x100,
00036 BOOLEAN,
00037 CHARACTER,
00038 FLOAT,
00039 IDENTIFIER,
00040 SINT32,
00041 STRING_FRAGMENT,
00042 UINT32,
00043
00044
00045 END_,
00046
00047
00048 array__,
00049 data_file__,
00050 element__,
00051 element_list__,
00052 string__,
00053 structure__,
00054 value__,
00055 value_list__,
00056
00057
00058 START_,
00059
00060
00061 ERROR_,
00062 DEFAULT_,
00063 INVALID_
00064 };
00065 };
00066
00067 DataFileParser ();
00068 ~DataFileParser ();
00069
00070 inline DataFileValue * const &GetAcceptedToken () const { return m_reduction_token; }
00071 inline void ClearAcceptedToken () { m_reduction_token = NULL; }
00072
00073 inline unsigned int DebugSpewLevel () const { return m_debug_spew_level; }
00074 inline void SetDebugSpewLevel (unsigned int debug_spew_level) { m_debug_spew_level = debug_spew_level; }
00075
00076 static void CheckStateConsistency ();
00077
00078 private:
00079
00080 enum ParserReturnCode
00081 {
00082 PRC_SUCCESS = 0,
00083 PRC_UNHANDLED_PARSE_ERROR = 1
00084 };
00085
00086 ParserReturnCode Parse ();
00087
00088 public:
00089
00090
00091 #line 29 "xrb_datafileparser.trison"
00092
00093 enum ReturnCode
00094 {
00095 RC_SUCCESS = 0,
00096 RC_INVALID_FILENAME,
00097 RC_FILE_OPEN_FAILURE,
00098 RC_PARSE_ERROR,
00099 RC_ERRORS_ENCOUNTERED
00100 };
00101
00102 inline DataFileStructure *AcceptedStructure () const
00103 {
00104 return DStaticCast<DataFileStructure *>(GetAcceptedToken());
00105 }
00106 inline DataFileStructure *StealAcceptedStructure ()
00107 {
00108 DataFileStructure *accepted_structure = AcceptedStructure();
00109 ClearAcceptedToken();
00110 return accepted_structure;
00111 }
00112
00113 ReturnCode Parse (std::string const &input_path);
00114
00115 private:
00116
00117 Token::Type Scan ();
00118
00119 void EmitWarning (std::string const &message);
00120 void EmitWarning (DataFileLocation const &file_location, std::string const &message);
00121
00122 void EmitError (std::string const &message);
00123 void EmitError (DataFileLocation const &file_location, std::string const &message);
00124
00125 DataFileScanner *m_scanner;
00126
00127 #line 128 "xrb_datafileparser.h"
00128
00129 private:
00130
00131 typedef unsigned int StateNumber;
00132
00133 enum TransitionAction
00134 {
00135 TA_SHIFT_AND_PUSH_STATE = 0,
00136 TA_PUSH_STATE,
00137 TA_REDUCE_USING_RULE,
00138 TA_REDUCE_AND_ACCEPT_USING_RULE,
00139 TA_THROW_AWAY_LOOKAHEAD_TOKEN,
00140
00141 TA_COUNT
00142 };
00143
00144 enum ActionReturnCode
00145 {
00146 ARC_CONTINUE_PARSING = 0,
00147 ARC_ACCEPT_AND_RETURN
00148 };
00149
00150 struct ReductionRule
00151 {
00152 typedef DataFileValue * (DataFileParser::*ReductionRuleHandler)();
00153
00154 Token::Type m_non_terminal_to_reduce_to;
00155 unsigned int m_number_of_tokens_to_reduce_by;
00156 ReductionRuleHandler m_handler;
00157 std::string m_description;
00158 };
00159
00160 struct Action
00161 {
00162 TransitionAction m_transition_action;
00163 unsigned int m_data;
00164 };
00165
00166 struct StateTransition
00167 {
00168 Token::Type m_token_type;
00169 Action m_action;
00170 };
00171
00172 struct State
00173 {
00174 unsigned int m_lookahead_transition_offset;
00175 unsigned int m_lookahead_transition_count;
00176 unsigned int m_default_action_offset;
00177 unsigned int m_non_terminal_transition_offset;
00178 unsigned int m_non_terminal_transition_count;
00179 };
00180
00181 inline void NewLookaheadToken ()
00182 {
00183 if (m_is_new_lookahead_token_required)
00184 {
00185 m_is_new_lookahead_token_required = false;
00186 if (m_get_new_lookahead_token_type_from_saved)
00187 {
00188 m_get_new_lookahead_token_type_from_saved = false;
00189 m_lookahead_token_type = m_saved_lookahead_token_type;
00190 }
00191 else
00192 ScanANewLookaheadToken();
00193 }
00194 }
00195 inline Token::Type LookaheadTokenType ()
00196 {
00197 NewLookaheadToken();
00198 return m_lookahead_token_type;
00199 }
00200 inline DataFileValue * const &LookaheadToken ()
00201 {
00202 NewLookaheadToken();
00203 return m_lookahead_token;
00204 }
00205 bool DoesStateAcceptErrorToken (StateNumber state_number) const;
00206
00207 ParserReturnCode PrivateParse ();
00208
00209 ActionReturnCode ProcessAction (Action const &action);
00210 void ShiftLookaheadToken ();
00211 void PushState (StateNumber state_number);
00212 void ReduceUsingRule (ReductionRule const &reduction_rule, bool and_accept);
00213 void PopStates (unsigned int number_of_states_to_pop, bool print_state_stack = true);
00214 void PrintStateStack () const;
00215 void PrintTokenStack () const;
00216 void PrintStateTransition (unsigned int state_transition_number) const;
00217 void ScanANewLookaheadToken ();
00218 void ThrowAwayToken (DataFileValue * token);
00219 void ThrowAwayTokenStack ();
00220
00221 typedef std::vector<StateNumber> StateStack;
00222 typedef std::vector<DataFileValue *> TokenStack;
00223
00224 unsigned int m_debug_spew_level;
00225
00226 StateStack m_state_stack;
00227 TokenStack m_token_stack;
00228
00229 Token::Type m_lookahead_token_type;
00230 DataFileValue * m_lookahead_token;
00231 bool m_is_new_lookahead_token_required;
00232
00233 Token::Type m_saved_lookahead_token_type;
00234 bool m_get_new_lookahead_token_type_from_saved;
00235 bool m_previous_transition_accepted_error_token;
00236
00237 bool m_is_returning_with_non_terminal;
00238 Token::Type m_returning_with_this_non_terminal;
00239
00240 DataFileValue * m_reduction_token;
00241 unsigned int m_reduction_rule_token_count;
00242
00243 static State const ms_state[];
00244 static unsigned int const ms_state_count;
00245 static ReductionRule const ms_reduction_rule[];
00246 static unsigned int const ms_reduction_rule_count;
00247 static StateTransition const ms_state_transition[];
00248 static unsigned int const ms_state_transition_count;
00249
00250 DataFileValue * ReductionRuleHandler0000 ();
00251 DataFileValue * ReductionRuleHandler0001 ();
00252 DataFileValue * ReductionRuleHandler0002 ();
00253 DataFileValue * ReductionRuleHandler0003 ();
00254 DataFileValue * ReductionRuleHandler0004 ();
00255 DataFileValue * ReductionRuleHandler0005 ();
00256 DataFileValue * ReductionRuleHandler0006 ();
00257 DataFileValue * ReductionRuleHandler0007 ();
00258 DataFileValue * ReductionRuleHandler0008 ();
00259 DataFileValue * ReductionRuleHandler0009 ();
00260 DataFileValue * ReductionRuleHandler0010 ();
00261 DataFileValue * ReductionRuleHandler0011 ();
00262 DataFileValue * ReductionRuleHandler0012 ();
00263 DataFileValue * ReductionRuleHandler0013 ();
00264 DataFileValue * ReductionRuleHandler0014 ();
00265 DataFileValue * ReductionRuleHandler0015 ();
00266 DataFileValue * ReductionRuleHandler0016 ();
00267 DataFileValue * ReductionRuleHandler0017 ();
00268 DataFileValue * ReductionRuleHandler0018 ();
00269 DataFileValue * ReductionRuleHandler0019 ();
00270 DataFileValue * ReductionRuleHandler0020 ();
00271 DataFileValue * ReductionRuleHandler0021 ();
00272 DataFileValue * ReductionRuleHandler0022 ();
00273 DataFileValue * ReductionRuleHandler0023 ();
00274 DataFileValue * ReductionRuleHandler0024 ();
00275 DataFileValue * ReductionRuleHandler0025 ();
00276 DataFileValue * ReductionRuleHandler0026 ();
00277
00278 };
00279
00280 std::ostream &operator << (std::ostream &stream, DataFileParser::Token::Type token_type);
00281
00282
00283 #line 65 "xrb_datafileparser.trison"
00284
00285 }
00286
00287 #line 291 "xrb_datafileparser.h"
00288
00289 #endif // !defined(_DataFileParser_H_)
00290