lastToken static method

Token lastToken(
  1. String source
)

Implementation

static Token lastToken(String source) {
  // Start by finding the start and logical end of the last line.
  int startPos = source.lastIndexOf('\n') + 1;
  int commentStart = commentStartPos(source, startPos);

  // Walk back from end of string or start of comment, skipping whitespace.
  int endPos = (commentStart >= 0 ? commentStart - 1 : source.length - 1);
  while (endPos >= 0 && isWhitespace(source[endPos])) {
    endPos--;
  }
  if (endPos < 0) return Token.eol;

  // Find the start of that last token.
  // There are several cases to consider here.
  int tokStart = endPos;
  String c = source[endPos];

  if (isIdentifier(c)) {
    while (tokStart > startPos && isIdentifier(source[tokStart - 1])) {
      tokStart--;
    }
  } else if (c == '"') {
    bool inQuote = true;
    while (tokStart > startPos) {
      tokStart--;
      if (source[tokStart] == '"') {
        inQuote = !inQuote;
        if (!inQuote && tokStart > startPos && source[tokStart - 1] != '"') {
          break;
        }
      }
    }
  } else if (c == '=' && tokStart > startPos) {
    String c2 = source[tokStart - 1];
    if (c2 == '>' || c2 == '<' || c2 == '=' || c2 == '!') {
      tokStart--;
    }
  }
  // Now use the standard lexer to grab just that bit.
  Lexer lex = Lexer(source);
  lex.position = tokStart;
  return lex.dequeue();
}