How to use the pegjs.compiler 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 wikimedia / parsoid / lib / wt2html / tokenizer.js View on Github external
PegTokenizer.prototype.initTokenizer = function() {
	// Construct a singleton static tokenizer.
	var pegSrcPath = path.join(__dirname, 'pegTokenizer.pegjs');
	this.src = fs.readFileSync(pegSrcPath, 'utf8');

	// FIXME: Don't report infinite loops, i.e. repeated subexpressions which
	// can match the empty string, since our grammar gives several false
	// positives (or perhaps true positives).
	delete PEG.compiler.passes.check.reportInfiniteLoops;

	function cacheRuleHook(opts) {
		var maxVisitCount = 20;
		return {
			start: [
				[
					'var checkCache = visitCounts[', opts.startPos,
					'] > ', maxVisitCount, ';',
				].join(''),
				'var cached, bucket, key;',
				'if (checkCache) {',
				[
					'  key = (', opts.variantIndex, '+',
					opts.variantCount, '*', opts.ruleIndex,
					').toString() + stops.key;',
				].join(''),
github pegjs / pegjs / test / unit / compiler / passes / remove-proxy-rules.spec.js View on Github external
"use strict";

const chai = require( "chai" );
const helpers = require( "./helpers" );
const pass = require( "pegjs" ).compiler.passes.transform.removeProxyRules;

chai.use( helpers );

const expect = chai.expect;

describe( "compiler pass |removeProxyRules|", function () {

    describe( "when a proxy rule isn't listed in |allowedStartRules|", function () {

        it( "updates references and removes it", function () {

            expect( pass ).to.changeAST(
                [
                    "start = proxy",
                    "proxy = proxied",
                    "proxied = 'a'",
github pegjs / pegjs / test / unit / compiler / passes / report-incorrect-plucking.spec.js View on Github external
"use strict";

const { expect, use } = require( "chai" );
const helpers = require( "./helpers" );
const pass = require( "pegjs" ).compiler.passes.check.reportIncorrectPlucking;

use( helpers );

describe( "compiler pass |reportIncorrectPlucking|", function () {

    function reports( error, edgecases ) {

        it( error.slice( 0, -1 ), () => {

            edgecases.forEach( grammar => expect( pass ).to.reportError( grammar, error ) );

        } );

    }

    reports( `"@" cannot be used with an action block.`, [
github pegjs / pegjs / test / unit / compiler / passes / report-undefined-rules.spec.js View on Github external
"use strict";

const chai = require( "chai" );
const helpers = require( "./helpers" );
const pass = require( "pegjs" ).compiler.passes.check.reportUndefinedRules;

chai.use( helpers );

const expect = chai.expect;

describe( "compiler pass |reportUndefinedRules|", function () {

    it( "reports undefined rules", function () {

        expect( pass ).to.reportError( "start = undefined", {
            message: "Rule \"undefined\" is not defined.",
            location: {
                start: { offset: 8, line: 1, column: 9 },
                end: { offset: 17, line: 1, column: 18 },
            },
        } );
github pegjs / pegjs / test / unit / compiler / passes / report-duplicate-rules.spec.js View on Github external
"use strict";

const chai = require( "chai" );
const helpers = require( "./helpers" );
const pass = require( "pegjs" ).compiler.passes.check.reportDuplicateRules;

chai.use( helpers );

const expect = chai.expect;

describe( "compiler pass |reportDuplicateRules|", function () {

    it( "reports duplicate rules", function () {

        expect( pass ).to.reportError( [
            "start = 'a'",
            "start = 'b'",
        ].join( "\n" ), {
            message: "Rule \"start\" is already defined at line 1, column 1.",
            location: {
                start: { offset: 12, line: 2, column: 1 },
github pegjs / pegjs / test / unit / compiler / passes / inference-match-result.spec.js View on Github external
"use strict";

const chai = require( "chai" );
const helpers = require( "./helpers" );
const pass = require( "pegjs" ).compiler.passes.generate.inferenceMatchResult;

chai.use( helpers );

const expect = chai.expect;

describe( "compiler pass |inferenceMatchResult|", function () {

    it( "calculate |match| property for |any| correctly", function () {

        expect( pass ).to.changeAST( "start = .          ", { rules: [ { match:  0 } ] }, {}, {} );

    } );

    it( "calculate |match| property for |literal| correctly", function () {

        expect( pass ).to.changeAST( "start = ''         ", { rules: [ { match:  1 } ] }, {}, {} );
github pegjs / pegjs / test / unit / compiler / passes / report-unused-rules.spec.js View on Github external
"use strict";

const chai = require( "chai" );
const helpers = require( "./helpers" );
const pass = require( "pegjs" ).compiler.passes.check.reportUnusedRules;

chai.use( helpers );

const expect = chai.expect;

describe( "compiler pass |reportUnusedRules|", function () {

    it( "should report rules that are not referenced", function () {

        expect( pass ).to.reportWarning(
            `
                start = .
                unused = .
            `,
            `Rule "unused" is not referenced.`,
        );
github pegjs / pegjs / test / unit / compiler / passes / report-infinite-repetition.spec.js View on Github external
"use strict";

const chai = require( "chai" );
const helpers = require( "./helpers" );
const pass = require( "pegjs" ).compiler.passes.check.reportInfiniteRepetition;

chai.use( helpers );

const expect = chai.expect;

describe( "compiler pass |reportInfiniteRepetition|", function () {

    it( "reports infinite loops for zero_or_more", function () {

        expect( pass ).to.reportError( "start = ('')*", {
            message: "Possible infinite loop when parsing (repetition used with an expression that may not consume any input).",
            location: {
                start: { offset: 8, line: 1, column: 9 },
                end: { offset: 13, line: 1, column: 14 },
            },
        } );
github pegjs / pegjs / test / unit / compiler / passes / report-duplicate-labels.spec.js View on Github external
"use strict";

const chai = require( "chai" );
const helpers = require( "./helpers" );
const pass = require( "pegjs" ).compiler.passes.check.reportDuplicateLabels;

chai.use( helpers );

const expect = chai.expect;

describe( "compiler pass |reportDuplicateLabels|", function () {

    describe( "in a sequence", function () {

        it( "reports labels duplicate with labels of preceding elements", function () {

            expect( pass ).to.reportError( "start = a:'a' a:'a'", {
                message: "Label \"a\" is already defined at line 1, column 9.",
                location: {
                    start: { offset: 14, line: 1, column: 15 },
                    end: { offset: 19, line: 1, column: 20 },
github pegjs / pegjs / test / unit / compiler / passes / report-infinite-recursion.spec.js View on Github external
"use strict";

const chai = require( "chai" );
const helpers = require( "./helpers" );
const pass = require( "pegjs" ).compiler.passes.check.reportInfiniteRecursion;

chai.use( helpers );

const expect = chai.expect;

describe( "compiler pass |reportInfiniteRecursion|", function () {

    it( "reports direct left recursion", function () {

        expect( pass ).to.reportError( "start = start", {
            message: "Possible infinite loop when parsing (left recursion: start -> start).",
            location: {
                start: { offset: 8, line: 1, column: 9 },
                end: { offset: 13, line: 1, column: 14 },
            },
        } );