identifier method

Future<void> identifier()

Implementation

Future<void> identifier() async {
  while (isAlphaNumeric(peek())) {
    advance();
  }
  String text = source.substring(start, current);
  if (text == 'import') {
    while (peek() == ' ' || peek() == '\t') {
      advance();
    }
    if (peek() != '"' && peek() != "'") {
      throw Exception("Expect '\"' after import.");
    }
    advance();

    String filePath = "";
    while ((peek() != '"' && peek() != "'") && !isAtEnd()) {
      filePath += advance();
    }

    if (isAtEnd()) throw Exception("Unterminated string in import.");

    advance(); // pass double quote
    advance(); // pass semicolon
    var oldFilePath = reader!.pathOrUrl;
    final currentDirectory = Uri.parse(oldFilePath).resolve(filePath);
    reader!.path = currentDirectory.toString();
    if (loadedFiles.contains(currentDirectory.path)) {
      return;
    }
    loadedFiles.add(currentDirectory.path);
    Scanner scanner = Scanner('', reader: reader, loadedFiles: loadedFiles);
    List<Token> tks = await scanner.scanTokens(isBase: false);
    reader!.path = oldFilePath;
    tokens.addAll(tks);
    return;
  }
  TokenType type = TokenType.IDENTIFIER;
  if (keywords.containsKey(text)) {
    type = keywords[text]!;
  }
  addToken(type);
}