0

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

  1. 添加 pegjs

  2. 使用var parserFile创建解析器

  3. 使用var makeFile创建文件

  4. 添加 contentFile、nameFile 和 var contentFile、nameFile在此处:var makeFile

  5. 在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))));


  • 未捕获的 SyntaxError:意外的标记 ')'

  • 第 1 行,第 1 列:应为“。” 但找到了“2”。

4

1 回答 1

1

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

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

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

开头的“(”或缺少的结尾“)”都是语法错误。分配给文件的右侧的函数没有返回语句,所以我猜是开头的“(”是错字,而不是 IIFE。

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

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

一旦语法错误得到修复,就会出现运行时错误。

于 2019-10-06T03:53:59.383 回答