home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / vrac / cuj0796.zip / SAKS.ZIP / SCANNER2.H < prev    next >
C/C++ Source or Header  |  1996-04-22  |  2KB  |  131 lines

  1. Listing 3 - The header that defines scanner and token
  2. classes that can look ahead an unlimited number of tokens.
  3.  
  4. //
  5. // scanner.h
  6. //
  7. // Copyright (C) 1996 by Dan Saks.
  8. // May be copied for private, non-commercial use,
  9. // provided this copyright notice remains intact.
  10. // All other rights reserved.
  11. //
  12.  
  13. #ifndef SCANNER_H_INCLUDED
  14. #define SCANNER_H_INCLUDED
  15.  
  16. #include <assert.h>
  17. #include <deque.h>
  18. #include <iostream.h>
  19. #include <limits.h>
  20. #include <string>
  21.  
  22. class token
  23.     {
  24.     friend class scanner;
  25. public:
  26.     enum category
  27.         {
  28.         AMPERSAND = '&',
  29.         COMMA = ',',
  30.         LEFT_BRACKET = '[',
  31.         LEFT_PAREN = '(',
  32.         RIGHT_BRACKET = ']',
  33.         RIGHT_PAREN = ')',
  34.         SEMICOLON = ';',
  35.         STAR = '*',
  36.         NO_MORE = CHAR_MAX + 1,
  37.         SCOPE,
  38.         INT_LITERAL,
  39.         IDENTIFIER,
  40.         NAME,
  41.         TYPE_KEYWORD,
  42.         CONST,
  43.         VOLATILE,
  44.         NO_SUCH,
  45.         NO_VALUE
  46.         };
  47.     token();
  48.     string text() const;
  49.     category kind() const;
  50. private:
  51.     token(category, const string &);
  52.     category the_kind;
  53.     string the_text;
  54.     };
  55.  
  56. const char *image(token::category);
  57.  
  58. class scanner
  59.     {
  60. public:
  61.     scanner(istream &);
  62.     token backup();
  63.     token current() const;
  64.     token get();
  65.     void mark();
  66.     void reset();
  67. private:
  68.     token scan();
  69.     token current_token;
  70.     istream &in_stream;
  71.     deque<token> in_queue;
  72.     deque<token>::iterator in_queue_iterator;
  73.     bool getting_from_queue;
  74.     bool looking_ahead;
  75.     struct pair
  76.         {
  77.         char const *text;
  78.         token::category kind;
  79.         };
  80.     static pair const keyword[];
  81.     scanner(const scanner &);
  82.     scanner &operator=(const scanner &);
  83.     };
  84.  
  85. //
  86. // token inline functions
  87. //
  88. inline
  89. token::token()
  90.     :   the_kind(NO_VALUE)
  91.     {
  92.     }
  93.  
  94. inline
  95. token::token(category c, const string &s)
  96.     :   the_kind(c), the_text(s)
  97.     {
  98.     }
  99.  
  100. inline
  101. string token::text() const
  102.     {
  103.     return the_text;
  104.     }
  105.  
  106. inline
  107. token::category token::kind() const
  108.     {
  109.     return the_kind;
  110.     }
  111.  
  112. //
  113. // scanner inline functions
  114. //
  115. inline
  116. scanner::scanner(istream &s)
  117.     :   in_stream(s),
  118.         looking_ahead(false),
  119.         getting_from_queue(false)
  120.     {
  121.     }
  122.  
  123. inline
  124. token scanner::current() const
  125.     {
  126.     return current_token;
  127.     }
  128.  
  129. #endif
  130.  
  131.