[FIX] update py.js, fixes tokenization of expressions with trailing whitespace

bzr revid: xmo@openerp.com-20120301111807-f3lthxw4zkytk42f
This commit is contained in:
Xavier Morel 2012-03-01 12:18:07 +01:00
parent e1ef7c7d88
commit 4a6776d7a1
4 changed files with 31 additions and 22 deletions

View File

@ -1,5 +1,5 @@
repo: 076b192d0d8ab2b92d1dbcfa3da055382f30eaea
node: 0274b3792917c9eba7b096dc2e8f74a508ddaee6
node: 7be96c381d9302b4aafaf5eb5381083b0731a9aa
branch: default
latesttag: 0.4
latesttagdistance: 4
latesttagdistance: 7

View File

@ -1,9 +1,5 @@
var py = {};
(function (py) {
var NUMBER = /^\d$/,
NAME_FIRST = /^[a-zA-Z_]$/,
NAME = /^[a-zA-Z0-9_]$/;
var create = function (o, props) {
function F() {}
F.prototype = o;
@ -256,7 +252,7 @@ var py = {};
var Name = '[a-zA-Z_]\\w*';
var DecNumber = '\\d+'
var DecNumber = '\\d+';
var IntNumber = DecNumber;
var PointFloat = group('\\d+\\.\\d*', '\\.\\d+');
var FloatNumber = PointFloat;
@ -266,7 +262,7 @@ var py = {};
"//=?", "[+\\-*/%&|^=<>]=?", "~");
var Bracket = '[\\[\\]\\(\\)\\{\\}]';
var Special = '[:;.,`@]';
var Funny = group(Operator, Bracket, Special)
var Funny = group(Operator, Bracket, Special);
var ContStr = group("'[^']*'", '"[^"]*"');
@ -280,8 +276,12 @@ var py = {};
while(pseudoprog.lastIndex < max) {
var pseudomatch = pseudoprog.exec(s);
if (!pseudomatch) {
throw new Error('Failed to tokenize ' + s
+ ' at index ' + (end || 0)
// if match failed on trailing whitespace, end tokenizing
if (/^\s+$/.test(s.slice(end))) {
break;
}
throw new Error('Failed to tokenize <<' + s
+ '>> at index ' + (end || 0)
+ '; parsed so far: ' + tokens);
}
@ -325,7 +325,7 @@ var py = {};
tokens.push(create(symbols['(end)']));
return tokens;
}
})()
})();
var token, next;
function expression(rbp) {
@ -398,9 +398,9 @@ var py = {};
var res = constructor.apply(instance, arguments);
// return result of constructor if any, otherwise instance
return res || instance;
}
};
return constructor;
}
};
var hash_counter = 0;
py.object = py.type(function object() {}, {}, {
@ -467,12 +467,12 @@ var py = {};
throw new Error(this.constructor.name + ' can not be converted to JSON');
}
});
NoneType = py.type(function NoneType() {}, py.object, {
var NoneType = py.type(function NoneType() {}, py.object, {
__nonzero__: function () { return py.False; },
toJSON: function () { return null; }
});
py.None = new NoneType();
NotImplementedType = py.type(function NotImplementedType(){});
var NotImplementedType = py.type(function NotImplementedType(){});
py.NotImplemented = new NotImplementedType();
var booleans_initialized = false;
py.bool = py.type(function bool(value) {
@ -641,25 +641,26 @@ var py = {};
return expression();
};
var evaluate_operator = function (operator, a, b) {
var v;
switch (operator) {
case '==': return a.__eq__(b)
case '==': return a.__eq__(b);
case 'is': return a === b ? py.True : py.False;
case '!=': return a.__ne__(b)
case '!=': return a.__ne__(b);
case 'is not': return a !== b ? py.True : py.False;
case '<':
var v = a.__lt__(b);
v = a.__lt__(b);
if (v !== py.NotImplemented) { return v; }
return PY_ensurepy(a.constructor.name < b.constructor.name);
case '<=':
var v = a.__le__(b);
v = a.__le__(b);
if (v !== py.NotImplemented) { return v; }
return PY_ensurepy(a.constructor.name <= b.constructor.name);
case '>':
var v = a.__gt__(b);
v = a.__gt__(b);
if (v !== py.NotImplemented) { return v; }
return PY_ensurepy(a.constructor.name > b.constructor.name);
case '>=':
var v = a.__ge__(b);
v = a.__ge__(b);
if (v !== py.NotImplemented) { return v; }
return PY_ensurepy(a.constructor.name >= b.constructor.name);
case 'in':

View File

@ -84,6 +84,11 @@ describe('Tokenizer', function () {
expect(toks).to.have.tokens(1);
expect(toks[0]).to.be.constant('False');
});
it('does not fuck up on trailing spaces', function () {
var toks = py.tokenize('None ');
expect(toks).to.have.tokens(1);
expect(toks[0]).to.be.constant('None');
});
});
describe('collections', function () {
it('tokenizes opening and closing symbols', function () {

View File

@ -134,7 +134,10 @@ describe('Comparisons', function () {
expect(py.eval('date >= current',
{date: '2010-06-08', current: '2010-06-05'}))
.to.be(true);
expect(py.eval('state == "cancel"', {state: 'cancel'}))
.to.be(true);
expect(py.eval('state == "cancel"', {state: 'open'}))
.to.be(false);
});
});
describe('missing eq/neq', function () {