How to use wikipeg - 6 common examples

To help you get started, we’ve selected a few wikipeg 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 / bin / inspectTokenizer.js View on Github external
rulesSource += tab + 'var ' + name + ' = null;\n';
			seen.add(name);
		}
	};
	// Collect all the code blocks in the AST.
	var dumpCode = function(node) {
		if (node.code) {
			// remove trailing whitespace for single-line predicates
			var code = node.code.replace(/[ \t]+$/, '');
			// wrap with a function, to prevent spurious errors caused
			// by redeclarations or multiple returns in a block.
			rulesSource += tab + '(function() {\n' + code + '\n' +
				tab + '})();\n';
		}
	};
	var visit = visitor.build({
		initializer: function(node) {
			if (node.code) {
				rulesSource += node.code + '\n';
			}
		},
		semantic_and: dumpCode,
		semantic_node: dumpCode,
		rule: function(node) {
			rulesSource += 'function rule_' + node.name + '() {\n';
			seen.clear();
			visit(node.expression);
			rulesSource += '}\n';
		},
		labeled: function(node) {
			addVar(node.label);
			visit(node.expression);
github wikimedia / parsoid / bin / parserTests.js View on Github external
ParserTests.prototype.main = Promise.async(function *(options, mockAPIServerURL) {
	this.runDisabled = ScriptUtils.booleanOption(options['run-disabled']);
	this.runPHP = ScriptUtils.booleanOption(options['run-php']);

	// test case filtering
	this.testFilter = null; // null is the 'default' by definition
	if (options.filter || options.regex) {
		// NOTE: filter.toString() is required because a number-only arg
		// shows up as a numeric type rather than a string.
		// Ex: parserTests.js --filter 53221
		var pattern = options.regex || JSUtils.escapeRegExp(options.filter.toString());
		this.testFilter = new RegExp(pattern);
	}

	this.testParserFilePath = path.join(__dirname, '../lib/parserTests/parserTests.pegjs');
	this.testParser = PEG.buildParser(yield fs.readFile(this.testParserFilePath, 'utf8'));

	const parsedTests = yield this.getTests(options);
	this.testFormat = parsedTests[0];
	this.cases = parsedTests[1];
	if (this.testFormat && this.testFormat.text) {
		this.testFormat = +(this.testFormat.text);
	} else {
		this.testFormat = 1;
	}

	if (options.maxtests) {
		var n = Number(options.maxtests);
		console.warn('maxtests:' + n);
		if (n > 0) {
			this.cases.length = n;
		}
github wikimedia / parsoid / lib / wt2html / tokenizer.js View on Github external
PegTokenizer.prototype.compileTokenizer = function(ast, compileOpts = {}) {
	var compiler = PEG.compiler;
	var env = this.env;

	// 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).
	var passes = {
		check: [
			compiler.passes.check.reportMissingRules,
			compiler.passes.check.reportLeftRecursion,
		],
		transform: [
			compiler.passes.transform.analyzeParams,
		],
		generate: [
			compiler.passes.generate.astToCode
		],
github wikimedia / parsoid / bin / inspectTokenizer.js View on Github external
function generateCallgraph(opts) {
	var file = getOutputStream(opts);
	var tokenizer = new PegTokenizer();
	var pegOpts = { php: opts.php };
	var ast = tokenizer.parseTokenizer(pegOpts);
	var visitor = require('wikipeg/lib/compiler/visitor');
	var edges = [];
	var currentRuleName;

	var visit = visitor.build({
		rule: function(node) {
			currentRuleName = node.name;
			visit(node.expression);
		},

		rule_ref: function(node) {
			var edge = "\t" + currentRuleName + " -> " + node.name + ";";
			if (edges.indexOf(edge) === -1) {
				edges.push(edge);
			}
		}
	});

	visit(ast);

	var dot = "digraph {\n" +
github wikimedia / parsoid / bin / inspectTokenizer.js View on Github external
function listOrphans(opts) {
	var file = getOutputStream(opts);
	var tokenizer = new PegTokenizer();
	var pegOpts = { php: opts.php };
	var ast = tokenizer.parseTokenizer(pegOpts);
	var visitor = require('wikipeg/lib/compiler/visitor');

	var rules = {};

	visitor.build({
		rule: function(node) {
			rules[node.name] = true;
		},
	})(ast);

	visitor.build({
		rule_ref: function(node) {
			delete rules[node.name];
		},
	})(ast);

	file.write(Object.getOwnPropertyNames(rules).join('\n') + '\n');
}
github wikimedia / parsoid / lib / wt2html / tokenizer.js View on Github external
PegTokenizer.prototype.parseTokenizer = function(compileOpts = {}) {
	var src = this.readSource(compileOpts);
	return PEG.parser.parse(src);
};

wikipeg

Parser generator for JavaScript and PHP

MIT
Latest version published 8 months ago

Package Health Score

59 / 100
Full package analysis

Similar packages