Search code examples
javascriptpegjs

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


I am trying to create a parser, a file. I am trying to use this file created to be parsed by the parser. The steps are:

  1. Add pegjs with

  2. Create an parser with var parserFile

  3. Create file with var makeFile

  4. Add contentFile, nameFile with var contentFile, nameFile and here: var makeFile

  5. Use parser in var makeFile with 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))));


  • Uncaught SyntaxError: Unexpected token ')'

  • Line 1, column 1: Expected "." but "2" found.


Solution

  • Until syntax errors are fixed, runtime errors should probably be ignored. There is a syntax error here:

    var file = (function () {  // <== opening bracket signals the possible start of an IIFE
      ...
    }  // <== closing bracket ")" expected
    
    // call, file & grammar 
    // show
    console.log( ...
    

    Either the opening "(" or the missing closing ")" are syntax errors. The function on the right hand side of the assignment to file doesn't have a return statement, so I guess that it's the opening "(" that is the typo and an IIFE is not intended.

    The first argument passed to runFile, i.e. contentFile, isn't used in the function.

    The runFile function doesn't have a return statement so returns undefined. Assigning the result to finishFile seems pointless.

    Once the syntax errors are fixed, there will be runtime errors.