Click here to Skip to main content
15,880,956 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.7K   2.8K   54  
A library allowing you to conveniently build a custom tokenizer and analyzer supporting precedence priorized rules
#ifdef _DEBUG

#include "cxTokenizerCharTokenRule.h"

bool	cxTokenizerContext::fRunDiagnostics()
	{
	cxTokenizerContext	ctx;
	cxListEntry			*pteOne, *pteTwo, *pteThree;

	pteOne			=new cxListEntry(5,(cxTokenizerMapData*)1);
	pteTwo			=new cxListEntry(7,(cxTokenizerMapData*)2);
	pteThree		=new cxListEntry(9,(cxTokenizerMapData*)3);

	if(pteOne->fIsCompleted())
		return false;
	if(pteTwo->fIsCompleted())
		return false;
	if(pteThree->fIsCompleted())
		return false;

	pteOne->vSetCompleted(10,(cxTokenizerMapData*)1);
	if(!pteOne->fIsCompleted())
		return false;
	pteTwo->vSetCompleted(11,(cxTokenizerMapData*)1);
	pteTwo->vSetNotApplying();
	if(!pteTwo->fIsNotApplying())
		return false;
	pteThree->vMarkForDeletion();
	if(!pteThree->fIsMarkedForDeletion())
		return false;

	ctx.m_lstTokenRules.push_back(pteOne);
	ctx.m_lstTokenRules.push_back(pteTwo);
	ctx.m_lstTokenRules.push_back(pteThree);

	if(ctx.nDeleteMarkedListEntries()!=1)
		return false;

	if(ctx.m_lstTokenRules.size()!=2)
		return false;

	cxTokenizerContextCookie *pCookie = new cxTokenizerContextCookie();
	cxTokenizerCharTokenRule *pRule = new cxTokenizerCharTokenRule(0,ttrf_character_rule,_T(""));

	if(ctx.ptccGetCookie(pRule)!=NULL)
		return false;
	if(!ctx.fSetCookie(pRule,pCookie))
		return false;
	if( ctx.fSetCookie(pRule,pCookie))
		return false;
	if(ctx.ptccGetCookie(pRule)!=pCookie)
		return false;

	delete pRule;

	if(ctx.m_mapCookies.begin()==ctx.m_mapCookies.end())
		return false;

	ctx.m_strCurrentText=_T("-test-");

	ctx.vCleanUpAfterTokenRecognition();

	if(ctx.m_mapCookies.begin()!=ctx.m_mapCookies.end())
		return false;

	if(ctx.m_lstTokenRules.size()!=0)
		return false;

	if(ctx.m_strCurrentText.length()!=0)
		return false;

	return true;
	}

bool	cxTokenizerContext::cxListEntry::fCheckValid() const
	{
	if(ptmdRule==NULL)
		return false;
	if(ptmdRuleLastValid!=NULL)
		{
		if(!ptmdRuleLastValid->fCheckValid())
			return false;
		}
	if(fNotApplying && nEndPosition==-1)
		return false;

	return true;
	}

bool	cxTokenizerContext::fCheckValid() const
	{
	tc_list_type::const_iterator		it1;
	tc_cookiemap_type::const_iterator	it2;

	for(it1=m_lstTokenRules.begin();
		it1!=m_lstTokenRules.end();
		it1++)
		{
		cxListEntry	*pteCur = (*it1);
		if(!pteCur->fCheckValid())
			return false;
		}

	for(it2=m_mapCookies.begin();
		it2!=m_mapCookies.end();
		it2++)
		{
		const cxTokenizerTokenRule	*pttrTemp;
		cxTokenizerContextCookie	*ptccTemp;

		pttrTemp	=(const cxTokenizerTokenRule*)((*it2).first);
		ptccTemp	=(*it2).second;

		ASSERT(pttrTemp!=NULL);
		ASSERT(ptccTemp!=NULL);
		if(!pttrTemp->fCheckValid())
			return false;
		if(!ptccTemp->fCheckValid())
			return false;
		}

	return true;
	}

#endif

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