Skip to content

Commit

Permalink
Move Context class to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
keichi committed Nov 11, 2014
1 parent ad9849a commit 81db0f8
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 81 deletions.
83 changes: 2 additions & 81 deletions lib/binary_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Globals
//========================================================================================

var Context = require("./context").Context;

var PRIMITIVE_TYPES = {
'UInt8' : 1,
'UInt16LE' : 2,
Expand Down Expand Up @@ -502,87 +504,6 @@ Parser.prototype.isInteger = function() {
return !!this.type.match(/U?Int[8|16|32][BE|LE]?|Bit\d+/);
};

//========================================================================================
// class Context
//========================================================================================

//----------------------------------------------------------------------------------------
// constructor
//----------------------------------------------------------------------------------------

var Context = function() {
this.code = '';
this.scopes = [];
this.isAsync = false;
this.bitFields = [];
this.tmpVariableCount = 0;
};

//----------------------------------------------------------------------------------------
// public methods
//----------------------------------------------------------------------------------------

Context.prototype.generateVariable = function(name) {
var arr = ['vars'];
Array.prototype.push.apply(arr, this.scopes);
if (name) {
arr.push(name);
}

return arr.join('.');
};

Context.prototype.generateOption = function(val) {
switch(typeof val) {
case 'number':
return val.toString();
case 'string':
return this.generateVariable(val);
case 'function':
return '(' + val + ').call(' + this.generateVariable() + ')';
}
};

Context.prototype.generateError = function() {
var args = Array.prototype.slice.call(arguments);
var err = Context.interpolate.apply(this, args);

if (this.isAsync) {
this.pushCode('return process.nextTick(function() { callback(new Error(' + err + '), vars); });');
} else {
this.pushCode('throw new Error(' + err + ');');
}
};

Context.prototype.generateTmpVariable = function() {
return 'tmp' + (this.tmpVariableCount++);
};

Context.prototype.pushCode = function() {
var args = Array.prototype.slice.call(arguments);

this.code += Context.interpolate.apply(this, args) + '\n';
};

//----------------------------------------------------------------------------------------
// private methods
//----------------------------------------------------------------------------------------

Context.interpolate = function(s) {
var re = /{\d+}/g;
var matches = s.match(re);
var params = Array.prototype.slice.call(arguments, 1);

if (matches) {
matches.forEach(function(match) {
var index = parseInt(match.substr(1, match.length - 2), 10);
s = s.replace(match, params[index].toString());
});
}

return s;
};

//========================================================================================
// Exports
//========================================================================================
Expand Down
82 changes: 82 additions & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//========================================================================================
// class Context
//========================================================================================

//----------------------------------------------------------------------------------------
// constructor
//----------------------------------------------------------------------------------------

var Context = function() {
this.code = '';
this.scopes = [];
this.isAsync = false;
this.bitFields = [];
this.tmpVariableCount = 0;
};

//----------------------------------------------------------------------------------------
// public methods
//----------------------------------------------------------------------------------------

Context.prototype.generateVariable = function(name) {
var arr = ['vars'];
Array.prototype.push.apply(arr, this.scopes);
if (name) {
arr.push(name);
}

return arr.join('.');
};

Context.prototype.generateOption = function(val) {
switch(typeof val) {
case 'number':
return val.toString();
case 'string':
return this.generateVariable(val);
case 'function':
return '(' + val + ').call(' + this.generateVariable() + ')';
}
};

Context.prototype.generateError = function() {
var args = Array.prototype.slice.call(arguments);
var err = Context.interpolate.apply(this, args);

if (this.isAsync) {
this.pushCode('return process.nextTick(function() { callback(new Error(' + err + '), vars); });');
} else {
this.pushCode('throw new Error(' + err + ');');
}
};

Context.prototype.generateTmpVariable = function() {
return 'tmp' + (this.tmpVariableCount++);
};

Context.prototype.pushCode = function() {
var args = Array.prototype.slice.call(arguments);

this.code += Context.interpolate.apply(this, args) + '\n';
};

//----------------------------------------------------------------------------------------
// private methods
//----------------------------------------------------------------------------------------

Context.interpolate = function(s) {
var re = /{\d+}/g;
var matches = s.match(re);
var params = Array.prototype.slice.call(arguments, 1);

if (matches) {
matches.forEach(function(match) {
var index = parseInt(match.substr(1, match.length - 2), 10);
s = s.replace(match, params[index].toString());
});
}

return s;
};

exports.Context = Context;

0 comments on commit 81db0f8

Please sign in to comment.