forked from keichi/binary-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Context class to a separate file
- Loading branch information
Showing
2 changed files
with
84 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |