parseInput function

List<ASTNode> parseInput(
  1. String input, {
  2. bool enableTrace = false,
  3. bool shouldLint = false,
})

Parses the given input string and returns a list of ASTNode objects representing the parsed document.

If enableTrace is true, the parser will output trace information during parsing. If shouldLint is true, the parser will output lint information for the parsed grammar.

If the parsing is successful, the method returns the list of ASTNode objects representing the document. If the parsing fails, the method prints the error message and the input source, and returns an empty list.

Implementation

List<ASTNode> parseInput(String input,
    {bool enableTrace = false, bool shouldLint = false}) {
  //parser fails to handle empty input
  if (input.isEmpty) {
    return [];
  }

  registerBuiltIns();
  final parser = LiquidGrammar().build();

  if (shouldLint) {
    print(linter(parser).join('\n\n'));
  }
  Result result;

  if (enableTrace) {
    result = trace(parser).parse(input);
  } else {
    result = parser.parse(input);
  }

  if (result is Success) {
    return (result.value as Document).children;
  }
  final lineCol = Token.lineAndColumnOf(input, result.position);

  throw ParsingException(
      result.message, input, lineCol[0], lineCol[0], result.position);
}