src/compiler/Scanner.h
author Asher Baker <asherkin@gmail.com>
Mon Jun 10 15:52:44 2013 +0100 (2013-06-10)
changeset 265 3a903b89a514
parent 263 ba85a47ee414
permissions -rw-r--r--
Fixed type checking for multidimensional arrays.
asherkin@263
     1
/* vim: set ts=4 sw=4 tw=99 et:
asherkin@263
     2
 *
asherkin@263
     3
 * Copyright (C) 2012 David Anderson
asherkin@263
     4
 *
asherkin@263
     5
 * This file is part of SourcePawn.
asherkin@263
     6
 *
asherkin@263
     7
 * SourcePawn is free software: you can redistribute it and/or modify it under
asherkin@263
     8
 * the terms of the GNU General Public License as published by the Free
asherkin@263
     9
 * Software Foundation, either version 3 of the License, or (at your option)
asherkin@263
    10
 * any later version.
asherkin@263
    11
 * 
asherkin@263
    12
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
asherkin@263
    13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
asherkin@263
    14
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
asherkin@263
    15
 *
asherkin@263
    16
 * You should have received a copy of the GNU General Public License along with
asherkin@263
    17
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
asherkin@263
    18
 */
asherkin@263
    19
#ifndef _include_jitcraft_scanner_h_
asherkin@263
    20
#define _include_jitcraft_scanner_h_
asherkin@263
    21
asherkin@263
    22
#include "Token.h"
asherkin@263
    23
#include "CompileContext.h"
asherkin@263
    24
#include "../Opcodes.h"
asherkin@263
    25
#include "../Vector.h"
asherkin@263
    26
asherkin@263
    27
namespace ke {
asherkin@263
    28
asherkin@263
    29
class Scanner
asherkin@263
    30
{
asherkin@263
    31
    template <bool Enabled>
asherkin@263
    32
    friend class AutoAllowTags;
asherkin@263
    33
asherkin@263
    34
    CompileContext &cc_;
asherkin@263
    35
    const char *stream_;
asherkin@263
    36
    size_t length_;
asherkin@263
    37
    bool pushed_;
asherkin@263
    38
    bool allowTags_;
asherkin@263
    39
    char saved_;
asherkin@263
    40
    const char *pos_;
asherkin@263
    41
    Vector<char> literal_;
asherkin@263
    42
    TokenKind lastToken_;
asherkin@263
    43
    SourcePosition last_;
asherkin@263
    44
    SourcePosition cur_;
asherkin@263
    45
    SourcePosition begin_;
asherkin@263
    46
asherkin@263
    47
  private:
asherkin@263
    48
    char getChar();
asherkin@263
    49
    void putChar(char c);
asherkin@263
    50
    void eatSingleLine();
asherkin@263
    51
    bool eatComment();
asherkin@263
    52
    void calcNewLine(char c);
asherkin@263
    53
    int readEscapeCode();
asherkin@263
    54
    TokenKind stringLiteral();
asherkin@263
    55
    TokenKind hexLiteral();
asherkin@263
    56
    TokenKind numberLiteral(char first);
asherkin@263
    57
    TokenKind identifier(char first);
asherkin@263
    58
asherkin@263
    59
    TokenKind next();
asherkin@263
    60
asherkin@263
    61
  public:
asherkin@263
    62
    Scanner(CompileContext &cc, const char *stream, size_t length);
asherkin@263
    63
asherkin@263
    64
    static double ParseDouble(const char *string);
asherkin@263
    65
asherkin@263
    66
    TokenKind scan();
asherkin@263
    67
    TokenKind peek();
asherkin@263
    68
    TokenKind last() const {
asherkin@263
    69
        return lastToken_;
asherkin@263
    70
    }
asherkin@263
    71
asherkin@263
    72
    bool tagsEnabled() const {
asherkin@263
    73
        return allowTags_;
asherkin@263
    74
    }
asherkin@263
    75
    void setTagsEnabled(bool enabled) {
asherkin@263
    76
        allowTags_ = enabled;
asherkin@263
    77
    }
asherkin@263
    78
asherkin@263
    79
    // Beginning source position of the last instruction.
asherkin@263
    80
    SourcePosition begin() const {
asherkin@263
    81
        return begin_;
asherkin@263
    82
    }
asherkin@263
    83
asherkin@263
    84
    // Note: the literal may have embedded NUL characters.
asherkin@263
    85
    const char *literal() const {
asherkin@263
    86
        return literal_.buffer();
asherkin@263
    87
    }
asherkin@263
    88
    size_t literal_length() const {
asherkin@263
    89
        return literal_.length() - 1;
asherkin@263
    90
    }
asherkin@263
    91
};
asherkin@263
    92
asherkin@263
    93
template <bool Enabled>
asherkin@263
    94
class AutoAllowTags
asherkin@263
    95
{
asherkin@263
    96
    Scanner &scanner_;
asherkin@263
    97
    bool prev_;
asherkin@263
    98
asherkin@263
    99
  public:
asherkin@263
   100
    AutoAllowTags(Scanner &scanner)
asherkin@263
   101
      : scanner_(scanner),
asherkin@263
   102
        prev_(scanner_.tagsEnabled())
asherkin@263
   103
    {
asherkin@263
   104
        scanner_.setTagsEnabled(Enabled);
asherkin@263
   105
    }
asherkin@263
   106
    ~AutoAllowTags()
asherkin@263
   107
    {
asherkin@263
   108
        scanner_.setTagsEnabled(prev_);
asherkin@263
   109
    }
asherkin@263
   110
};
asherkin@263
   111
asherkin@263
   112
} // namespace ke
asherkin@263
   113
asherkin@263
   114
#endif // _include_jitcraft_scanner_h_