parseTokens method

List<ASTNode> parseTokens(
  1. Token token, {
  2. HTSource? source,
  3. ParseStyle? style,
})

Convert tokens into a list of ASTNode by a certain grammar rules set.

If source is not specified, the token will not be binded to a HTSource.

If style is not specified, will use source.sourceType to determine, if source is null at the same time, will use ParseStyle.script by default.

Implementation

List<ASTNode> parseTokens(Token token,
    {HTSource? source, ParseStyle? style}) {
  // create new list of errors here, old error list is still usable
  errors = <HTError>[];
  final nodes = <ASTNode>[];
  setTokens(token: token);
  currentSource = source;
  currrentFileName = source?.fullName;
  if (style == null) {
    if (currentSource != null) {
      final sourceType = currentSource!.type;
      if (sourceType == HTResourceType.hetuModule) {
        style = ParseStyle.module;
      } else if (sourceType == HTResourceType.hetuScript ||
          sourceType == HTResourceType.hetuLiteralCode) {
        style = ParseStyle.script;
      } else if (sourceType == HTResourceType.hetuValue) {
        style = ParseStyle.expression;
      } else {
        return nodes;
      }
    } else {
      style = ParseStyle.script;
    }
  }

  while (curTok.type != Semantic.endOfFile) {
    final stmt = parseStmt(style: style);
    if (stmt != null) {
      if (stmt is ASTEmptyLine && style == ParseStyle.expression) {
        continue;
      }
      nodes.add(stmt);
    }
  }
  return nodes;
}