//------------------------------------------------------------------------------
// Lamp : Open source game middleware
// Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//------------------------------------------------------------------------------

/** @file
 * ストリームトークナイザ実装
 * @author Junpee
 */

#include "LampBasic.h"
#include "Core/InputOutput/StreamTokenizer.h"
#include "Core/InputOutput/TextReader.h"

namespace Lamp{

// デフォルトデリミタ
const String StreamTokenizer::defaultDelimiter(" \t\r\f");

//------------------------------------------------------------------------------
// コンストラクタ
StreamTokenizer::StreamTokenizer(TextReader* reader, const String& delimiter) :
	reader_(reader), delimiter_(delimiter),
	slashSlash_(true), slashStar_(true), lineNumber_(0){
}
//------------------------------------------------------------------------------
// デストラクタ
StreamTokenizer::~StreamTokenizer(){
}
//------------------------------------------------------------------------------
// 次のトークン
bool StreamTokenizer::nextToken(){
	// トークンのが出てくるまで読み飛ばす
	bool check = checkTokenzier();
	if(!check){ return false; }
	// トークンの読み込み
	String token = tokenizer_.getNextToken();
	// スラッシュスラッシュコメント
	if(slashSlash_){
		if(token.startsWith("//")){
			while(tokenizer_.hasMoreTokens()){ tokenizer_.getNextToken(); }
			return nextToken();
		}
	}
	if(slashStar_){
		if(token.startsWith("/*")){
			while(true){
				// トークンのが出てくるまで読み飛ばす
				if(!checkTokenzier()){ return false; }
				// 終了チェック
				String endComment = tokenizer_.getNextToken();
				if(endComment.endsWith("*/")){ return nextToken(); }
			}
		}
	}

	// トークンの書き出し
	previousToken_ = nowToken_;
	nowToken_ = token;
	return true;
}
//------------------------------------------------------------------------------
bool StreamTokenizer::checkTokenzier(){
	// tokenizer_.hasMoreToken()がfalseならトークナイザ無効
	// かつファイルが読み終わっていればトークンなし
	if(tokenizer_.hasMoreTokens()){ return true; }
	while(true){
		if(reader_->isEnd()){ return false; }
		tokenizer_.initialize(reader_->readLine(), delimiter_);
		lineNumber_++;
		if(tokenizer_.hasMoreTokens()){ return true; }
	}
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
