Skip to content

Commit

Permalink
Predef
Browse files Browse the repository at this point in the history
  • Loading branch information
soney committed Jul 19, 2012
1 parent 90dabca commit 7aab987
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.DS_Store
node_modules
build/*
dist/*
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
all: core
all: _build

core:
_build:
mkdir -p build
./Makefile.dryice.js
./Makefile.dryice.js build

dist: _build
mkdir -p dist
./Makefile.dryice.js dist

clean:
rm -f build/*.js
Expand Down
29 changes: 26 additions & 3 deletions Makefile.dryice.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#!/usr/bin/env node
var args = process.argv.slice(2)
var command = args[0];

if(!command) {
command = "build";
}

var copy = require('dryice').copy;

var do_build_main = function() {
Expand All @@ -19,9 +26,25 @@ var do_build = function() {


if(require.main === module) { // Called directly
console.log("Building...");
do_build();
console.log("Done!");
if(command === "build") {
console.log("Building...");
do_build();
console.log("Done!");
} else if(command === "dist") {
//http://stackoverflow.com/questions/5754153/zip-archives-in-node-js
var spawn = require('child_process').spawn
, fs = require('fs');

var package_info = JSON.parse(fs.readFileSync("package.json"));
var version = package_info.version;
var filename = "dist/jsep_"+version+".zip";
var zip = spawn('zip', ['-rj', filename, "build/jsep.js", "build/jsep.min.js"], {cwd: __dirname});
zip.on('exit', function (code) {
if(code !== 0) {
console.log('zip process exited with code ' + code);
}
});
}
} else { // Included via require
exports.build = function(callback) {
do_build();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ "name": "jsep"
, "description": "JavaScript Expression Parser"
, "version": "0.0.4"
, "version": "0.1.0"
, "author": "Stephen Oney (Carnegie Mellon)"
, "maintainers": [
{
Expand Down
44 changes: 36 additions & 8 deletions src/jsep.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
(function (root) {
"use strict";

var unary_ops = ["-", "!"], // Permissible unary operations
binary_ops = ["+", "-", "*", "/", "&&", "||", "&", "|", "<<", ">>", "===", "==", ">=", "<=", "<", ">"]; //Permissible binary operations
var default_unary_ops = ["-", "!"], // Permissible unary operations
default_binary_ops = ["+", "-", "*", "/", "&&", "||", "&", "|", "<<", ">>", "===", "==", ">=", "<=", "<", ">"], //Permissible binary operations
default_constants = ["true", "false"];


//Trim the left hand side of a string
var ltrim_regex = /^\s+/,
Expand All @@ -12,7 +14,11 @@

var Parser = function (expr, options) {
this.expr = expr;
this.options = options;
this.options = _.extend({
unary_ops: default_unary_ops,
binary_ops: default_binary_ops,
constrants: default_constants
}, options);
this.buffer = this.expr;
this.curr_node = null;
};
Expand Down Expand Up @@ -96,6 +102,9 @@
if (node === false) {
node = this.parse_square_brackets_property();
}
if (node === false) {
node = this.parse_predef();
}
if (node === false) {
node = this.parse_constant();
}
Expand Down Expand Up @@ -177,6 +186,25 @@
}
return false;
};

proto.parse_predef = function () {
var match;
var i, len;
for (i = 0, len = this.options.constants.length; i<len; i++) {
var constant = this.options.constants[i];
var regex = new RegExp("^("+constant+")[^a-zA-Z0-9_\\$]");
match = this.buffer.match(regex);
if(match) {
this.buffer = this.buffer.substr(match[0].length);
return {
type: "predef",
value: match[0]
};
}
}
return false;
};

var start_str_regex = new RegExp("^['\"]");
var number_regex = new RegExp("^(\\d+(\\.\\d+)?)");
proto.parse_constant = function () {
Expand Down Expand Up @@ -278,8 +306,8 @@
};
proto.parse_unary_op = function () {
var i, leni;
for (i = 0, leni = unary_ops.length; i < leni; i += 1) {
var unary_op = unary_ops[i];
for (i = 0, leni = this.options.unary_ops.length; i < leni; i += 1) {
var unary_op = this.options.unary_ops[i];
if (starts_with(this.buffer, unary_op)) {
this.buffer = this.buffer.substr(unary_op.length);
var operand = this.gobble_expression();
Expand All @@ -298,8 +326,8 @@
proto.parse_binary_op = function () {
if (this.curr_node !== null) {
var i, len;
for (i = 0, len = binary_ops.length; i < len; i += 1) {
var binary_op = binary_ops[i];
for (i = 0, len = this.options.binary_ops.length; i < len; i += 1) {
var binary_op = this.options.binary_ops[i];
if (starts_with(this.buffer, binary_op)) {
this.buffer = this.buffer.substr(binary_op.length);
var operand_1 = this.curr_node;
Expand Down Expand Up @@ -337,7 +365,7 @@
var parser = new Parser(expr, options);
return parser.tokenize();
};
do_parse.version = "0.0.4";
do_parse.version = "0.1.0";

if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
Expand Down

0 comments on commit 7aab987

Please sign in to comment.