parseString()

in lib/lexer.js [111:174]


  parseString() {
    var quote = this.peek;
    let str = '';
    this.getch();
    let start = this.loc();
    var end;
    for (; ;) {
      if (this.peek === quote) {
        end = this.loc();
        this.getch();
        break;
      }

      var c = this.peek;
      if (this.peek === '\\') {
        this.getch();
        switch (this.peek) { // 解析转义字符
        case '0':
          c = '\0';
          break;
        case 'b':
          c = '\b';
          break;
        case 't':
          c = '\t';
          break;
        case 'n':
          c = '\n';
          break;
        case 'v':
          c = '\v';
          break;
        case 'f':
          c = '\f';
          break;
        case 'r':
          c = '\r';
          break;
        case '\'':
          c = '\'';
          break;
        case '"':
          c = '"';
          break;
        case '\\':
          c = '\\';
          break;
        default:
          this.error(`Invalid char: \\0x${this.peek}/'\\0x${this.peek.charCodeAt(0)}'`);
        }
        str += c;
        this.getch();
      } else if (this.peek) {
        str += this.peek;
        this.getch();
      } else {
        this.error('Unexpect end of file');
      }
    }

    return new StringLiteral(str, {
      start, end
    });
  }