rename expression.js -> syntax.js

This commit is contained in:
harttle
2016-10-24 23:11:37 +08:00
parent cb75e69122
commit 6d20f7aa8e
8 changed files with 81 additions and 81 deletions
+5 -5
View File
@@ -6,7 +6,7 @@ const lexical = require('./src/lexical.js');
const Tag = require('./src/tag.js');
const Filter = require('./src/filter.js');
const Template = require('./src/parser');
const Expression = require('./src/expression.js');
const Syntax = require('./src/syntax.js');
const tags = require('./tags');
const filters = require('./filters');
const Promise = require('any-promise');
@@ -127,9 +127,9 @@ function resolvePath(root, path) {
}
factory.lexical = lexical;
factory.isTruthy = Expression.isTruthy;
factory.isFalsy = Expression.isFalsy;
factory.evalExp = Expression.evalExp;
factory.evalValue = Expression.evalValue;
factory.isTruthy = Syntax.isTruthy;
factory.isFalsy = Syntax.isFalsy;
factory.evalExp = Syntax.evalExp;
factory.evalValue = Syntax.evalValue;
module.exports = factory;
-55
View File
@@ -1,55 +0,0 @@
const syntax = require('./syntax.js');
const lexical = require('./lexical.js');
function evalExp(exp, scope) {
if (!scope) throw new Error('unable to evalExp: scope undefined');
var operatorREs = lexical.operators,
match;
for (var i = 0; i < operatorREs.length; i++) {
var operatorRE = operatorREs[i];
var expRE = new RegExp(`^(${lexical.quoteBalanced.source})(${operatorRE.source})(${lexical.quoteBalanced.source})$`);
if (match = exp.match(expRE)) {
var l = evalExp(match[1], scope);
var op = syntax.operators[match[2].trim()];
var r = evalExp(match[3], scope);
return op(l, r);
}
}
if (match = exp.match(lexical.rangeLine)) {
var low = evalValue(match[1], scope),
high = evalValue(match[2], scope);
var range = [];
for (var j = low; j <= high; j++) {
range.push(j);
}
return range;
}
return evalValue(exp, scope);
}
function evalValue(str, scope) {
str = str && str.trim();
if (!str) return undefined;
if (lexical.isLiteral(str)) {
return lexical.parseLiteral(str);
}
if (lexical.isVariable(str)) {
return scope.get(str);
}
}
function isTruthy(val) {
if (val instanceof Array) return !!val.length;
return !!val;
}
function isFalsy(val) {
return !isTruthy(val);
}
module.exports = {
evalExp, evalValue, isTruthy, isFalsy
};
+2 -2
View File
@@ -1,5 +1,5 @@
const lexical = require('./lexical.js');
const Exp = require('./expression.js');
const Syntax = require('./syntax.js');
var valueRE = new RegExp(`${lexical.value.source}`, 'g');
@@ -8,7 +8,7 @@ module.exports = function() {
var _filterInstance = {
render: function(output, scope) {
var args = this.args.map(arg => Exp.evalValue(arg, scope));
var args = this.args.map(arg => Syntax.evalValue(arg, scope));
args.unshift(output);
return this.filter.apply(null, args);
},
+13
View File
@@ -0,0 +1,13 @@
var operators = {
'==': (l, r) => l == r,
'!=': (l, r) => l != r,
'>': (l, r) => l > r,
'<': (l, r) => l < r,
'>=': (l, r) => l >= r,
'<=': (l, r) => l <= r,
'contains': (l, r) => l.indexOf(r) > -1,
'and': (l, r) => l && r,
'or': (l, r) => l || r
};
module.exports = operators;
+2 -2
View File
@@ -1,4 +1,4 @@
const Exp = require('./expression.js');
const Syntax = require('./syntax.js');
const Promise = require('any-promise');
var render = {
@@ -89,7 +89,7 @@ var render = {
evalOutput: function(template, scope, opts) {
if(!scope) throw new Error('unable to evalOutput: scope undefined');
var val = Exp.evalExp(template.initial, scope);
var val = Syntax.evalExp(template.initial, scope);
template.filters.some(filter => {
if (filter.error) {
if (opts.strict_filters) {
+54 -12
View File
@@ -1,13 +1,55 @@
var operators = {
'==': (l, r) => l == r,
'!=': (l, r) => l != r,
'>': (l, r) => l > r,
'<': (l, r) => l < r,
'>=': (l, r) => l >= r,
'<=': (l, r) => l <= r,
'contains': (l, r) => l.indexOf(r) > -1,
'and': (l, r) => l && r,
'or': (l, r) => l || r
};
const operators = require('./operators.js');
const lexical = require('./lexical.js');
exports.operators = operators;
function evalExp(exp, scope) {
if (!scope) throw new Error('unable to evalExp: scope undefined');
var operatorREs = lexical.operators,
match;
for (var i = 0; i < operatorREs.length; i++) {
var operatorRE = operatorREs[i];
var expRE = new RegExp(`^(${lexical.quoteBalanced.source})(${operatorRE.source})(${lexical.quoteBalanced.source})$`);
if (match = exp.match(expRE)) {
var l = evalExp(match[1], scope);
var op = operators[match[2].trim()];
var r = evalExp(match[3], scope);
return op(l, r);
}
}
if (match = exp.match(lexical.rangeLine)) {
var low = evalValue(match[1], scope),
high = evalValue(match[2], scope);
var range = [];
for (var j = low; j <= high; j++) {
range.push(j);
}
return range;
}
return evalValue(exp, scope);
}
function evalValue(str, scope) {
str = str && str.trim();
if (!str) return undefined;
if (lexical.isLiteral(str)) {
return lexical.parseLiteral(str);
}
if (lexical.isVariable(str)) {
return scope.get(str);
}
}
function isTruthy(val) {
if (val instanceof Array) return !!val.length;
return !!val;
}
function isFalsy(val) {
return !isTruthy(val);
}
module.exports = {
evalExp, evalValue, isTruthy, isFalsy
};
+2 -2
View File
@@ -1,6 +1,6 @@
const lexical = require('./lexical.js');
const Promise = require('any-promise');
const Exp = require('./expression.js');
const Syntax = require('./syntax.js');
function hash(markup, scope) {
var obj = {}, match;
@@ -8,7 +8,7 @@ function hash(markup, scope) {
while (match = lexical.hashCapture.exec(markup)) {
var k = match[1],
v = match[2];
obj[k] = Exp.evalValue(v, scope);
obj[k] = Syntax.evalValue(v, scope);
}
return obj;
}
+3 -3
View File
@@ -1,10 +1,10 @@
const chai = require("chai");
const expect = chai.expect;
var expression = require('../src/expression.js');
var syntax = require('../src/syntax.js');
var Scope = require('../src/scope.js');
var evalExp = expression.evalExp;
var evalValue = expression.evalValue;
var evalExp = syntax.evalExp;
var evalValue = syntax.evalValue;
describe('expression', function() {
var scope;