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