scanStringLiteral method

Token scanStringLiteral(
  1. int x
)

Implementation

Token scanStringLiteral(int x) {
  List<int> buffer =
      <int>[]; // String value without quotes, after resolving escapes.
  int quote = x;
  x = next();
  while (x != quote) {
    if (x == char.BACKSLASH) {
      x = next();
      switch (x) {
        case char.$b:
          buffer.add(char.BS);
          x = next();
          break;
        case char.$f:
          buffer.add(char.FF);
          x = next();
          break;
        case char.$n:
          buffer.add(char.LF);
          x = next();
          break;
        case char.$r:
          buffer.add(char.CR);
          x = next();
          break;
        case char.$t:
          buffer.add(char.TAB);
          x = next();
          break;
        case char.$v:
          buffer.add(char.VTAB);
          x = next();
          break;

        case char.$x:
          ++index;
          buffer.add(scanHexSequence(2));
          x = current;
          break;

        case char.$u:
          ++index;
          buffer.add(scanHexSequence(4));
          x = current;
          break;

        case char.$0:
        case char.$1:
        case char.$2:
        case char.$3:
        case char.$4:
        case char.$5:
        case char.$6:
        case char.$7: // Octal escape
          int value = (x - char.$0);
          x = next();
          while (isDigit(x)) {
            int nextValue = (value << 3) + (x - char.$0);
            if (nextValue > 127) break;
            value = nextValue;
            x = next();
          }
          buffer.add(value);
          break; // OK

        case char.LF:
        case char.LS:
        case char.PS:
          ++currentLine;
          x = next(); // just continue on next line
          break;

        case char.CR:
          ++currentLine;
          x = next();
          if (x == char.LF) {
            x = next(); // Escape entire CR-LF sequence
          }
          break;

        case char.SQUOTE:
        case char.DQUOTE:
        case char.BACKSLASH:
        default:
          buffer.add(x);
          x = next();
          break;
      }
    } else if (isEOL(x)) {
      // Note: EOF counts as an EOL
      fail("Unterminated string literal");
    } else {
      buffer.add(x); // ordinary char
      x = next();
    }
  }
  ++index; // skip ending quote
  String value = new String.fromCharCodes(buffer);
  return emitValueToken(Token.STRING)..value = value;
}