src/Scanner.h
changeset 86 32ad0523b309
parent 59 b7d61ddb3c8d
child 92 e3974e7360e9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Scanner.h	Tue Jul 24 23:53:34 2012 -0700
     1.3 @@ -0,0 +1,81 @@
     1.4 +/*
     1.5 + * Copyright (C) 2012 David Anderson
     1.6 + *
     1.7 + * This file is part of JITCraft.
     1.8 + *
     1.9 + * JITCraft is free software: you can redistribute it and/or modify it under
    1.10 + * the terms of the GNU General Public License as published by the Free
    1.11 + * Software Foundation, either version 3 of the License, or (at your option)
    1.12 + * any later version.
    1.13 + * 
    1.14 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    1.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License along with
    1.19 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    1.20 + */
    1.21 +#ifndef _include_jitcraft_scanner_h_
    1.22 +#define _include_jitcraft_scanner_h_
    1.23 +
    1.24 +#include <string>
    1.25 +#include "Token.h"
    1.26 +#include "CompileContext.h"
    1.27 +
    1.28 +namespace ke {
    1.29 +
    1.30 +class Scanner
    1.31 +{
    1.32 +    CompileContext &cc_;
    1.33 +    const char *stream_;
    1.34 +    size_t length_;
    1.35 +    bool pushed_;
    1.36 +    char saved_;
    1.37 +    const char *pos_;
    1.38 +    std::string literal_;
    1.39 +    TokenKind lastToken_;
    1.40 +    SourcePosition last_;
    1.41 +    SourcePosition cur_;
    1.42 +    SourcePosition begin_;
    1.43 +
    1.44 +  private:
    1.45 +    char getChar();
    1.46 +    void putChar(char c);
    1.47 +    void eatSingleLine();
    1.48 +    bool eatComment();
    1.49 +    void calcNewLine(char c);
    1.50 +    int readEscapeCode();
    1.51 +    TokenKind stringLiteral();
    1.52 +    TokenKind numberLiteral(char first);
    1.53 +    TokenKind identifier(char first);
    1.54 +
    1.55 +    TokenKind next();
    1.56 +
    1.57 +  public:
    1.58 +    Scanner(CompileContext &cc, const char *stream, size_t length);
    1.59 +
    1.60 +    static double ParseDouble(const char *string);
    1.61 +
    1.62 +    TokenKind scan();
    1.63 +    TokenKind peek();
    1.64 +    TokenKind last() const {
    1.65 +        return lastToken_;
    1.66 +    }
    1.67 +
    1.68 +    // Beginning source position of the last instruction.
    1.69 +    SourcePosition begin() const {
    1.70 +        return begin_;
    1.71 +    }
    1.72 +
    1.73 +    // Note: the literal may have embedded NUL characters.
    1.74 +    const char *literal() const {
    1.75 +        return literal_.c_str();
    1.76 +    }
    1.77 +    size_t literal_length() const {
    1.78 +        return literal_.size();
    1.79 +    }
    1.80 +};
    1.81 +
    1.82 +} // namespace ke
    1.83 +
    1.84 +#endif // _include_jitcraft_scanner_h_