// joycub - an interpeter for the programming language Joy
// 
// (c) 2004-2006 Sergey Zubkov 
// $Id: token_list.h,v 1.4 2006/02/08 15:10:19 cubbi Exp $
//
// Parser for joycub
#ifndef TOKEN_LIST
#define TOKEN_LIST

#include <vector>
#include "globals.h"

typedef enum {
		STATE_SPACE,		// empty space
		STATE_M_P_NUM,		//  - or + (start of number, float, or operator)
		STATE_D_FLOAT,		//  . (start of float or operator)
		STATE_NUM,			//  digit (start of number, float, or operator)
		STATE_OP,			// letter (start of operator)
		STATE_SET,			// { or }
		STATE_LIST,			// [ or ]
		STATE_CHAR,			// character after '
		STATE_CHAR_E0,		// fist digit after 'backslash
		STATE_CHAR_E1,		// second digit after 'backslash
		STATE_CHAR_E2,		// third digit after 'backslash
		STATE_FLOAT,		// digits after . (float or operator)
		STATE_FLOAT_E0,		// digits or +/- after nn.nnnE (float or operator)
		STATE_FLOAT_E1,		// digits after nn.nnnE+/- (float or operator)
		STATE_STRING,		// data in "
		STATE_STRING_ESC,	// data in " after backslash
		STATE_COMMENT_B,	// after (, before *
		STATE_COMMENT,		// comments, delimited by (* *)
		STATE_COMMENT_E,	// after * in a comment
		VALID_STATES,		// HWM for valid parser states
		STATE_ERROR			// parser error state
} Parse_state;

using namespace std;

class Token_list {
private:
	Parse_state state; // state machine
	vector<pair<obj_type,string> > contents; // parsed token + type

	vector<pair<obj_type,string> > parse_buffer;
	string parse_step_buffer; // new token while it is built up
	unsigned int list_counter;

	vector<pair<obj_type,string> > tokenize(const string);
	string::const_iterator tokenize_step(string::const_iterator);

	vector<string> old_contents;
	vector<string> old_tokenize(const string);
	pair<string,string> old_tokenizeonce(const string);
public:
	vector<pair<obj_type,string> >::const_iterator begin() {return contents.begin();}
	vector<pair<obj_type,string> >::const_iterator end() {return contents.end();}

	Token_list(void);
	Token_list(istream& ss, Parse_state st = STATE_SPACE);
	Token_list(string s);
	void debug_print(void);
	string to_string(void);
	void concat(Token_list &);
	void erase(void);
	bool has_dot(void) const;
	inline Parse_state get_last_state(void) const {return state;};
};

#endif /* TOKEN_LIST */

