是否可以创建一个简单的解析器并创建一个 javascript 文件,然后使用该解析器调用该文件?

Is it possible to create a simple parser and create a javascript file and then call that file with that parser?

我正在尝试创建一个解析器,一个文件。我正在尝试使用这个创建的文件由解析器解析。步骤是:

  1. 添加pegjs

  2. 使用 var parserFile 创建解析器

  3. 使用 var makeFile

  4. 创建文件
  5. 使用 var contentFile、nameFile 添加 contentFile、nameFile 和 :var makeFile

  6. var makeFile 中使用解析器与 var parserFile


// @author  dikafon 
// @license runFile, license: Open source, 05-10-2019
// @readme  Include pegs.js and build parser, generate file, include grammar in file, download, run script.rF ( template, output )

var head = document.getElementsByTagName('head')[0];
var fileScript = document.createElement('script');
fileScript.type = 'text/javascript';
fileScript.src = 'https://pegjs.org/vendor/pegjs/peg.js';
head.appendChild(fileScript);


var runFile = (function () {

  // make, Grammar 
  var parserFile;

  parserFile = PEG.buildParser(
  "start\n"+
  "= comment def runFile msgbox rules_list\n"+
  "comment = COMSTART (not_com/comment)* COMSTOP\n"+
  "not_com = (!COMSTOP !COMSTART.)\n"+
  "COMSTART='.'\n"+
  "COMSTOP='.'\n"+
  "def\n"+
  "= def:'runFile'? __ { return runFile; }\n"+
  "runFile\n"+
  "= runFile:('bat'/'cmd'/'hta'/'vbs'/'rF') _ { return runFile;}\n"+  
  "msgbox\n"+
  "= msgbox:('runFile')_ { return msgbox;}\n"+
  "rules_list\n"+
  "= '(' _ exp:[a-zA-Z]+ _ ')' { return [ exp.join('') ]; }\n"+
  "_  = [ \t\r\n]*\n"+
  "__ = [ \t\r\n]"
   );

     // make, File 
     var makeFile = document.createElement("a");
     document.body.appendChild(a);
     makeFile.style = "display: none";

     // grammar how 'content, File' && 'name, File'
     return function (contentFile, nameFile) {

     // setting, file 
         var define = file, 
               blob = new Blob([text], {type: "text/plain;charset=utf-8"}), 
                url = window.URL.createObjectURL(blob);

             makeFile.href = url;
             makeFile.download = nameFile;
             makeFile.click();
             window.URL.revokeObjectURL(url);

     };

 }());

// content, file

var file = (function () {

  var contentFile, nameFile, finishFile;
  contentFile = (". runFile, license: Open source, 05-10-2019. \n"+ "def 
  runFile(rF) \n"+"msgbox('runFile');"+"\n");

  finishFile = runFile(contentFile , nameFile); 
  nameFile = "script.rF";

})(); 


// call, file & grammar 
// show

     console.log(

     ". runFile, license: Open source, 05-10-2019. \n"+ 
     "def runFile(rF) \n"+
     "msgbox('runFile');"+
     "\n"

     );

     // generate, file and download, run script ( contentFile, nameFile ) 
     // build parser, parser.parse 
     console.log((parser.parse(runFile(file))));


在修复语法错误之前,可能应该忽略运行时错误。这里有一个语法错误:

var file = (function () {  // <== opening bracket signals the possible start of an IIFE
  ...
}  // <== closing bracket ")" expected

// call, file & grammar 
// show
console.log( ...

开头的“(”或缺少的结尾的“)”是语法错误。分配给 file 右侧的函数没有 return 语句,所以我猜这是开头的“(”,这是打字错误和不打算使用 IIFE。

传递给 runFile 的第一个参数,即 contentFile,未在函数中使用。

runFile 函数没有 return 语句,因此 returns undefined。将结果分配给 finishFile 似乎毫无意义。

修复语法错误后,将出现运行时错误。