Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to define my own helper functions/external globals for nools files? #26

Merged
merged 9 commits into from
May 23, 2013
23 changes: 23 additions & 0 deletions lib/parser/nools/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,29 @@ module.exports = {
}
},

"global": function (orig, context) {
var src = orig.replace(/^global\s*/, "");
var name = src.match(/^([a-zA-Z_$][0-9a-zA-Z_$]*\s*)/);
if (name) {
src = src.replace(name[0], "").replace(/^\s*|\s*$/g, "");
if (utils.findNextToken(src) === "=") {
name = name[1].replace(/^\s+|\s+$/g, '');
var fullbody = utils.getTokensBetween(src, "=", ";", true).join("");
var body = fullbody.substring(1, fullbody.length - 1);
body = body.replace(/^\s+|\s+$/g, '');
context.scope.push({name: name, body: body});
src = src.replace(fullbody, "");
return src;
}
else {
throw new Error("unexpected token : expected : '=' found : '" + utils.findNextToken(src) + "'");
}
}
else {
throw new Error("missing name");
}
},

"function": function (orig, context) {
var src = orig.replace(/^function\s*/, "");
var name = src.match(/^([a-zA-Z_$][0-9a-zA-Z_$]*)\s*/);
Expand Down
21 changes: 21 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Or [download the source](https://raw.github.com/doug-martin/C2FO/master/nools.js
* [Structure](#rule-structure)
* [Constraints](#constraints)
* [Actions](#action)
* [Globals](#globals)
* [Browser Support](#browser-support)
* [Fibonacci](#fib)

Expand Down Expand Up @@ -586,6 +587,26 @@ then {

For rules defined using the rules language nools will automatically determine what parameters need to be passed in based on what is referenced in the action.

<a name="action"></a>

### Globals

Globals are accessible through the current working scope in your rules runs from the nools rules format. Note that globals are not part of the working
memory so are not accessible during the when portion of your rules run. Globals are used like the following:

```
global util = require('util');

rule "A Rule" {
when {
$obj: Object;
}
then{
util.log("Hello Globals :)");
}
}
```

## Emitting custom events.

You may also emit events from your rule actions using the sessions emit function.
Expand Down
18 changes: 18 additions & 0 deletions test/noolsParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ it.describe("nools dsl parser",function (it) {
});
});

it.should("parse a global statement", function () {
var parsed = noolsParser.parse("global util = require('util');");
assert.equal(parsed.scope[0].name, 'util');
assert.equal(parsed.scope[0].body, "require('util')");
});

it.should("throw an error when the define block is missing a name", function () {
assert.throws(function () {
noolsParser.parse("define {myProp : 'value'");
Expand All @@ -46,6 +52,18 @@ it.describe("nools dsl parser",function (it) {
noolsParser.parse("define Test myProp : 'value'}");
});
});

it.should("throw an error when the global statement is missing a name", function () {
assert.throws(function () {
noolsParser.parse("global = require('util');");
});
});

it.should("throw an error when the global statement is missing a ;", function () {
assert.throws(function () {
noolsParser.parse("global util = require('util')");
});
});
});

it.describe("parsing function", function (it) {
Expand Down
3 changes: 3 additions & 0 deletions test/rules/scope.nools
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ function matches(str, regex){
return regex.test(str);
}

global util = require('util');

rule Hello {
when {
m : Message matches(m.message, /^hello(\\s*world)?$/);
}
then {
util.log("Hello Globals :)");
modify(m, function(){
this.message += " goodbye";
})
Expand Down