Back to index

courier  0.68.2
lexer.h
Go to the documentation of this file.
00001 #ifndef       lexer_h
00002 #define       lexer_h
00003 
00004 
00005 #include      "mio.h"
00006 #include      "buffer.h"
00007 #include      "token.h"
00008 
00010 //
00011 // Lexer is a lexical scanner - translates a text file into individual
00012 // tokens.  The Token class is used to represent an individual token, the
00013 // Lexer class is used to transform a text file into a list of tokens.
00014 //
00015 // The scanning algorythm is quite involved.  We may read ahead in the file,
00016 // then back up.  An 'undo' buffer is maintained.  The actual file is
00017 // referred to indirectly via the Mio class.
00018 //
00019 // The concept of the 'current line number' and 'filename' is also recorded
00020 // by the Lexer object.  An error message function is provided - given an
00021 // error message, the filename, and the line number is printer to standard
00022 // error, followed by the error message.
00023 //
00025 
00026 class  Lexer {
00027 
00028        Mio file;
00029        int linenum;
00030        Buffer filename;
00031        Token::tokentype lasttokentype;
00032        // curchar() represents the next character in the file.
00033        // Calling curchar() does NOT actual "read" the character, this
00034        // is a "peek" function.
00035 
00036        int    curchar() {   return (file.peek()); }
00037 
00038        // nextchar() is used to actually read the next character.
00039 
00040        int    nextchar() { int c=file.get();
00041 
00042                             if (c == '\n')       ++linenum;
00043                             return (c);
00044                      }
00045        void   error(const char *);
00046 
00047 public:
00048        Lexer()       {}
00049        ~Lexer()      {}
00050        int    Open(const char *);  // Open file to read
00051        void   token(Token &);             // Scan the next token in
00052 private:
00053        void   token2(Token &);
00054 public:
00055        void   errmsg(const char *);       // Show error message
00056        void   errmsg(unsigned long, const char *);
00057                                    // Show error message for a different
00058                                    // line
00059        int    Linenum() { return (linenum); }
00060                                    // Return current line number in recipe
00061                                    // file
00062 } ;
00063 
00064 #endif