str method

void str({
  1. required String start,
  2. required String end,
})

Implementation

void str({required String start, required String end}) {
  // Consume the lexeme used as quotes.
  final streamChar = advance();

  final int startIdx = curr, startLine = line;
  String lexeme = '';
  StreamChar prev = peek(offset: -1);
  bool loop = true;

  while (!eof() && loop) {
    // NOTE: will consume the terminating quote
    // as a side-effect.
    final next = advance();
    final bool found = next.lexeme == end;

    prev = peek(offset: -2);
    loop = !found || (found && prev.lexeme == '\\');

    // Only append to the string if the lexeme is not used
    // as a terminating quote.
    if (loop) {
      lexeme += next.lexeme;
    }
  }

  if (eof()) {
    final String lineInfo = '$startLine:$startIdx';
    addError('Unterminated string starting on line $lineInfo');

    // There's no recovering from EOF.
    return;
  }

  tokens.add(streamChar.toToken(TokenType.kString, lexeme: lexeme));
}