Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C++

Tokenizer and analyzer package supporting precedence prioritized rules

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
1 Jan 20023 min read 180.1K   2.8K   54  
A library allowing you to conveniently build a custom tokenizer and analyzer supporting precedence priorized rules
/*********************************************************************
	Copyright (C) 2001 by

		Alexander Berthold, alexander-berthold@web.de.
		Hoegestr. 54
		79108 Freiburg i. Breisgau
		Germany

    -- This file is part of cxTokenizer --

    "cxTokenizer" 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 of the License, or any later version.

    "cxTokenizer" 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 "cxTokenizer"; if not, write to the Free 
	Software  Foundation, Inc., 59 Temple Place, Suite 330, 
	Boston, MA  02111-1307  USA

    ---------------------------------------------------------------
      If you find any bugs or if you make other corrections/
	  enhancements, i'd appreciate if you'd let me know about 
	  that. My email is
  
       alexander-berthold@web.de
  
      If you share this code, do not remove this text.
    ---------------------------------------------------------------

*********************************************************************/

// cxTokenizerStringTokenRule.cpp: implementation of the cxTokenizerStringTokenRule class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "cxTokenizerTokenRule.h"
#include "cxTokenizerContextCookie.h"
#include "cxTokenizerContext.h"
#include "cxTokenizerStringTokenRule.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

cxTokenizerStringTokenRule::cxTokenizerStringTokenRule(int nIDValue, xttr_flags ttrf)
: cxTokenizerTokenRule(nIDValue, ttrf,NULL)
	{
	
	}

cxTokenizerStringTokenRule::~cxTokenizerStringTokenRule()
	{
	
	}

//////////////////////////////////////////////////////////////////////
// Operations
//////////////////////////////////////////////////////////////////////

bool	cxTokenizerStringTokenRule::fDoesApply(
			const cxTokenizerContext *ptcContext,
			TCHAR tcChar, bool *pfComplete,
			int nStartPos, int nCurPos) const
	{
	const std::tstring	strCurrentText = ptcContext->strGetCurrentTextConst();
	const cxTokenizerStringContextCookie
						*pCookie;

	pCookie		=reinterpret_cast<const cxTokenizerStringContextCookie*>
					(ptcContext->ptccGetConstCookie(reinterpret_cast<const cxTokenizerTokenRule*>(this)));
	if(nCurPos==nStartPos)
		{
		if(tcChar=='"')
			{
			if(pCookie!=NULL)
				{
				// This is the case when the end of a quoted section is found:
				// "hello"
				//		 ^ here this happens.
				// So it must be ignored
				return false;
				}
			return true;
			}
		else
			return false;
		}

	ASSERT(pCookie!=NULL);

	if(pCookie->fCompleted)
		return false;

	if(pCookie->fInEscape)
		return true;

	if(tcChar=='"')
		{
		(*pfComplete)=true;
		return true;
		}

	return true;
	}

void	cxTokenizerStringTokenRule::vApplied(
			cxTokenizerContext *ptcContext,
			TCHAR tcChar,
			bool /* fCompleted */,
			int nStartPos, int nCurPos) const
	{
	const std::tstring	strCurrentText = ptcContext->strGetCurrentTextConst();
	cxTokenizerStringContextCookie
						*pCookie;

	pCookie		=reinterpret_cast<cxTokenizerStringContextCookie*>
					(ptcContext->ptccGetCookie(reinterpret_cast<const cxTokenizerTokenRule*>(this)));

	// First character?
	if(nCurPos==nStartPos)
		{
		if(tcChar=='"')
			{
			// We need exclusive access to the following characters:
			ptcContext->vSetFlag(tctx_exclusive,true);

			ASSERT(pCookie==NULL);
			pCookie		=new cxTokenizerStringContextCookie();
			ptcContext->fSetCookie(reinterpret_cast<const cxTokenizerTokenRule*>(this),reinterpret_cast<cxTokenizerContextCookie*>(pCookie));
			return;
			}
		else
			return;
		}

	// No, cookie must already exist.
	ASSERT(pCookie!=NULL);
	if(pCookie->fCompleted)
		return;

	TCHAR	tcCharToAdd = _T('\0');

	// In escape sequence?
	if(!pCookie->fInEscape)
		{
		// No.
		if(tcChar=='\\')
			{
			// Start escape sequence
			pCookie->fInEscape	=true;
			pCookie->esct		=esc_begin;
			}
		else
			{
			// End of quotation?
			if(tcChar=='\"')
				{
				// Release exclusive access
				ptcContext->vSetFlag(tctx_exclusive,false);

				// Set completed flag
				pCookie->fCompleted=true;
				return;
				}

			// Otherwise, 'tcChar' must be added to result
			tcCharToAdd=tcChar;
			}
		}
	else
		{
		// Yes, escape sequence
		// First character after '\'?
		if(pCookie->esct==esc_begin)
			{
			// Yes.
			switch(tcChar)
				{
				case '\\':
				case '\"':
				case '\'':
				case '\?':
					tcCharToAdd=tcChar;
					pCookie->fInEscape=false;
					break;
/*				case isdigit(tcChar):
					pCookie->strEsc=tcChar;
					pCookie->esct=esc_octal;
					break;*/
				case 'x':
					pCookie->strEsc=_T("");
					pCookie->esct=esc_hex;
					break;
				default:
					pCookie->fInEscape=false;
					throw cxTokenizerException(LEXERR_INVALID_ESCAPE_SEQUENCE);
					break;
				}
			}
		else
			{
			// No
			if(pCookie->esct==esc_octal)
				ASSERT(FALSE);
			if(pCookie->esct==esc_hex)
				{
				pCookie->strEsc+=tcChar;
				if(pCookie->strEsc.length()==2)
					{
					tcCharToAdd	=(TCHAR)_tcstoul(pCookie->strEsc.data(),NULL,16);
					pCookie->fInEscape=false;
					}
				}
			}
		}

	if(tcCharToAdd!=_T('\0'))
		pCookie->strResult+=tcCharToAdd;
	}

bool	cxTokenizerStringTokenRule::fGetResultString(const cxTokenizerContext* ptcContext, std::tstring& strResult) const
	{
	const cxTokenizerStringContextCookie
						*pCookie;

	pCookie		=reinterpret_cast<const cxTokenizerStringContextCookie*>
					(ptcContext->ptccGetConstCookie(reinterpret_cast<const cxTokenizerTokenRule*>(this)));
	ASSERT(pCookie!=NULL);

	strResult	=pCookie->strResult;
	return true;
	}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions