$ npm install --save rmutt
var rmutt = require('rmutt');
Since internally some of these methods reuse others, options of the reused methods also apply to those using them:
- All
rmutt.transpile
options apply tormutt.compile
andrmutt.expand
. - All
rmutt.compile
options apply tormutt.expand
. - All
expander
options apply tormutt.expand
.
For instance, take the following grammar:
top: expr " = " (expr > xcalc);
expr: "1 + 2";
The following example will define the transformation xcalc
:
rmutt.expand(grammar, {
externals: {
xcalc: function (input) {
return eval(input).toString(); // sorry, the Evil Eval got a part in this story
}
}
}, function (err, result) {
// result.expanded = "1 + 2 = 3"
});
Another example grammar:
top: expr " = " calc[expr, "USD"];
expr: "1 + 2";
The following will define the rule with arguments calc
:
rmutt.expand(grammar, {
externals: {
calc: function (input, unit) {
return unit + ' ' + eval(input).toString();
}
}
}, function (err, result) {
// result.expanded = "1 + 2 = USD 3"
});
-
iteration (number): A given rmutt grammar can generate N possible strings, where N is finite or infnite depending on whether or not the grammar is recursive. Specifying an iteration will deterministically generate the N-th possible string. If the iteration specified (call it i) is greater than N, rmutt will generate the
i mod N
th iteration. Enumerating all possible strings of a grammar is usually only useful for very simple grammars; most grammars can produce more strings than can be enumerated with a JavaScript number. -
maxStackDepth (number): Specifies the maximum depth to which rmutt will expand the grammar. This is usually used to prevent recursive grammars from crashing rmutt with stack overflows. Beyond the maximum stack depth, a rule will expand to an empty, zero-length string.
-
randomSeed (number|Array): Specifies a seed for the random number expander. Two runs against the same grammar with the same seed will generate identical output. The seed can be a single 32-bit integer or an array of 16 32-bit integers. If no seed is specified, a seed is generated according to randomSeedType option and returned in the callback result options. Also, it can be expanded in the grammar using the
$options.randomSeed
rule.
This option overrides the randomSeedType
option passed to
rmutt.transpile
or rmutt.compile
.
rmutt.compile(grammar, function (err, result) {
var expander = result.compiled;
});
Or...
var expander = require('path/to/transpiled/file');
Then
expander(function (err, result) {
console.log(result.expanded);
});
A function with the following arguments:
- error (Error)
- result (object): An object with the following properties:
- options (object): The options used (some may have changed during execution).
- compiled (Function): The
expander
function.
-
cache (boolean): Loads or creates a cached transpiled code file.
-
cacheFile (string): Absolute path and file name where the transpiled code will be cached.
-
cacheRegenerate (boolean): Write the transpiled file even if it already exists.
rmutt.compile(grammar, {entry: 'entry-rule'}, function (err, result) {
result.compiled(function (err, result1) {
});
result.compiled(function (err, result2) {
// depending on the chances, result2.expanded is different than result1.expanded
});
});
A function with the following arguments:
- error (Error)
- result (object): An object with the following properties:
- options (object): The options used (some may have changed during execution).
- transpiled (string): The generated JavaScript code.
This option can be overriden by the entry
option passed to
the expander
function.
- header (string): Adds a comment line under the "Generated by rmutt" line in the transpiled code.
This option can be overriden by the randomSeedType
option passed to
the expander
function.
rmutt.expand("top: a,b,c,d;", function (err, result) {
console.log(result.expanded);
});