{"version":3,"file":"expression.cjs","names":["BaseOutputParser","ASTParser","MasterHandler"],"sources":["../../src/output_parsers/expression.ts"],"sourcesContent":["import { BaseOutputParser } from \"@langchain/core/output_parsers\";\nimport { MasterHandler } from \"./expression_type_handlers/factory.js\";\nimport { ParsedType } from \"./expression_type_handlers/types.js\";\nimport { ASTParser } from \"./expression_type_handlers/base.js\";\nimport type { ExpressionNode } from \"../types/expression-parser.js\";\n\nexport type ParseFunction = Awaited<\n  ReturnType<typeof ASTParser.importASTParser>\n>;\n\n/**\n * We need to be able to handle the following cases:\n * ExpressionStatement\n *  CallExpression\n *      Identifier | MemberExpression\n *      ExpressionLiterals: [\n *          CallExpression\n *          StringLiteral\n *          NumericLiteral\n *          ArrayLiteralExpression\n *              ExpressionLiterals\n *          ObjectLiteralExpression\n *              PropertyAssignment\n *                  Identifier\n *                  ExpressionLiterals\n *      ]\n */\n\nexport class ExpressionParser extends BaseOutputParser<ParsedType> {\n  lc_namespace = [\"langchain\", \"output_parsers\", \"expression\"];\n\n  parser: ParseFunction;\n\n  /**\n   * We should separate loading the parser into its own function\n   * because loading the grammar takes some time. If there are\n   * multiple concurrent parse calls, it's faster to just wait\n   * for building the parser once and then use it for all\n   * subsequent calls. See expression.test.ts for an example.\n   */\n  async ensureParser() {\n    if (!this.parser) {\n      this.parser = await ASTParser.importASTParser();\n    }\n  }\n\n  /**\n   * Parses the given text. It first ensures the parser is loaded, then\n   * tries to parse the text. If the parsing fails, it throws an error. If\n   * the parsing is successful, it returns the parsed expression.\n   * @param text The text to be parsed.\n   * @returns The parsed expression\n   */\n  async parse(text: string) {\n    await this.ensureParser();\n\n    try {\n      const program = this.parser(text);\n\n      const node = program.body;\n      if (!ASTParser.isExpressionStatement(node)) {\n        throw new Error(\n          `Expected ExpressionStatement, got ${(node as ExpressionNode).type}`\n        );\n      }\n\n      const { expression: expressionStatement } = node;\n      if (!ASTParser.isCallExpression(expressionStatement)) {\n        throw new Error(\"Expected CallExpression\");\n      }\n      const masterHandler = MasterHandler.createMasterHandler();\n      return await masterHandler.handle(expressionStatement);\n    } catch (err) {\n      throw new Error(`Error parsing ${err}: ${text}`);\n    }\n  }\n\n  /**\n   * This method is currently empty, but it could be used to provide\n   * instructions on the format of the input text.\n   * @returns string\n   */\n  getFormatInstructions(): string {\n    return \"\";\n  }\n}\n\nexport * from \"./expression_type_handlers/types.js\";\n\nexport { MasterHandler } from \"./expression_type_handlers/factory.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,IAAa,mBAAb,cAAsCA,+BAAAA,iBAA6B;CACjE,eAAe;EAAC;EAAa;EAAkB;EAAa;CAE5D;;;;;;;;CASA,MAAM,eAAe;AACnB,MAAI,CAAC,KAAK,OACR,MAAK,SAAS,MAAMC,aAAAA,UAAU,iBAAiB;;;;;;;;;CAWnD,MAAM,MAAM,MAAc;AACxB,QAAM,KAAK,cAAc;AAEzB,MAAI;GAGF,MAAM,OAFU,KAAK,OAAO,KAAK,CAEZ;AACrB,OAAI,CAACA,aAAAA,UAAU,sBAAsB,KAAK,CACxC,OAAM,IAAI,MACR,qCAAsC,KAAwB,OAC/D;GAGH,MAAM,EAAE,YAAY,wBAAwB;AAC5C,OAAI,CAACA,aAAAA,UAAU,iBAAiB,oBAAoB,CAClD,OAAM,IAAI,MAAM,0BAA0B;AAG5C,UAAO,MADeC,gBAAAA,cAAc,qBAAqB,CAC9B,OAAO,oBAAoB;WAC/C,KAAK;AACZ,SAAM,IAAI,MAAM,iBAAiB,IAAI,IAAI,OAAO;;;;;;;;CASpD,wBAAgC;AAC9B,SAAO"}