Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.3 KiB

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