🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

agfl-generate

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

agfl-generate - npm Package Compare versions

Comparing version
0.1.0
to
0.2.0
+35
-22
index.js

@@ -6,2 +6,10 @@ #!/usr/bin/env node

/**
* Generates a sample for the given rule in the given grammar
* @param {Object} grammar The grammar object
* @param {Function} joiner A joiner function, may also be a string
* @param {String} rule The rule for which a sample should be generated
* @param {Array} args List of arguments for the rule
* @return {String} A random sample
*/
var generateExample = function (grammar, joiner, rule, args) {

@@ -28,3 +36,3 @@ if (typeof args === 'undefined' || args === null) {

var choice = makeChoice(selected.choices, types, args.length, rule);
return applyTokens(grammar, joiner, choice.tokens, args);
return applyTokens(grammar, joiner, choice.tokens, types);
}

@@ -36,10 +44,22 @@ } else {

/**
* When an alternative is chosen, work out all the tokens.
* @param {Object} grammar The grammar on which we're working
* @param {Function} joiner Joiner function, can also be a string
* @param {Array} tokens List of tokens from the selected alternative.
* @param {Object} args List of argument names and their values.
* @return {String}
*/
var applyTokens = function (grammar, joiner, tokens, args) {
return _(tokens).map(function (token) {
var returnValue = "";
var isTerminal = false;
if (token.what === 'string') {
// basic terminal string
returnValue = token.value;
isTerminal = true;
} else if (token.what === 'call') {
// retrieve the result from another rule
var arglist = _(token.args).map(function (arg) {

@@ -58,9 +78,5 @@ if (typeof args[arg] !== 'undefined' && args[arg] !== null) {

returnValue = generateExample(grammar, joiner, token.name, arglist);
} else {
throw new Error(util.format(
"Invalid token type %s in expression %s",
token.what,
_(tokens).map(displayToken).join(', ')
));
}
// either use the joiner function or append the joiner string
if (typeof joiner === 'function') {

@@ -75,19 +91,17 @@ return joiner(returnValue, isTerminal);

}
// finally merge all tokens together to form the string
}).join('');
};
var displayToken = function (token) {
if (token.what === 'string') {
return util.format('"%s"', token.value);
} else if (token.what === 'call') {
if (token.args.length > 0) {
return util.format('%s(%s)', token.name, token.args.join(', '));
} else {
return token.name;
}
} else {
return JSON.stringify(token);
}
};
/**
* Pick a choice from a list of alternatives.
* If arguments are given, only those alternatives that match the choice will
* be selected.
* @param {Array} choices List of alternatives for the rule
* @param {Object} args List of argument names and their values
* @param {Number} argsize Number of arguments
* @param {String} rule Name of the rule, for error reporting purposes
* @return {Array} The alternative chosen from the list of possibilities
*/
var makeChoice = function (choices, args, argsize, rule) {

@@ -118,2 +132,1 @@ if (argsize > 0) {

module.exports.generateExample = generateExample;
module.exports.displayToken = displayToken;
{
"name": "agfl-generate",
"version": "0.1.0",
"version": "0.2.0",
"description": "Generator using agfl grammar",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -1,3 +0,3 @@

# AGFL generator
Based on http://www.agfl.cs.ru.nl/cgi-bin/boeket/boeket.cgi
# Sample string generator for AGFL grammar
Generates a string from an AGFL grammar, based on http://www.agfl.cs.ru.nl/cgi-bin/boeket/boeket.cgi