src/Scanner.h
author David Anderson <danderson@mozilla.com>
Wed Jul 25 01:32:49 2012 -0700 (2012-07-25)
changeset 93 23189d937087
parent 92 e3974e7360e9
child 172 48cffae8c9e0
permissions -rw-r--r--
Fix opt clang build.
     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 #include "Vector.h"
    25 
    26 namespace ke {
    27 
    28 class Scanner
    29 {
    30     CompileContext &cc_;
    31     const char *stream_;
    32     size_t length_;
    33     bool pushed_;
    34     char saved_;
    35     const char *pos_;
    36     Vector<char> literal_;
    37     TokenKind lastToken_;
    38     SourcePosition last_;
    39     SourcePosition cur_;
    40     SourcePosition begin_;
    41 
    42   private:
    43     char getChar();
    44     void putChar(char c);
    45     void eatSingleLine();
    46     bool eatComment();
    47     void calcNewLine(char c);
    48     int readEscapeCode();
    49     TokenKind stringLiteral();
    50     TokenKind numberLiteral(char first);
    51     TokenKind identifier(char first);
    52 
    53     TokenKind next();
    54 
    55   public:
    56     Scanner(CompileContext &cc, const char *stream, size_t length);
    57 
    58     static double ParseDouble(const char *string);
    59 
    60     TokenKind scan();
    61     TokenKind peek();
    62     TokenKind last() const {
    63         return lastToken_;
    64     }
    65 
    66     // Beginning source position of the last instruction.
    67     SourcePosition begin() const {
    68         return begin_;
    69     }
    70 
    71     // Note: the literal may have embedded NUL characters.
    72     const char *literal() const {
    73         return literal_.buffer();
    74     }
    75     size_t literal_length() const {
    76         return literal_.length() - 1;
    77     }
    78 };
    79 
    80 } // namespace ke
    81 
    82 #endif // _include_jitcraft_scanner_h_