readQuotedString method

String readQuotedString()

Read a string enclosed in double quotes

Implementation

String readQuotedString() {
  _quotedString = true;
  var nxtChar = next();

  if (nxtChar != '"' && nxtChar != "'") {
    throw Exception(
        'Expected double quotes or single quotes at the start of a string');
  }
  StringBuffer result = StringBuffer();
  bool escaping = false;
  String quoteDigit = nxtChar;

  while (canRead) {
    String char = next();

    if (char == '"' && quoteDigit == "\"") {
      break;
    } else if (char == '\\' && peek() == '\'' && quoteDigit == '\'') {
      escaping = true;
      continue;
    } else if (char == '\'' && quoteDigit == '\'') {
      if (!escaping) break;
    }
    escaping = false;
    result.write(char);
  }
  _quotedString = false;
  return result.toString();
}