How to use the wikipeg/lib/compiler/visitor.build function in wikipeg

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 / 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');
}

wikipeg

Parser generator for JavaScript and PHP

MIT
Latest version published 8 months ago

Package Health Score

59 / 100
Full package analysis

Similar packages