Permalink
Browse files

changes

  • Loading branch information...
1 parent 2fdfb05 commit 97b91d36e792ff6eee800974783d807056d5f437 @lfdoherty committed Jul 12, 2012
Showing with 236 additions and 161 deletions.
  1. +40 −23 binaryreader.js
  2. +35 −17 binarysingle.js
  3. +85 −39 binarystream.js
  4. +26 −4 binarywriter.js
  5. +25 −50 bufw.js
  6. +8 −0 parserast.js
  7. +6 −2 parsicle.js
  8. +11 −26 rs.js
View
@@ -1,23 +1,14 @@
var _ = require('underscorem');
function isPrimitive(pt){
- return pt === 'int' || pt === 'string' || pt === 'constant' || pt === 'boolean' || pt === 'byte' || pt === 'long' || pt === 'binary';
+ return pt === 'int' || pt === 'string' || pt === 'constant' || pt === 'boolean' || pt === 'byte' || pt === 'long' || pt === 'binary' || pt === 'real';
}
//var ccc = console.log
function makePrimitiveReader(type, s,part){
if(type === 'int'){
return function(){
- if(!s.has(4)){
- //if(console.log !== ccc){
- // console.log('no has int')
- //}
- return;
- }
- var i = s.readInt();
- //if(console.log !== ccc){
- //console.log('read int: ' + i)
- //}
- return i;
+ if(!s.has(4)){return;}
+ return s.readInt();
}
}else if(type === 'string'){
return function(){
@@ -26,9 +17,15 @@ function makePrimitiveReader(type, s,part){
if(!s.has(len)) return;
return s.readString(len);
}
+ }else if(type === 'real'){
+ return function(){
+ var len = s.readLength();
+ if(len === undefined) return;
+ if(!s.has(len)) return;
+ return Number(s.readString(len));
+ }
}else if(type === 'constant'){
return function(){
- //console.log('"read" constant: ' + part.value)
return part.value;
}
}else if(type === 'boolean'){
@@ -287,28 +284,47 @@ function makeEitherReader(ast, s, getReader,getWrappedReader){
var options = [];
var readersByType = {};
+ var hasObjectType = false
+ var typeToRealTypeMap = {}
for(var i=0;i<ast.length;++i){
var a = ast[i];
var type = getReaderName(a)
if(a.type === 'include'){
- type = 'object';//TODO correct this
- }
- options.push(type);
+ type = a.name//'object';//TODO correct this
+ typeToRealTypeMap[a.name] = a.name
+ hasObjectType = true
+ readersByType[type] = getReader().bind(undefined, a.name)
+ }else{
+ //options.push(type);
- readersByType[type] = makeSpecificReader(a, s, getReader,getWrappedReader)
+ readersByType[type] = makeSpecificReader(a, s, getReader,getWrappedReader)
+ }
}
//console.log('either after: ' + after)
- options.sort();
+ /*options.sort();
var readers = [];
for(var i=0;i<options.length;++i){
var type = options[i];
readers[i] = readersByType[type];
- }
+ }*/
function f(){
- if(!s.has(1)) return;
- var b = s.readByte();
- //console.log('code: ' + b + ' ' + JSON.stringify(options))
- var r = readers[b];
+ //if(!s.has(1)) return;
+
+ var len = s.readLength()
+ if(len === undefined) return
+ if(!s.has(len)) return
+ var b = s.readString(len)
+ if(b === undefined) return;
+ _.assertString(b)
+ //if(typeToRealTypeMap[b]) b = typeToRealTypeMap[b];
+ var r = readersByType[b];
+
+ if(r === undefined) {
+ console.log('got ' + JSON.stringify(Object.keys(readersByType)))
+ _.errout('no reader found for either code(' + b + ')')
+ }else{
+ //console.log('read either: ' + b)
+ }
return r();
}
return f;
@@ -339,6 +355,7 @@ function makeObjectReader(ast, s, getReader,getWrappedReader){
var obj = {};
var worked = nextStep(obj);
if(worked === undefined) return;
+ //console.log('done object')
return obj;
}
return readObject;
View
@@ -41,22 +41,26 @@ exports.add = function(wrapped, state, handle){
//}
//return input;
- var ss = rs.make();
+ var ss = rs.make(true);
var s = ss.s;
var selector = makeBinarySingleReaderInternal(reader, s).selector;
return function(buf){
ss.reset()
-
+ //console.log('begin top-level single read')
_.assertBuffer(buf)
ss.put(buf);
var result = selector(true);
if(result === undefined){
_.errout('single parsing error, needs more bytes: ' + ss.needs())
}
+ //console.log('result: ' + JSON.stringify(result))
ss.assertEmpty()
_.assertDefined(result)
+
+ //console.log('done top-level single read')
+ //console.log(new Error().stack)
return result;
}
}
@@ -71,6 +75,7 @@ exports.add = function(wrapped, state, handle){
readParsers.codeCount = state.ids.length;
function readObject(idCode){
+ //console.log('reading object with idCode: ' + idCode)
var br = baseReaders[idCode];
//if(ccc !== console.log) console.log('idCode: ' + idCode)
@@ -86,14 +91,18 @@ exports.add = function(wrapped, state, handle){
}
function selector(noReader){
- if(!s.has(4)){
+ //console.log('in single selector ^^^^^^^^6')
+ //if(!s.has(4)){
//if(ccc !== console.log) console.log('no parser int')
//return;
- _.errout('not enough data to read type (less than 4 bytes!)')
- }
- var idCode = s.readInt();
-
- var id = state.ids[idCode];
+ // _.errout('not enough data to read type (less than 4 bytes!)')
+ //}
+ var idCodeLen = s.readLength()
+ if(idCodeLen === undefined) _.errout('not enough data')
+ var idCode = s.readString(idCodeLen)//s.readInt();
+ if(idCode === undefined) _.errout('not enough data')
+
+ var id = idCode//state.ids[idCode];
var obj = readObject(idCode);
if(obj !== undefined){
if(!noReader){
@@ -102,6 +111,8 @@ exports.add = function(wrapped, state, handle){
console.log('ids: ' + JSON.stringify(state.ids))
console.log('got: ' + JSON.stringify(Object.keys(baseReaders)));
_.errout('no reader defined for: ' + id);
+ }else{
+ //console.log('reading single: ' + id)
}
typeReader(obj);
}
@@ -120,7 +131,7 @@ exports.add = function(wrapped, state, handle){
var after = [];
function getReader(){
- return selector;
+ return readObject;
}
var wrappedReader = wrapped ? wrapped.binary.single._internalMakeReader({},s) : undefined;
@@ -134,9 +145,13 @@ exports.add = function(wrapped, state, handle){
}
return function(){
- if(!s.has(4)) return;
- var idCode = s.readInt();
- var id = rrm.ids[idCode];
+ //if(!s.has(1)) return;
+ var len = s.readLength()
+ if(len === undefined) return
+ var idCode = s.readString(len)
+ if(idCode === undefined) return
+ //var idCode = //s.readInt();
+ var id = idCode//rrm.ids[idCode];
var result = rrm(idCode)
if(result){
@@ -152,7 +167,7 @@ exports.add = function(wrapped, state, handle){
state.ids.forEach(function(id, idCode){
var parser = readParsers[id];
- var br = baseReaders[idCode] = binaryReader.make(parser, s, getReader,getWrappedReader);
+ var br = baseReaders[id] = binaryReader.make(parser, s, getReader,getWrappedReader);
_.assertFunction(br);
@@ -269,27 +284,30 @@ exports.add = function(wrapped, state, handle){
var dummyProcessors = {};
state.ids.forEach(function(parserId){
+ //_.assertString(parserId)
var parser = state.parsers[parserId];
- var localIdCode = state.idCodes[parserId];
+ //var localIdCode = state.idCodes[parserId];
var jf = maker(parser)
after.push(function(){
optimizer.optimize(jf, parser, makeWriter);
})
- dummyProcessors[localIdCode] = jf;
+ dummyProcessors[parserId] = jf;
var njf = jf;
var rf = unwrapped[parserId] = function(json){
_.assertDefined(json);
var sf = jf.specialize(json);
- var code = localIdCode;
+ var code = parserId;
if(sf !== jf) code = sf.code;
_.assertInt(code)
- w.putInt(code);
+ //w.putInt(code);
+ //_.assertString(code)
+ w.putString(code+'')
sf(json);
sf.optimize(json, writeParsers, state.ids, dummyProcessors)
}
Oops, something went wrong.

0 comments on commit 97b91d3

Please sign in to comment.