cpp-programming

Simple C++ programs
git clone git://git.laack.co/cpp-programming.git
Log | Files | Refs | README

scanner.cpp (8023B)


      1 class Scanner {
      2     public:
      3         Scanner(std::string input, Lox* loxRef) {
      4             lRef = loxRef;
      5             source = input;
      6         }
      7         std::vector<Token> scanTokens() {
      8         
      9             current = 0;
     10 
     11             while(!isDone()) {
     12                 start = current;
     13                 scanToken();
     14             }
     15 
     16             tokens.push_back({TokenType::EOF_, "" , {} , line});
     17             return tokens;
     18         }
     19     private:
     20 
     21         int start;
     22         int current = 0;
     23         int line = 1;
     24         Lox* lRef;
     25 
     26         std::string source;
     27         std::vector<Token> tokens;
     28 
     29         char advance() {
     30             char c = source[current];
     31             current += 1;
     32             return c;
     33         }
     34 
     35         bool isDone() {
     36             return current >= source.size();
     37         }
     38 
     39         char peek() {
     40             if(current < source.size()) {
     41                 return source[current];
     42             }
     43             return '\0';
     44         }
     45 
     46         bool isDigit() {
     47             return (peek() <= '9' && peek() >= '0');
     48         }
     49         char doublePeek() {
     50             if(current + 1 < source.size()) {
     51                 return source[current+1];
     52             }
     53             return '\0';
     54         }
     55         bool isDigit(char ch) {
     56             return (ch <= '9' && ch >= '0');
     57         }
     58 
     59         bool isAlpha(char ch) {
     60             return ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch == '_') );
     61         }
     62         
     63         bool match(char c) {
     64             if(source.size() > current) {
     65                 bool res = (c == source[current]);
     66                 if(res) {
     67                     advance();
     68                 }
     69                 return res;
     70             }
     71             return false;
     72         }
     73         
     74         std::unordered_map<std::string, TokenType> idLookup {
     75             {"and", TokenType::AND},
     76             {"class",TokenType::CLASS},
     77             {"else",TokenType::ELSE},
     78             {"false",TokenType::FALSE},
     79             {"for",TokenType::FOR},
     80             {"fun",TokenType::FUN},
     81             {"if",TokenType::IF},
     82             {"nil",TokenType::NIL},
     83             {"or",TokenType::OR},
     84             {"print",TokenType::PRINT},
     85             {"return",TokenType:: RETURN},
     86             {"super",TokenType::SUPER},
     87             {"this",TokenType::THIS},
     88             {"true",TokenType::TRUE},
     89             {"var",TokenType::VAR},
     90             {"while",TokenType::WHILE}
     91         };
     92 
     93         void identifier() {
     94             std::string acc = "";
     95             acc += source[current-1];
     96             while (isAlpha(peek())) {
     97                 char ch = advance();
     98                 acc += ch;
     99             }
    100             
    101             if(idLookup.find(acc) == idLookup.end()) {
    102                 addToken(TokenType::IDENTIFIER);
    103             } else {
    104                 addToken(idLookup[acc]);
    105             }
    106         }
    107 
    108         void number() {
    109             
    110             char ch = peek();
    111             bool firstDot = false;
    112             while(isDigit() || (peek() == '.' && !firstDot && isDigit(doublePeek()))) {
    113                 if(ch == '.') {
    114                     firstDot = true;
    115                 }
    116                 if(current < source.size()) {
    117                     ch = advance();
    118                 } else {
    119                     break; // eof
    120                 }
    121             }
    122             std::string numStr = source.substr(start,current-start);
    123             double res = std::stod(numStr);
    124             Literal lit = Literal("", res, false);
    125             addToken(TokenType::NUMBER,lit);
    126         }
    127 
    128         void string() {
    129             std::string strLiteral = "";
    130             char ch;
    131 
    132             if(current < source.size()) {
    133                 ch = advance();
    134             } else {
    135                 lRef->error(line, "Unterminated string.");
    136                 return;
    137             }
    138 
    139             while(ch != '"') {
    140                 if(ch == '\n') {
    141                     line++;
    142                 }
    143                 strLiteral += ch;
    144                 if(current < source.size()) {
    145                     ch = advance();
    146                 } else {
    147                     lRef->error(line, "Unterminated string.");
    148                     return;
    149                 }
    150             }
    151             addToken(TokenType::STRING,{strLiteral});
    152         }
    153 
    154         void addToken(TokenType type, Literal lit) {
    155             std::string text = source.substr(start,current-start);
    156             Token tk = Token(type, text, lit, line);
    157             tokens.push_back(tk);
    158         }
    159 
    160         void addToken(TokenType tp) {
    161             addToken(tp, {});
    162         }
    163 
    164         void scanToken() {
    165             char c = advance();
    166                 switch (c) {
    167                 case '(':
    168                     addToken(TokenType::LEFT_PAREN);
    169                     break;
    170                 case ')':
    171                     addToken(TokenType::RIGHT_PAREN);
    172                     break;
    173                 case '{':
    174                     addToken(TokenType::LEFT_BRACE);
    175                     break;
    176                 case '}':
    177                     addToken(TokenType::RIGHT_BRACE);
    178                     break;
    179                 case ',':
    180                     addToken(TokenType::COMMA);
    181                     break;
    182                 case '.':
    183                     addToken(TokenType::DOT);
    184                     break;
    185                 case '-':
    186                     addToken(TokenType::MINUS);
    187                     break;
    188                 case '+':
    189                     addToken(TokenType::PLUS);
    190                     break;
    191                 case ';':
    192                     addToken(TokenType::SEMICOLON);
    193                     break;
    194                 case '*':
    195                     addToken(TokenType::STAR);
    196                     break;
    197                 case '\n':
    198                     line += 1;
    199                     break;
    200                 case ' ':
    201                 case '\t':
    202                 case '\r':
    203                     break;
    204                 case '!':
    205                     if(match('=')){
    206                         addToken(TokenType::BANG_EQUAL);
    207                     } else {
    208                         addToken(TokenType::BANG);
    209                     }
    210                     break;
    211                 case '=':
    212                     if(match('=')){
    213                         addToken(TokenType::EQUAL_EQUAL);
    214                     } else {
    215                         addToken(TokenType::EQUAL);
    216                     }
    217                     break;
    218                 case '<':
    219                     if(match('=')){
    220                         addToken(TokenType::LESS_EQUAL);
    221                     } else {
    222                         addToken(TokenType::LESS);
    223                     }
    224                     break;
    225                 case '>':
    226                     if(match('=')){
    227                         addToken(TokenType::GREATER_EQUAL);
    228                     } else {
    229                         addToken(TokenType::GREATER);
    230                     }
    231                     break;
    232                 case '/':
    233                     if(match('/')){
    234                         while(peek() != '\n' && current < source.size()) {
    235                             advance();
    236                         }
    237                     } else if(match('*')) {
    238                         while((peek() != '*' && doublePeek() != '/' ) && current < source.size()) {
    239                             advance();
    240                         }
    241 
    242                         if(current < source.size()) {
    243                             advance();
    244                         }
    245 
    246                         if(current < source.size()) {
    247                             advance();
    248                         }
    249 
    250                     } else {
    251                         addToken(TokenType::SLASH);
    252                     }
    253                     break;
    254                 case '"':
    255                     string();
    256                     break;
    257                 default:
    258                     if(isDigit(c)) {
    259                         number();
    260                     } else if(isAlpha(c)) {
    261                         identifier();
    262                     } else {
    263                         lRef->error(line, "Unexpected character.");
    264                     }
    265                     break;
    266         }
    267     }
    268 };