function parseLiteral()

in packages/shell-parser/src/parser.ts [313:348]


function parseLiteral<T extends NodeType>(
  type: T,
  startChars: string,
  endChars: string,
) {
  const canHaveChildren =
    type === NodeType.Expansion || type === NodeType.String;
  const isString = type === NodeType.String;
  return (str: string, startIndex: number): BaseNode<T> => {
    const children = [];
    for (let i = startIndex + startChars.length; i < str.length; i += 1) {
      const child = canHaveChildren
        ? childAtIndex(str, i, isString, [endChars])
        : null;
      if (child !== null) {
        children.push(child);
        i = child.endIndex - 1;
      } else if (str.charAt(i) === "\\" && type !== NodeType.RawString) {
        i += 1;
      } else if (str.slice(i, i + endChars.length) === endChars) {
        return createNode<BaseNode<T>>(str, {
          startIndex,
          type,
          children,
          endIndex: i + endChars.length,
        });
      }
    }
    return createNode<BaseNode<T>>(str, {
      startIndex,
      type,
      children,
      complete: false,
    });
  };
}