www.gusucode.com > VC++编写的SQL服务端和客户端源码程序 > VC++编写的SQL服务端和客户端源码程序\code\Server\LexAnalyzer.cpp

    //Download by http://www.NewXing.com
// LexAnalyzer.cpp : implementation file
//

#include "stdafx.h"
#include "miniSQL.h"
#include "LexAnalyzer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CString SQLKeyList[] =
{
    ";", ",", "*", "=", ".", 
	"<", ">", "(", ")", "+", "-",
	"between", "and", "exit",
    "create", "drop", "insert", "delete", "update", "select",
    "table", "index", "on", "into", "from", "set", "where", "values",
    "unique", "null", 
    "int", "long", "float", "char", "date"
};

/////////////////////////////////////////////////////////////////////////////
// CLexAnalyzer

CLexAnalyzer::CLexAnalyzer( CString& str ) :
	CTokenizer( str )
{
	m_bPushedBack = FALSE;

	MapKeyword();
}

CLexAnalyzer::~CLexAnalyzer()
{
}

void CLexAnalyzer::MapKeyword()
{
	m_KeyIndex.RemoveAll();
	m_KeyIndex.InitHashTable( MAX_KW );

	for( int i = 0; i < sizeof( SQLKeyList ) / sizeof( *SQLKeyList ); i++ )
		m_KeyIndex[ SQLKeyList[i] ] = (SQLKey) i;
}

Lex CLexAnalyzer::NextToken()
{
	if (m_bPushedBack)
	{
		m_bPushedBack = FALSE;
		return m_Lex;
	}

	SQLKey code;
	int val = CTokenizer::NextToken();
	
	if( val == TT_EOF )
	{
		m_Lex.key = EndOfFile;
		m_Lex.str = "EOF";
		return m_Lex;
	}

	if( val == TT_WORD )
	{
		if( m_KeyIndex.Lookup( m_sVal, code ) )
			m_Lex.key = code;
		else 
			m_Lex.key = User;
		m_Lex.str = m_sVal;
	}
	else 
	if( val == TT_INTEGER || val == TT_REAL )
	{
		m_Lex.key = Digit;
		m_Lex.str = GetStrValue();
	}
	else
	if( ::strchr( "+-*=,;.<>()", val ) )
	{
		m_Lex.str = (TCHAR)val;
		m_Lex.key = m_KeyIndex[ m_Lex.str ];
	}
	else
	if( val == TT_STRING )
	{
		m_Lex.key = String;
		m_Lex.str = m_sVal;
	}
	else
	if( val == TT_EOL )
		return NextToken();
	else
		throw Error( ILLEGAL_INPUT, LineNo(), (CString)(TCHAR)val );

	return m_Lex;
}

void CLexAnalyzer::PushBack()
{
	m_bPushedBack = TRUE;
}