-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
90 changed files
with
1,542 additions
and
270 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
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
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,97 @@ | ||
function forgivingPropertyAccessor(left, right) { | ||
return typeof left === 'undefined' || left === null ? undefined : left[right]; | ||
} | ||
|
||
var UNARY_OPERATORS = { | ||
'!': function (right) { return !right; }, | ||
'-': function (right) { return -right; }, | ||
'[': function (right) { return right; }, //array literal | ||
'{': function (right) { //object literal | ||
|
||
var result = {}, keyVal; | ||
for (var i = 0; i < right.length; i++) { | ||
keyVal = right[i]; | ||
result[keyVal.k] = keyVal.v; | ||
} | ||
|
||
return result; | ||
} | ||
}; | ||
|
||
var BINARY_OPERATORS = { | ||
'+': function (left, right) { return left + right; }, | ||
'-': function (left, right) { return left - right; }, | ||
'*': function (left, right) { return left * right; }, | ||
'/': function (left, right) { return left / right; }, | ||
'%': function (left, right) { return left % right; }, | ||
'<': function (left, right) { return left < right; }, | ||
'>': function (left, right) { return left > right; }, | ||
'>=': function (left, right) { return left >= right; }, | ||
'<=': function (left, right) { return left <= right; }, | ||
'==': function (left, right) { return left == right; }, | ||
'!=': function (left, right) { return left != right; }, | ||
'===': function (left, right) { return left === right; }, | ||
'!==': function (left, right) { return left !== right; }, | ||
'||': function (left, right) { return left || right; }, | ||
'&&': function (left, right) { return left && right; }, | ||
'(': function (left, right) { //function call on a scope | ||
return left.apply(left, right); | ||
}, | ||
'.': forgivingPropertyAccessor, | ||
'[': forgivingPropertyAccessor | ||
}; | ||
|
||
var TERNARY_OPERATORS = { | ||
'(': function (target, name, args) { //function call on an object | ||
return typeof target === 'undefined' || target === null ? | ||
undefined : target[name].apply(target, args); | ||
}, | ||
'?': function (test, trueVal, falseVal) { return test ? trueVal : falseVal; }, | ||
'|': function (input, pipeFn, args) { return pipeFn.apply(pipeFn, [input].concat(args)); } //pipe (filter) | ||
}; | ||
|
||
module.exports = function getTreeValue(tree, scope) { | ||
|
||
var operatorFn, result; | ||
var parsedVal, argExp, arrayResult; | ||
|
||
if (tree instanceof Array) { | ||
|
||
if (tree.length > 0) { | ||
result = new Array(tree.length); | ||
for (var i = 0; i < tree.length; i++) { | ||
argExp = tree[i]; | ||
arrayResult = parsedVal = getTreeValue(argExp, scope); | ||
if (argExp.key) { | ||
arrayResult = { | ||
k: argExp.key, | ||
v: parsedVal | ||
}; | ||
} | ||
result[i] = arrayResult; | ||
} | ||
} else { | ||
result = []; | ||
} | ||
return result; | ||
} | ||
|
||
if (tree.a === 'literal') { | ||
result = tree.v; | ||
} else if (tree.a === 'idn') { | ||
result = scope[tree.v]; | ||
} else if (tree.a === 'unr' && UNARY_OPERATORS[tree.v]) { | ||
operatorFn = UNARY_OPERATORS[tree.v]; | ||
result = operatorFn(getTreeValue(tree.l, scope)); | ||
} else if (tree.a === 'bnr' && BINARY_OPERATORS[tree.v]) { | ||
operatorFn = BINARY_OPERATORS[tree.v]; | ||
result = operatorFn(getTreeValue(tree.l, scope), getTreeValue(tree.r, scope)); | ||
} else if (tree.a === 'tnr' && TERNARY_OPERATORS[tree.v]) { | ||
operatorFn = TERNARY_OPERATORS[tree.v]; | ||
result = operatorFn(getTreeValue(tree.l, scope), getTreeValue(tree.r, scope), getTreeValue(tree.othr, scope)); | ||
} else { | ||
throw new Error('Unknown tree entry of type "'+ tree.a +' and value ' + tree.v + ' in:' + JSON.stringify(tree)); | ||
} | ||
|
||
return result; | ||
}; |
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,29 @@ | ||
module.exports = function getIdentifiers(tree) { | ||
|
||
var partialResult; | ||
|
||
if (tree instanceof Array) { | ||
partialResult = []; | ||
if (tree.length > 0) { | ||
for (var i = 0; i < tree.length; i++) { | ||
partialResult = partialResult.concat(getIdentifiers(tree[i])); | ||
} | ||
} | ||
return partialResult; | ||
} | ||
|
||
if (tree.a === 'literal') { | ||
return []; | ||
} else if (tree.a === 'idn') { | ||
return [tree.v]; | ||
} else if (tree.a === 'unr') { | ||
return getIdentifiers(tree.l); | ||
} else if (tree.a === 'bnr') { | ||
return getIdentifiers(tree.l).concat(getIdentifiers(tree.r)); | ||
} else if (tree.a === 'tnr') { | ||
return getIdentifiers(tree.l).concat(getIdentifiers(tree.r)) | ||
.concat(getIdentifiers(tree.othr)); | ||
} else { | ||
throw new Error('unknown entry' + JSON.stringify(tree)); | ||
} | ||
}; |
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,124 @@ | ||
function isWhitespace(ch) { | ||
return ch === '\t' || ch === '\r' || ch === '\n' || ch === ' '; | ||
} | ||
|
||
function isQuote(ch) { | ||
return ch === '"' || ch === "'"; | ||
} | ||
|
||
function isDigit(ch) { | ||
return ch >= '0' && ch <= '9'; | ||
} | ||
|
||
function isIdentifierStart(ch) { | ||
return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch === '$' || ch === '_'; | ||
} | ||
|
||
function isIdentifierPart(ch) { | ||
return isIdentifierStart(ch) || isDigit(ch); | ||
} | ||
|
||
function isOperator(ch) { | ||
return '+-*/%!|&.,=<>()[]{}?:'.indexOf(ch) > -1; | ||
} | ||
|
||
function isSuffixOperator(ch) { | ||
return '=|&'.indexOf(ch) > -1; | ||
} | ||
|
||
/** | ||
* A lexing function | ||
* @param input - a string of characters to be tokenised | ||
* @returns {Array} - an array of token objects with the following properties: | ||
* - t: type of token, one of: num (number), idn (identifier), str (string), opr (operator) | ||
* - v: value of a token | ||
* - f: from where (index) a given token starts in the input | ||
* @throws {Error} when an unknown character is detected in the input (ex.: ^) | ||
*/ | ||
module.exports = function (initialInput) { | ||
|
||
var input, EOF = String.fromCharCode(0); | ||
var result = []; | ||
var i = 0, current, quote; //current is a character that the lexer is currently looking at | ||
var from, value; | ||
|
||
if (typeof initialInput === 'string') { | ||
|
||
//append special EOF token to avoid constant checks for the input end | ||
input = initialInput + EOF; | ||
|
||
current = input.charAt(0); | ||
while (current !== EOF) { | ||
|
||
//reset variables responsible for accumulating results | ||
from = i; | ||
value = ''; | ||
|
||
if (isWhitespace(current)) { | ||
|
||
current = input.charAt(++i); //skip | ||
|
||
} else if (isOperator(current)) { | ||
|
||
do { | ||
value += current; | ||
current = input.charAt(++i); | ||
|
||
} while (isSuffixOperator(current)); | ||
|
||
result.push({t: 'opr', v: value, f: from}); | ||
|
||
} else if (isIdentifierStart(current)) { | ||
|
||
do { | ||
value += current; | ||
current = input.charAt(++i); | ||
|
||
} while (isIdentifierPart(current)); | ||
|
||
result.push({t: 'idn', v: value, f: from}); | ||
|
||
} else if (isQuote(current)) { | ||
|
||
quote = current; | ||
current = input.charAt(++i); //skip the initial quote | ||
|
||
while (current !== quote && current !== EOF) { | ||
|
||
if (current === '\\' && input.charAt(i + 1) === quote) { | ||
value += quote; | ||
current = input.charAt(++i); | ||
} else { | ||
value += current; | ||
} | ||
current = input.charAt(++i); | ||
} | ||
|
||
if (isQuote(current)) { | ||
result.push({t: 'str', v: value, f: from}); | ||
current = input.charAt(++i); //consume the closing quote | ||
} else { | ||
throw new Error('Error parsing "' + initialInput + '": unfinished string at ' + from); | ||
} | ||
|
||
} else if (isDigit(current)) { | ||
|
||
do { | ||
value += current; | ||
current = input.charAt(++i); | ||
|
||
} while (isDigit(current) || current === '.'); | ||
|
||
result.push({ | ||
t: 'num', | ||
v: value.indexOf('.') > -1 ? parseFloat(value) : parseInt(value), | ||
f: from}); | ||
|
||
} else { | ||
throw new Error('Error parsing "' + initialInput + '": unknown token ' + current + ' at ' + from); | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
}; |
Oops, something went wrong.