src/Scanner.cpp
changeset 92 e3974e7360e9
parent 86 32ad0523b309
child 133 7797936ba34c
     1.1 --- a/src/Scanner.cpp	Wed Jul 25 00:48:08 2012 -0700
     1.2 +++ b/src/Scanner.cpp	Wed Jul 25 01:30:33 2012 -0700
     1.3 @@ -1,4 +1,5 @@
     1.4 -/*
     1.5 +/* vim: set ts=4 sw=4 tw=99 et:
     1.6 + *
     1.7   * Copyright (C) 2012 David Anderson
     1.8   *
     1.9   * This file is part of JITCraft.
    1.10 @@ -18,6 +19,8 @@
    1.11  #include <assert.h>
    1.12  #include <ctype.h>
    1.13  #include <math.h>
    1.14 +#include <limits.h>
    1.15 +#include <string.h>
    1.16  #include "Scanner.h"
    1.17  #include "Utility.h"
    1.18  
    1.19 @@ -263,76 +266,97 @@
    1.20                  return TOK_ERROR;
    1.21              c = char(code);
    1.22          }
    1.23 -        literal_ += c;
    1.24 +        if (!literal_.append(c))
    1.25 +            return TOK_ERROR;
    1.26      }
    1.27  
    1.28 +    if (!literal_.append('\0'))
    1.29 +        return TOK_ERROR;
    1.30 +
    1.31      return TOK_STRING_LITERAL;
    1.32  }
    1.33  
    1.34  TokenKind
    1.35  Scanner::numberLiteral(char first)
    1.36  {
    1.37 -    literal_ = first;
    1.38 +    literal_.clear();
    1.39 +    if (!literal_.append(first))
    1.40 +        return TOK_ERROR;
    1.41  
    1.42      char c;
    1.43      for (;;) {
    1.44          c = getChar();
    1.45          if (!IsDigit(c))
    1.46              break;
    1.47 -        literal_ += c;
    1.48 +        if (!literal_.append(c))
    1.49 +            return TOK_ERROR;
    1.50      }
    1.51  
    1.52      if (c != '.') {
    1.53          putChar(c);
    1.54 +        if (!literal_.append('\0'))
    1.55 +            return TOK_ERROR;
    1.56          return TOK_INTEGER_LITERAL;
    1.57      }
    1.58 -    literal_ += c;
    1.59 +    if (!literal_.append(c))
    1.60 +        return TOK_ERROR;
    1.61  
    1.62      c = getChar();
    1.63      if (!IsDigit(c)) {
    1.64          cc_.reportError(begin(), Message_ExpectedDigitForFloat, c);
    1.65          return TOK_ERROR;
    1.66      }
    1.67 -    literal_ += c;
    1.68 +    if (!literal_.append(c))
    1.69 +        return TOK_ERROR;
    1.70  
    1.71      for (;;) {
    1.72          c = getChar();
    1.73          if (!IsDigit(c))
    1.74              break;
    1.75 -        literal_ += c;
    1.76 +        if (!literal_.append(c))
    1.77 +            return TOK_ERROR;
    1.78      }
    1.79  
    1.80      if (c != 'e') {
    1.81          putChar(c);
    1.82      } else {
    1.83 -        literal_ += c;
    1.84 +        if (!literal_.append(c))
    1.85 +            return TOK_ERROR;
    1.86          c = getChar();
    1.87          if (c == '-') {
    1.88 -            literal_ += c;
    1.89 +            if (!literal_.append(c))
    1.90 +                return TOK_ERROR;
    1.91              c = getChar();
    1.92          }
    1.93          if (!IsDigit(c)) {
    1.94              cc_.reportError(begin(), Message_ExpectedDigitForFloat, c);
    1.95              return TOK_ERROR;
    1.96          }
    1.97 -        literal_ += c;
    1.98 +        if (!literal_.append(c))
    1.99 +            return TOK_ERROR;
   1.100          for (;;) {
   1.101              c = getChar();
   1.102              if (!IsDigit(c)) {
   1.103                  putChar(c);
   1.104                  break;
   1.105              }
   1.106 -            literal_ += c;
   1.107 +            if (!literal_.append(c))
   1.108 +                return TOK_ERROR;
   1.109          }
   1.110      }
   1.111  
   1.112 +    if (!literal_.append('\0'))
   1.113 +        return TOK_ERROR;
   1.114 +
   1.115      return TOK_FLOAT_LITERAL;
   1.116  }
   1.117  
   1.118  TokenKind
   1.119  Scanner::identifier(char first)
   1.120  {
   1.121 -    literal_ = first;
   1.122 +    literal_.clear();
   1.123 +    if (!literal_.append(first))
   1.124 +        return TOK_ERROR;
   1.125  
   1.126      char c;
   1.127      for (;;) {
   1.128 @@ -342,36 +366,40 @@
   1.129                  putChar(c);
   1.130              break;
   1.131          }
   1.132 -        literal_ += c;
   1.133 +        if (!literal_.append(c))
   1.134 +            return TOK_ERROR;
   1.135      }
   1.136  
   1.137 -    if (literal_ == "new")
   1.138 +    if (!literal_.append('\0'))
   1.139 +        return TOK_ERROR;
   1.140 +
   1.141 +    if (!strcmp(literal(),  "new"))
   1.142          return TOK_NEW;
   1.143 -    if (literal_ == "for")
   1.144 +    if (!strcmp(literal(),  "for"))
   1.145          return TOK_FOR;
   1.146 -    if (literal_ == "return")
   1.147 +    if (!strcmp(literal(),  "return"))
   1.148          return TOK_RETURN;
   1.149 -    if (literal_ == "if")
   1.150 +    if (!strcmp(literal(),  "if"))
   1.151          return TOK_IF;
   1.152 -    if (literal_ == "else")
   1.153 +    if (!strcmp(literal(),  "else"))
   1.154          return TOK_ELSE;
   1.155 -    if (literal_ == "enum")
   1.156 +    if (!strcmp(literal(),  "enum"))
   1.157          return TOK_ENUM;
   1.158 -    if (literal_ == "while")
   1.159 +    if (!strcmp(literal(),  "while"))
   1.160          return TOK_WHILE;
   1.161 -    if (literal_ == "do")
   1.162 +    if (!strcmp(literal(),  "do"))
   1.163          return TOK_DO;
   1.164 -    if (literal_ == "break")
   1.165 +    if (!strcmp(literal(),  "break"))
   1.166          return TOK_BREAK;
   1.167 -    if (literal_ == "continue")
   1.168 +    if (!strcmp(literal(),  "continue"))
   1.169          return TOK_CONTINUE;
   1.170 -    if (literal_ == "forward")
   1.171 +    if (!strcmp(literal(),  "forward"))
   1.172          return TOK_FORWARD;
   1.173 -    if (literal_ == "public")
   1.174 +    if (!strcmp(literal(),  "public"))
   1.175          return TOK_PUBLIC;
   1.176 -    if (literal_ == "const")
   1.177 +    if (!strcmp(literal(),  "const"))
   1.178          return TOK_CONST;
   1.179 -    if (literal_ == "native")
   1.180 +    if (!strcmp(literal(),  "native"))
   1.181          return TOK_NATIVE;
   1.182      return (c == ':') ? TOK_LABEL : TOK_NAME;
   1.183  }