jitcraft/Scanner.h
author David Anderson <dvander@alliedmods.net>
Mon Jul 02 01:24:57 2012 -0700 (2012-07-02)
changeset 25 23e8b3d8e4e6
parent 21 af5d8e985502
child 26 a48382113da6
permissions -rw-r--r--
Scanning, parsing, and static analysis now report errors properly.
     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 #include "CompileContext.h"
    24 
    25 namespace jc {
    26 
    27 struct SourcePosition
    28 {
    29     int line;
    30     int col;
    31 
    32     SourcePosition()
    33       : line(-1),
    34         col(-1)
    35     {
    36     }
    37 };
    38 
    39 class Scanner
    40 {
    41     CompileContext &cc_;
    42     const char *stream_;
    43     size_t length_;
    44     bool pushed_;
    45     char saved_;
    46     const char *pos_;
    47     std::string literal_;
    48     TokenKind lastToken_;
    49     SourcePosition last_;
    50     SourcePosition cur_;
    51     SourcePosition begin_;
    52 
    53   private:
    54     char getChar();
    55     void putChar(char c);
    56     void eatSingleLine();
    57     bool eatComment();
    58     void calcNewLine(char c);
    59     int readEscapeCode();
    60     TokenKind stringLiteral();
    61     TokenKind numberLiteral(char first);
    62     TokenKind identifier(char first);
    63 
    64     TokenKind next();
    65 
    66   public:
    67     Scanner(CompileContext &cc, const char *stream, size_t length);
    68 
    69     static double ParseDouble(const char *string);
    70 
    71     TokenKind scan();
    72     TokenKind peek();
    73     TokenKind last() const {
    74         return lastToken_;
    75     }
    76 
    77     // Beginning source position of the last instruction.
    78     SourcePosition begin() const {
    79         return begin_;
    80     }
    81 
    82     // Note: the literal may have embedded NUL characters.
    83     const char *literal() const {
    84         return literal_.c_str();
    85     }
    86     size_t literal_length() const {
    87         return literal_.size() + 1;
    88     }
    89 };
    90 
    91 } // namespace jc
    92 
    93 #endif // _include_jitcraft_scanner_h_