jitcraft/Scanner.h
author David Anderson <dvander@alliedmods.net>
Tue Jun 26 00:35:15 2012 -0700 (2012-06-26)
changeset 2 46fc53b8fd2f
parent 0 c455ebb9a5dc
child 4 633176823669
permissions -rw-r--r--
Added support for enums.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #ifndef _include_jitcraft_scanner_h_
    19 #define _include_jitcraft_scanner_h_
    20 
    21 #include <string>
    22 #include "Token.h"
    23 
    24 namespace jc {
    25 
    26 class Scanner
    27 {
    28     const char *stream_;
    29     size_t length_;
    30     bool pushed_;
    31     char saved_;
    32     const char *pos_;
    33     std::string literal_;
    34     TokenKind lastToken_;
    35 
    36   private:
    37     char getChar();
    38     void putChar(char c);
    39     void eatSingleLine();
    40     bool eatComment();
    41     TokenKind integerLiteral(char first);
    42     TokenKind identifier(char first);
    43 
    44     TokenKind next();
    45 
    46   public:
    47     Scanner(const char *stream, size_t length);
    48 
    49     TokenKind scan();
    50     TokenKind peek();
    51     TokenKind last() const {
    52         return lastToken_;
    53     }
    54 
    55     const char *literal() const {
    56         return literal_.c_str();
    57     }
    58 };
    59 
    60 } // namespace jc
    61 
    62 #endif // _include_jitcraft_scanner_h_