Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tokenizer and analyzer package supporting precedence prioritized rules

, 1 Jan 2002
A library allowing you to conveniently build a custom tokenizer and analyzer supporting precedence priorized rules
cxtpackagetut_win32vc.zip
cxTokenizer
cxTokenizer.dsp
cxTokenizer.plg
cxtPackage
cxtPackage.plg
cxtPackage.dsp
mathTok
mathTok.plg
mathTok.dsp
simpleCalc
simpleCalc.bmp
simpleCalc.dsp
simpleCalc.plg
tkCommon
cxAnalyzer
cxAnalyzer.dsp
cxAnalyzer.plg
cxtpackage_win32vc.zip
cxTokenizer.dsp
cxTokenizer.plg
Debug
cxAnalyzer.dsp
cxAnalyzer.plg
cxtPackage.plg
cxtPackage.dsp
grammaride.zip
pkgcomplete.zip
cxTokenizerMatchTokenRule.inl
cxTokenizer.dsp
cxtPackage.dsp
emptyTestApp
emptyTestApp.clw
res
emptyTestApp.ico
emptyTestApp.dsp
grammarIDE
grammarIDE.clw
res
grammarIDEDoc.ico
icon1.ico
zoomable.ico
Toolbar.bmp
grammarIDE.ico
vssver.scc
grammarIDE.dsp
simpleCalc.bmp
simpleCalc.dsp
vssver.scc
cxaToken.inl
cxAnalyzer.dsp
pkgsrconly.zip
cxTokenizerMatchTokenRule.inl
cxTokenizer.dsp
cxtPackage.dsp
emptyTestApp.clw
emptyTestApp.ico
emptyTestApp.dsp
vssver.scc
cxaToken.inl
cxAnalyzer.dsp
/*********************************************************************
	Copyright (C) 2001/2 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.
    ---------------------------------------------------------------

Class:      cxTokenizerInputStream
Author:     Alexander Berthold
Copyright:  Alexander Berthold
Date:       2001/12/19
Version:	0.2.01
Purpose:    Base class for the input stream for the lexxer.
            Must support 'ctkExternalObjectPointer'.
            See 'cxTokenizerTextInputStream' for an application.


Version history:

	-	2001/05/19
		Renamed class from 'cpLexxerInputStream' to 'cxTokenizerInputStream'.

    -	2001/06/05
		Current source labeled version 0.1.15

	-	2001/06/08
		Added method (fGetCurrentLine) to get the current line.
		Also intruduced 'fReadBlock' to randomly read within the input stream.
		Must not be supported by derived classes; but if it isn't supported,
		'fGetCurrentLine' won't work.

	-	2001/06/12
		Fixed some problems + added some methods for conveniently (if implemented
		by the derived class) getting line number information.
		New version number is now 0.1.16
		
	-	2001/12/19
		Labeled 0.2.01
		
*********************************************************************/

// cxTokenizerInputStream.h: interface for the cxTokenizerInputStream class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CXTOKENIZERINPUTSTREAM_H__2EEE2883_3AE8_42FD_9387_38FF266EAD49__INCLUDED_)
#define AFX_CXTOKENIZERINPUTSTREAM_H__2EEE2883_3AE8_42FD_9387_38FF266EAD49__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

enum {
	crlf_cronly=0,
	crlf_lfonly=1,
	crlf_crlf=2
	};

class cxTokenizerInputStream :
	public ctkExternalObjectPointer,
	public ctkCheckValid
{
// Construction
public:
	cxTokenizerInputStream()
        {};
	virtual ~cxTokenizerInputStream()
        {};

// Attributes
protected:

// ctkCheckValid operations
public:
#ifdef _DEBUG
	virtual bool			fCheckValid() const { return true; };
	static bool				fRunDiagnostics() { return false; };
#else
	virtual bool			fCheckValid() const { return true; };
#endif

// Protected operations
protected:
	virtual bool	fReadBlock(long /*lStartPos*/, long /*lLength*/, std::tstring& /*strResult*/, TCHAR /*tcTermChar*/) const
		{	return false;	};

// Operations
public:
	virtual std::tstring strGetName() const { return std::tstring(); };
	virtual TCHAR	tcGetNextCharacter() PURE_VIRTUAL_FALSE;
	virtual TCHAR	tcPeekNextCharacter() const PURE_VIRTUAL_FALSE;
	virtual bool	fPutBack(LPCTSTR /*lpszText*/) PURE_VIRTUAL_FALSE;
	virtual bool	fPutBack(TCHAR /*tcChar*/) PURE_VIRTUAL_FALSE;
	virtual bool	fIsEofReached() const PURE_VIRTUAL_FALSE;
	virtual void	vInit() {};
	virtual bool	fIncludeFile(const std::tstring& name) { return false; };

	virtual long	lGetCurrentLine() const PURE_VIRTUAL_FALSE;
	virtual long	lGetCurrentPosition() const PURE_VIRTUAL_FALSE;
	virtual long	lGetCurrentPosInLine() const PURE_VIRTUAL_FALSE;
	virtual bool	fGetCurrentLine(std::tstring& /*strResult*/) const PURE_VIRTUAL_FALSE;
	virtual bool	fGetLineAtPosition(long /*lPosition*/, std::tstring& /*strResult*/) const PURE_VIRTUAL_FALSE;
	virtual long	lGetLineNumberFromPosition(long /*lPosition*/) const PURE_VIRTUAL_FALSE;
	virtual long	lGetPositionFromLineNumber(long /*lLineNumber*/) const PURE_VIRTUAL_FALSE;
	long			lGetPosInLine(long lPosition) const
		{
		long	lLine = lGetLineNumberFromPosition(lPosition);
		long	lStartPos;
		if(lLine==-1) return -1;
		lStartPos = lGetPositionFromLineNumber(lLine);
		if(lStartPos==-1) return -1;
		return lPosition-lStartPos;
		}
};

template<int t_nMode, bool t_fUseLNCallback = false>
class cxTokenizerInputStreamDefaultImpl :
	public cxTokenizerInputStream
{
// Construction/Destruction
public:
	cxTokenizerInputStreamDefaultImpl()
		{
		m_nCurrentLine	=0;
		m_nCurPosInLine	=0;
		m_nCurPosInStream=0;
		m_nCurLineStartPos=0;
		}

// Attributes
protected:
	int				m_nCurrentLine;
	int				m_nCurPosInLine;
	int				m_nCurPosInStream;
	int				m_nCurLineStartPos;

// Protected callback operations
protected:
	virtual	void	vLineNumberReached(long lLineNumber, long lPosition) 
		{	return;	};

// Operations
public:
	TCHAR			tcGetNextCharacterDefaultImpl(TCHAR tcChar)
		{
		bool		fNewLine = false;

		if(t_nMode==crlf_cronly && tcChar=='\x0d')
			fNewLine = true;
		if(t_nMode==crlf_lfonly && tcChar=='\x0a')
			fNewLine = true;

		if(fNewLine)
			{
			m_nCurrentLine++;
			m_nCurPosInLine=0;
			m_nCurLineStartPos=m_nCurPosInStream+1;
			if(t_fUseLNCallback)
				vLineNumberReached(m_nCurrentLine,m_nCurLineStartPos);
			}
		else
			{
			if(m_nCurPosInLine!=-1)
				m_nCurPosInLine++;
			}

		m_nCurPosInStream++;
		return tcChar;
		}

	bool			fPutBackDefaultImpl(LPCTSTR lpszText)
		{
		TCHAR		tcChar;
		while( (tcChar=(*(lpszText++)))!='\0' )
			{
			m_nCurPosInStream--;
			if(t_nMode==crlf_cronly && tcChar=='\x0d')
				m_nCurrentLine--, m_nCurPosInLine=-1, m_nCurLineStartPos=lGetCurrentPosition();
			if(t_nMode==crlf_lfonly && tcChar=='\x0a')
				m_nCurrentLine--, m_nCurPosInLine=-1, m_nCurLineStartPos=lGetPositionFromLineNumber(m_nCurrentLine);
			}
		return true;
		}

	bool			fPutBackDefaultImpl(TCHAR tcChar)
		{
		m_nCurPosInStream--;
		if(t_nMode==crlf_cronly && tcChar=='\x0d')
			m_nCurrentLine--, m_nCurPosInLine=-1, m_nCurLineStartPos=lGetCurrentPosition();
		if(t_nMode==crlf_lfonly && tcChar=='\x0a')
			m_nCurrentLine--, m_nCurPosInLine=-1, m_nCurLineStartPos=lGetCurrentPosition();
		return true;
		}

	virtual long	lGetCurrentLine() const { return m_nCurrentLine; };
	virtual long	lGetCurrentPosition() const { return m_nCurPosInStream; };
	virtual long	lGetCurrentPosInLine() const { return m_nCurPosInLine; };
	virtual bool	fGetCurrentLine(std::tstring& strResult) const
		{
		if(t_nMode==crlf_cronly)
			return fReadBlock(m_nCurLineStartPos,512,strResult,_T('\x0d'));
		if(t_nMode==crlf_lfonly)
			return fReadBlock(m_nCurLineStartPos,512,strResult,_T('\x0a'));

		return false;
		}

	virtual bool	fGetLineAtPosition(long lPosition, std::tstring& strResult) const
		{	return false;	};
	virtual long	lGetLineNumberFromPosition(long lPosition) const
		{	return -1;	};
	virtual long	lGetPositionFromLineNumber(long lLineNumber) const
		{	return -1;	};
};

template<int t_nMode>
class cxTokenizerInputStreamDefaultWithLineNumberImpl :
	public cxTokenizerInputStreamDefaultImpl<t_nMode,true>
{
// Attributes
public:
	typedef std::map<long,long>	L2L_map_type;
	L2L_map_type	m_mapLNToPosition;
	L2L_map_type	m_mapPositionToLN;

// Protected callback operations
protected:
	virtual	void	vLineNumberReached(long lLineNumber, long lPosition) 
		{
		if(m_mapLNToPosition.find(lLineNumber)==m_mapLNToPosition.end())
			m_mapLNToPosition.insert(L2L_map_type::value_type(lLineNumber,lPosition));
		if(m_mapPositionToLN.find(lPosition)==m_mapPositionToLN.end())
			m_mapPositionToLN.insert(L2L_map_type::value_type(lPosition,lLineNumber));
		return;	
		};

// Operations
public:
	virtual void	vInit()
		{
		vLineNumberReached(0,0);
		}

	virtual bool	fGetLineAtPosition(long lPosition, std::tstring& strResult) const
		{
		long		lLineNumber = lGetLineNumberFromPosition(lPosition);
		long		lStartPos = -1;
		L2L_map_type::const_iterator	it;

		if(lLineNumber==-1)
			return false;

		it			=m_mapLNToPosition.find(lLineNumber);
		if(it==m_mapLNToPosition.end())
			lStartPos = 0;
		else
			lStartPos	=it->second;

		if(t_nMode==crlf_cronly)
			return fReadBlock(lStartPos,512,strResult,_T('\x0d'));
		if(t_nMode==crlf_lfonly)
			return fReadBlock(lStartPos,512,strResult,_T('\x0a'));

		return false;
		};

	virtual long	lGetLineNumberFromPosition(long lPosition) const
		{
		L2L_map_type::const_iterator	it;
		long		lLineNumber = 0;

		it			=m_mapPositionToLN.begin();

// @ToDo:
		// Could be improved by a binary search or something,
		// but as this method should only be called when a parsing error
		// happened, it shouldn't be time critical.
		while(it!=m_mapPositionToLN.end())
			{
			if(it->first>lPosition)
				break;
			lLineNumber	=it->second;
			it++;
			}

		return lLineNumber;
		};

	virtual long	lGetPositionFromLineNumber(long lLineNumber) const
		{
		if(lLineNumber==0)
			return 0;

		L2L_map_type::const_iterator	it;

		it			=m_mapLNToPosition.find(lLineNumber);
		if(it==m_mapLNToPosition.end())
			return -1;

		return it->second;
		};
};

#endif // !defined(AFX_CXTOKENIZERINPUTSTREAM_H__2EEE2883_3AE8_42FD_9387_38FF266EAD49__INCLUDED_)

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

Share

About the Author

Alexander Berthold
Web Developer
Germany Germany
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140826.1 | Last Updated 2 Jan 2002
Article Copyright 2001 by Alexander Berthold
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid