/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/@rokucommunity/brs@0.47.4/lib/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,r,t,n){void 0===n&&(n=t);var o=Object.getOwnPropertyDescriptor(r,t);o&&!("get"in o?!r.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return r[t]}}),Object.defineProperty(e,n,o)}:function(e,r,t,n){void 0===n&&(n=t),e[n]=r[t]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,r){Object.defineProperty(e,"default",{enumerable:!0,value:r})}:function(e,r){e.default=r}),__importStar=this&&this.__importStar||function(){var e=function(r){return e=Object.getOwnPropertyNames||function(e){var r=[];for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&(r[r.length]=t);return r},e(r)};return function(r){if(r&&r.__esModule)return r;var t={};if(null!=r)for(var n=e(r),o=0;o<n.length;o++)"default"!==n[o]&&__createBinding(t,r,n[o]);return __setModuleDefault(t,r),t}}(),__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.parser=exports.preprocessor=exports.types=exports.lexer=void 0,exports.execute=execute,exports.getCoverageResults=getCoverageResults,exports.lexParseSync=lexParseSync,exports.createExecuteWithScope=createExecuteWithScope,exports.repl=repl;const fs=__importStar(require("fs")),readline=__importStar(require("readline")),chalk_1=__importDefault(require("chalk")),util_1=require("util"),mkdtemp=(0,util_1.promisify)(fs.mkdtemp),decompress_1=__importDefault(require("decompress")),sanitize_filename_1=__importDefault(require("sanitize-filename")),lexer_1=require("./lexer"),PP=__importStar(require("./preprocessor"));exports.preprocessor=PP;const scenegraph_1=require("./scenegraph"),parser_1=require("./parser"),interpreter_1=require("./interpreter"),extensions_1=require("./extensions"),BrsError=__importStar(require("./Error")),LexerParser=__importStar(require("./LexerParser")),coverage_1=require("./coverage"),stdlib_1=require("./stdlib"),_lexer=__importStar(require("./lexer"));exports.lexer=_lexer;const BrsTypes=__importStar(require("./brsTypes"));exports.types=BrsTypes;const _parser=__importStar(require("./parser"));exports.parser=_parser;const url_1=require("url"),path=__importStar(require("path")),p_settle_1=__importDefault(require("p-settle")),os_1=__importDefault(require("os")),Environment_1=require("./interpreter/Environment");let coverageCollector=null;async function execute(e,r){let{lexerParserFn:t,interpreter:n}=await loadFiles(r),o=new Array;e.forEach((e=>{o.push({type:"text/brightscript",uri:e})}));let i=await t(o);return n.exec(i)}async function loadFiles(e){const r=Object.assign(Object.assign({},interpreter_1.defaultExecutionOptions),e);let t=await PP.getManifest(r.root),n=e.isComponentLibrary?t.get("sg_component_libs_provided"):void 0;if("boolean"==typeof n)throw new Error("Encountered invalid boolean value for manifest key 'sg_component_libs_provided'");if(e.isComponentLibrary&&null==n)throw new Error("Could not find required manifest key 'sg_component_libs_provided' in component library");let o=await(0,scenegraph_1.getComponentDefinitionMap)(r.root,r.componentDirs,n),i=Array.from(o.values()).map((e=>({component:e,libraries:e.children.filter((e=>"componentlibrary"===e.name.toLowerCase()))}))).filter((({libraries:e})=>e&&e.length>0)),s=new Map,a=[];for(let{component:t,libraries:n}of i)for(let o of n){let n=o.fields.uri;if(!n)continue;if(n.startsWith("http://")||n.startsWith("https://")){r.stderr.write(`WARNING: Only pkg:/-local component libraries are supported; ignoring '${n}'\n`);continue}let i=r.root.replace(/[\/\\]+/g,path.posix.sep),l=t.xmlPath.replace(/[\/\\]+/g,path.posix.sep);"win32"===process.platform&&(i=i.replace(/^[a-zA-Z]:/,""),l=l.replace(/^[a-zA-Z]:/,""));let p=new url_1.URL(n,`pkg:/${path.posix.relative(i,l)}`).toString();if(s.has(p))continue;let c=(0,sanitize_filename_1.default)(p),u=await mkdtemp(path.join(os_1.default.tmpdir(),`brs-${c}`),"utf8");s.set(p,u);let _=path.join(r.root,new url_1.URL(n).pathname);a.push((0,decompress_1.default)(_,u).then((()=>loadFiles(Object.assign(Object.assign({},e),{root:u,componentDirs:[],isComponentLibrary:!0})))))}o.forEach((e=>{if(!(e.scripts.length<1))try{e.scripts=e.scripts.map((e=>(e.uri&&(e.uri=path.join(r.root,new url_1.URL(e.uri).pathname)),e)))}catch(r){throw new Error(`Encountered an error when parsing component ${e.name}: ${r}`)}}));const l=await interpreter_1.Interpreter.withSubEnvsFromComponents(o,t,r);if(!l)throw new Error("Unable to build interpreter.");l.manifest=t;let p=(await(0,p_settle_1.default)(a)).filter((e=>e.isFulfilled)).map((e=>e.value.interpreter));l.mergeNodeDefinitionsWith(p),await(0,stdlib_1.loadTranslationFiles)(l,r.root);let c=LexerParser.getLexerParserFn(t,e);return r.generateCoverage&&(coverageCollector=new coverage_1.CoverageCollector(r.root,c),await coverageCollector.crawlBrsFiles(),l.setCoverageCollector(coverageCollector)),{lexerParserFn:c,interpreter:l}}function getCoverageResults(){if(coverageCollector)return coverageCollector.getCoverage()}function lexParseSync(e,r){const t=Object.assign(Object.assign({},interpreter_1.defaultExecutionOptions),r);let n=PP.getManifestSync(t.root);return e.map((e=>{let r=new lexer_1.Lexer,o=new PP.Preprocessor,i=new parser_1.Parser;[r,o,i].forEach((e=>e.onError(BrsError.getLoggerUsing(t.stderr))));let s=fs.readFileSync(e,"utf8"),a=r.scan(s,e),l=o.preprocess(a.tokens,n),p=i.parse(l.processedTokens);if(p.errors.length>0)throw"Error occurred parsing";return p.statements})).reduce(((e,r)=>[...e,...r]),[])}async function createExecuteWithScope(e,r){let{lexerParserFn:t,interpreter:n}=await loadFiles(r),o=new Array;e.forEach((e=>{o.push({type:"text/brightscript",uri:e})}));let i=await t(o);return n.exec(i),n.errors=[],(e,r)=>{n.environment.resetMocks(),(0,extensions_1.resetTestData)();let t=lexParseSync(e,n.options),o=[],i=n.inSubEnv((e=>{let n=e.exec(t,...r)[0]||BrsTypes.BrsInvalid.Instance;return o=e.errors,e.errors=[],n}));if(o.length)throw o;return i}}function repl(){const e=new interpreter_1.Interpreter;e.onError(BrsError.getLoggerUsing(process.stderr));const r=readline.createInterface({input:process.stdin,output:process.stdout});r.setPrompt(`${chalk_1.default.magenta("brs")}> `),r.on("line",(t=>{var n,o,i;const s=t.trim().toLowerCase();if(["quit","exit","q"].includes(s))process.exit();else{if(["cls","clear"].includes(s))return process.stdout.write("c"),void r.prompt();if(["help","hint"].includes(s))return printHelp(),void r.prompt();if(["var","vars"].includes(null===(n=t.split(" ")[0])||void 0===n?void 0:n.toLowerCase().trim())){const n=null!==(i=null===(o=t.split(" ")[1])||void 0===o?void 0:o.toLowerCase().trim())&&void 0!==i?i:"function";let s=Environment_1.Scope.Function;return"global"===n?(s=Environment_1.Scope.Global,console.log(chalk_1.default.cyanBright("\r\nGlobal variables:\r\n"))):"module"===n?(s=Environment_1.Scope.Module,console.log(chalk_1.default.cyanBright("\r\nModule variables:\r\n"))):console.log(chalk_1.default.cyanBright("\r\nLocal variables:\r\n")),console.log(chalk_1.default.cyanBright(e.formatVariables(s))),void r.prompt()}}let a=run(t,interpreter_1.defaultExecutionOptions,e);a&&a.map((e=>{e!==BrsTypes.BrsInvalid.Instance&&console.log((0,interpreter_1.colorize)(e.toString()))})),r.prompt()})),console.log((0,interpreter_1.colorize)("type `help` to see the list of valid REPL commands.\r\n")),r.prompt()}function run(e,r=interpreter_1.defaultExecutionOptions,t){const n=new lexer_1.Lexer,o=new parser_1.Parser,i=BrsError.getLoggerUsing(r.stderr);n.onError(i),o.onError(i);const s=n.scan(e,"REPL");if(s.errors.length>0)return;const a=o.parse(s.tokens);if(!(a.errors.length>0)&&0!==a.statements.length)try{return t.exec(a.statements)}catch(e){return}}function printHelp(){let e="\r\n";e+="REPL Command List:\r\n",e+="   print|?           Print variable value or expression\r\n",e+="   var|vars [scope]  Display variables and their types/values\r\n",e+="   help|hint         Show this REPL command list\r\n",e+="   clear|cls         Clear terminal screen\r\n",e+="   exit|quit|q       Terminate REPL session\r\n\r\n",e+="   Type any valid BrightScript expression for a live compile and run.\r\n",console.log(chalk_1.default.cyanBright("\r\nREPL Command List:\r\n   print|?           Print variable value or expression\r\n   var|vars [scope]  Display variables and their types/values\r\n   help|hint         Show this REPL command list\r\n   clear|cls         Clear terminal screen\r\n   exit|quit|q       Terminate REPL session\r\n\r\n   Type any valid BrightScript expression for a live compile and run.\r\n"))}
//# sourceMappingURL=/sm/5108ea8a0bc8303c02dd31ee1c7dcf02b922ed40599a429a5a5ccaecd68520f6.map