src/Scanner.h
author David Anderson <dvander@alliedmods.net>
Sun Aug 12 20:48:49 2012 -0700 (2012-08-12)
changeset 172 48cffae8c9e0
parent 92 e3974e7360e9
child 176 2b9141b01023
permissions -rw-r--r--
Implement switch.
     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 numberLiteral(char first);
    56     TokenKind identifier(char first);
    57 
    58     TokenKind next();
    59 
    60   public:
    61     Scanner(CompileContext &cc, const char *stream, size_t length);
    62 
    63     static double ParseDouble(const char *string);
    64 
    65     TokenKind scan();
    66     TokenKind peek();
    67     TokenKind last() const {
    68         return lastToken_;
    69     }
    70 
    71     bool tagsEnabled() const {
    72         return allowTags_;
    73     }
    74     void setTagsEnabled(bool enabled) {
    75         allowTags_ = enabled;
    76     }
    77 
    78     // Beginning source position of the last instruction.
    79     SourcePosition begin() const {
    80         return begin_;
    81     }
    82 
    83     // Note: the literal may have embedded NUL characters.
    84     const char *literal() const {
    85         return literal_.buffer();
    86     }
    87     size_t literal_length() const {
    88         return literal_.length() - 1;
    89     }
    90 };
    91 
    92 template <bool Enabled>
    93 class AutoAllowTags
    94 {
    95     Scanner &scanner_;
    96     bool prev_;
    97 
    98   public:
    99     AutoAllowTags(Scanner &scanner)
   100       : scanner_(scanner),
   101         prev_(scanner_.tagsEnabled())
   102     {
   103         scanner_.setTagsEnabled(Enabled);
   104     }
   105     ~AutoAllowTags()
   106     {
   107         scanner_.setTagsEnabled(prev_);
   108     }
   109 };
   110 
   111 } // namespace ke
   112 
   113 #endif // _include_jitcraft_scanner_h_