parseInput function

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

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

Parameters

  • input: The Liquid template string to parse.
  • config: Optional delimiter configuration. If null, uses standard delimiters.
  • enableTrace: If true, outputs trace information during parsing.
  • shouldLint: If true, outputs lint information for the parsed grammar.

Example

// Standard delimiters
final nodes = parseInput('Hello {{ name }}!');

// Custom delimiters
final config = LiquidConfig(tagStart: '[%', tagEnd: '%]', varStart: '[[', varEnd: ']]');
final nodes = parseInput('Hello [[ name ]]!', config: config);

If the parsing is successful, the method returns the list of ASTNode objects representing the document. If the parsing fails, throws a ParsingException with details about the error.

Implementation

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

  final parser = _getCachedParser(config);

  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,
  );
}