Skip to content

Commit

Permalink
skeleton of actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthropohedron committed Apr 9, 2015
1 parent 77a2ea3 commit 7e41a8c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*jslint vars: true, white: true, plusplus: true, todo: true */
"use strict";

var actionRE = /(?:^|\s)([\-+])([\-a-z]+)(?:{([^}]+)})?(?=\s|$)/g;
var emptyActionsRE = /^\s*\{\s*\}\s*$/;

function parseActions(actionsClause) {
var actions = [];
var remainder = actionsClause.replace(actionRE, function() {
actions.push(Array.prototype.slice.call(arguments, 1));
return "";
});
if (!emptyActionsRE.test(remainder)) {
throw new Error("Malformed actions clause:\n\n" + actionsClause);
}
return actions;
}

exports.parseActions = parseActions;

30 changes: 30 additions & 0 deletions test/test-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var aParser = require("./actions");

exports["test parse valid"] = function(assert) {
var actions = aParser.parseActions([
"{",
"+block{nothing}",
"-handle-as-image",
"}"
].join("\n"));
assert.ok(actions, "Actions were parsed");
assert.strictEqual(actions.length, 2, "Correct number of actions");
};

exports["test parse invalid"] = function(assert) {
assert.throws(function() {
aParser.parseActions("{ foo +block{nothing} -handle-as-image }");
}, "Extra garbage inside braces");
assert.throws(function() {
aParser.parseActions("foo{ +block{nothing} -handle-as-image }");
}, "Extra garbage outside braces");
assert.throws(function() {
aParser.parseActions("{ +block{nothing}} -handle-as-image }");
}, "Extra braces");
assert.throws(function() {
aParser.parseActions("{} +block{nothing} -handle-as-image {}");
}, "More extra braces");
};

require("sdk/test").run(exports);

0 comments on commit 7e41a8c

Please sign in to comment.