home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / genlex.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  2.3 KB  |  104 lines

  1. /*++
  2.  
  3. Copyright (C) 1999 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     GENLEX.H
  8.  
  9. Abstract:
  10.  
  11.     Generic lexer framework classes.
  12.  
  13. History:
  14.  
  15. --*/
  16.  
  17. #ifndef _GENLEX_H_
  18. #pragma option push -b -a8 -pc -A- /*P_O_Push*/
  19. #define _GENLEX_H_
  20.  
  21. #include <Polarity.h>
  22.  
  23. class CGenLexSource
  24. {
  25. public:
  26.     virtual wchar_t NextChar() = 0;
  27.         // Return 0 on end-of-input
  28.     virtual void Pushback(wchar_t) = 0;
  29.     virtual void Reset() = 0;
  30. };
  31.  
  32. class CTextLexSource : public CGenLexSource
  33. {
  34.     const wchar_t *m_pSrcBuf;
  35.     const wchar_t *m_pStart;
  36.  
  37. public:
  38.     CTextLexSource(const wchar_t *pSrc) { SetString(pSrc); }
  39.         // Binds directly to <pSrc> buffer, but doesn't delete it.
  40.  
  41.     wchar_t NextChar()
  42.     {
  43.         if (!m_pSrcBuf)
  44.             return 0;
  45.         else
  46.             return *m_pSrcBuf++ ? m_pSrcBuf[-1] : 0;
  47.     }
  48.  
  49.     void Pushback(wchar_t)
  50.     {
  51.         if (m_pSrcBuf)
  52.             --m_pSrcBuf;
  53.     }
  54.  
  55.     void Reset() { m_pSrcBuf = m_pStart; }
  56.     void SetString (const wchar_t *pSrc) { m_pSrcBuf = m_pStart = pSrc; }
  57. };
  58.  
  59.  
  60. #pragma pack(2)
  61. struct LexEl
  62. {
  63.     wchar_t cFirst, cLast;
  64.     WORD wGotoState;
  65.     WORD wReturnTok;
  66.     WORD wInstructions;
  67. };
  68. #pragma pack()
  69.  
  70.  
  71. // Lexer driver instructions
  72.  
  73. #define GLEX_ACCEPT      0x1            // Add the char to the token
  74. #define GLEX_CONSUME     0x2            // Consume the char without adding to token
  75. #define GLEX_PUSHBACK    0x4            // Place the char back in the source buffer for next token
  76. #define GLEX_NOT         0x8            // A match occurs if the char is NOT the one specified
  77. #define GLEX_LINEFEED    0x10               // Increase the source linecount
  78. #define GLEX_RETURN      0x20               // Return the indicated token to caller
  79. #define GLEX_ANY         wchar_t(0xFFFF)    // Any character
  80. #define GLEX_EMPTY       wchar_t(0xFFFE)    // When subrange is not specified
  81.  
  82. class POLARITY CGenLexer
  83. {
  84.     wchar_t    *m_pTokenBuf;
  85.     int         m_nCurrentLine;
  86.     int         m_nCurBufSize;
  87.     CGenLexSource   *m_pSrc;
  88.     LexEl           *m_pTable;
  89.     
  90. public:
  91.     CGenLexer(LexEl *pTbl, CGenLexSource *pSrc);
  92.     
  93.    ~CGenLexer(); 
  94.     int NextToken();
  95.         // Returns 0 on end of input.
  96.  
  97.     wchar_t* GetTokenText() { return m_pTokenBuf; }
  98.     int GetLineNum() { return m_nCurrentLine; }
  99.     void Reset();
  100. };
  101.  
  102. #pragma option pop /*P_O_Pop*/
  103. #endif
  104.