string method

void string(
  1. String c
)

Implementation

void string(String c) {
  bool interplote = false;
  while (peek() != c && !isAtEnd()) {
    if (peek() == '\n') line++;
    if (peek() == '\$') {
      advance();
      if (peek() == '{') {
        if (interplote == true) {
          addToken(TokenType.PLUS);
        }
        if (current - start > 2) {
          addToken1(
              TokenType.STRING, source.substring(start + 1, current - 1));
          addToken(TokenType.PLUS);
        }
        interplote = true;
        addToken(TokenType.LEFT_PAREN);
        advance();
        while (peek() != '}' && !isAtEnd()) {
          start = current;
          scanToken();
        }
        if (peek() == '}') {
          addToken(TokenType.RIGHT_PAREN);
          addToken(TokenType.DOT);
          addToken2(TokenType.IDENTIFIER, "toString");
          start = current;
          advance();
        } else {
          error(line, "Unterminated expression in string interpolation.");
        }
      }
    } else {
      advance();
    }
  }

  if (isAtEnd()) {
    error(line, "Unterminated string.");
    return;
  }

  advance();

  if (interplote) {
    addToken(TokenType.PLUS);
  }
  addToken1(TokenType.STRING, source.substring(start + 1, current - 1));
}