How to use the pegjs.generate function in pegjs

To help you get started, we’ve selected a few pegjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github implydata / plywood / extra / pegjs.js View on Github external
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');
}
github Click-to-Cloud / Sweet.apex / src / parser / index.js View on Github external
// 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;
    }
};
github jamesramsay / hercule / src / grammar / index.js View on Github external
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,
};
github pegjs / pegjs / test / behavior / generated-parser-behavior.spec.js View on Github external
it( "consumes the matched character", function () {

                    const parser = peg.generate( "start = . .", options );

                    expect( parser ).to.parse( "ab" );

                } );
github pegjs / pegjs / test / behavior / generated-parser-behavior.spec.js View on Github external
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.",
                    } );

                } );
github pegjs / pegjs / test / behavior / generated-parser-behavior.spec.js View on Github external
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" } ],
                        } );

                    } );
github onsip / SIP.js / build / grammarGenerator.js View on Github external
"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",
github papandreou / node-cldr / lib / CldrPluralRule.js View on Github external
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') {
github booleanapp / elastic-muto / build / build-parser.js View on Github external
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)
);