Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function mkParser(pegjsFilename, outputFilename) {
var grammar = fs.readFileSync(pegjsFilename, 'utf8');
try {
var parserSrc = peg.generate(grammar, {
format: 'bare',
output: 'source',
optimize: "speed" // or "size"
});
} catch (e) {
console.error(e);
process.exit(1);
}
parserSrc = 'module.exports =\n' + parserSrc.replace("\n(function() {\n", "\nfunction(plywood, chronoshift) {\n").replace("\n})()", "\n}");
fs.writeFileSync(outputFilename, parserSrc, 'utf8');
}
// The content of the grammar file
const pegContent = fs.readFileSync(__dirname + path.sep + pegFileName, 'utf8');
// Only these entries are allowed in the grammar file
const allowedStartRules = [
'CompilationUnit',
'TypeDeclaration',
'ClassBodyDeclaration',
'BlockStatement',
'Expression',
'Type',
];
// The generated peg parser
// Use cache to improve performance
const parser = peg.generate(pegContent, {
cache: true,
allowedStartRules,
});
// Parse the source code into AST nodes
const parse = (src, options) => {
const result = pegUtil.parse(parser, src, options);
if(result.error) {
throw new Error("ERROR: Parsing Failure:\n" +
pegUtil.errorMessage(result.error, true).replace(/^/mg, "ERROR: "));
}
else {
return result.ast;
}
};
import fs from 'fs';
import path from 'path';
let grammar;
try {
grammar = require('./link'); // eslint-disable-line
} catch (ex) {
// Permits using compiling grammar when using ES2015 source
const peg = require('pegjs'); // eslint-disable-line
grammar = peg.generate(
fs.readFileSync(path.join(__dirname, 'link.pegjs'), 'utf8')
);
}
module.exports = {
grammar,
};
it( "consumes the matched character", function () {
const parser = peg.generate( "start = . .", options );
expect( parser ).to.parse( "ab" );
} );
it( "removes duplicates from expectations", function () {
const parser = peg.generate( "start = 'a' / 'a'", options );
expect( parser ).to.failToParse( "b", {
message: "Expected \"a\" but \"b\" found.",
} );
} );
it( "discards any expectations recorded when matching the expression", function () {
const parser = peg.generate( "start 'start' = 'a'" );
expect( parser ).to.failToParse( "b", {
expected: [ { type: "other", description: "start" } ],
} );
} );
beforeEach(function () {
Parser = PEG.generate(grammar);
});
"use strict";
const fs = require("fs"),
pegjs = require("pegjs"),
tspegjs = require("ts-pegjs");
const thisFolder = "./src/Grammar",
inputFile = thisFolder + "/src/Grammar.pegjs",
outputFolder = thisFolder + "/dist",
outputFile = outputFolder + "/Grammar.ts";
const grammarContents = fs.readFileSync(inputFile, "utf8");
const parser = pegjs.generate(grammarContents, {
allowedStartRules: [
"Contact",
"Name_Addr_Header",
"Record_Route",
"Request_Response",
"SIP_URI",
"Subscription_State",
"Supported",
"Require",
"Via",
"absoluteURI",
"Call_ID",
"Content_Disposition",
"Content_Length",
"Content_Type",
"CSeq",
const fs = require('fs');
const Path = require('path');
const PEG = require('pegjs');
const parser = PEG.generate(
fs.readFileSync(Path.resolve(__dirname, 'cldrPluralRule.pegjs'), 'utf-8')
);
function rangeListToJavaScriptAst(
rangeListNode,
lhsJavaScriptAst,
withinSemantics
) {
let javaScriptAst;
let seenRange = false;
for (let i = rangeListNode.ranges.length - 1; i >= 0; i -= 1) {
const range = rangeListNode.ranges[i];
let itemJavaScriptAst;
if (range.type === 'number') {
const options = {
dependencies: {
BoolQuery: 'elastic-builder/lib/queries/compound-queries/bool-query'
},
cache: true,
optimize: 'speed',
format: 'commonjs',
output: 'source',
trace: false,
plugins: []
};
fs.writeFileSync(
pegjsOutputPath,
PEG.generate(fs.readFileSync(pegjsDefPath, 'utf8'), options)
);