Example #1
0
 this.parse = function(val) {
   var functionName = thisFileName + "parse()";
   var fileString;
   var data = {
     str : "",
     temp : ""
   };
   var result;
   var filename = val.substring(1);
   try {
     fileString = fs.readFileSync(filename, "utf-8");
   } catch (e) {
     throw new Error(fsmsg(functionName, "error reading command line file '" + val + "'", e));
   }
   var grammar = new fileContentGrammar();
   var parser = new apglib.parser();
   parser.ast = new apglib.ast();
   parser.trace = new apglib.trace();
   parser.trace.filter.operators["<all>"] = true;
   parser.ast.callbacks["any-token"] = semAnyToken;
   parser.ast.callbacks["token"] = semToken;
   parser.ast.callbacks["dvalue"] = semDValue;
   parser.ast.callbacks["svalue"] = semSValue;
   var chars = apglib.utils.stringToChars(fileString);
   try {
     result = parser.parse(grammar, "file", chars);
   } catch (e) {
     var html = parser.trace.displayHtml("good input");
     apglib.utils.htmlToPage(html, "html/filecontent.html");
   }
   if (result.success === false) {
     throw new Error(thisFileName + "parse: parse of command line file '" + val + "' failed");
   }
   parser.ast.translate(data);
   return data.str;
 }
Example #2
0
function parse(text, debug) {

  var parser=new apglib.parser();

  // (see the apg-js2-examples/trace/setup.html example for details on filtering the records).
  if (debug) {
    parser.trace=new apglib.trace();
    parser.trace.setMaxRecords(100000);
    if (debug!=-1) {
      var r=parser.trace.filter.rules;
      r["array"]=true;
      r["begin-array"]=true;
      r["begin-object"]=true;
      r["comma-separator"]=true;
      r["comment"]=true;
      r["end-array"]=true;
      r["end-object"]=true;
      r["false"]=true;
      r["hjson-text"]=true;
      r["json-string"]=true;
      r["keyname"]=true;
      r["lf-separator"]=true;
      r["member"]=true;
      r["ml-string"]=true;
      r["name"]=true;
      r["name-separator"]=true;
      r["null"]=true;
      r["number"]=true;
      r["object"]=true;
      r["ql-string"]=true;
      r["root-object"]=true;
      r["string"]=true;
      r["true"]=true;
      r["value"]=true;
      r["value-separator"]=true;
    }
  }

  var output=[];

  function addMatch(name) {
    var match=function(result, chars, phraseIndex, data) {
      if (result.state === id.MATCH) {
        data.push(name+"="+utils.charsToString(chars, phraseIndex, result.phraseLength));
      }
    }
    parser.callbacks[name]=match;
  }

  ["object", "array", "member", "value"].forEach(name => addMatch(name));
  ["name", "comment"].forEach(name => addMatch(name));
  ["number", "ql-string", "ml-string", "json-string"].forEach(name => addMatch(name));

  var result=parser.parse(hjsonGrammar, startRule, utils.stringToChars(text), output);
  result.output=output;

  if (debug)
    result.trace=parser.trace.toHtmlPage("ascii", "default trace");

  return result; // see result.success
}