Skip to content

Commit

Permalink
Merge pull request #26 from xmltechgeek/add-globals
Browse files Browse the repository at this point in the history
Is there a way to define my own helper functions/external globals for nools files?
  • Loading branch information
doug-martin committed May 23, 2013
2 parents 1395a89 + 372c176 commit 46c01d6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/parser/nools/tokens.js
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
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
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
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

0 comments on commit 46c01d6

Please sign in to comment.