diff --git a/examples/buffers/json-payload/index.js b/examples/buffers/json-payload/index.js index 2af8842..e235595 100644 --- a/examples/buffers/json-payload/index.js +++ b/examples/buffers/json-payload/index.js @@ -10,21 +10,21 @@ console.log('example buffers/json') // first added will be the start node. // this decodes a length header specifying the number // of bytes in the following JSON content. -nodes.add('header', function header(control) { +nodes.add('header', function header(control, N) { if ((this.input.length - this.index) < 4) { control.wait() } else { this.contentLength = this.input.readUInt32BE(this.index) this.index += 4 - control.next('content') + control.next(N.content) } }) // this gathers buffers until it has enough to extract // the JSON content; according to length header. -nodes.add('content', function content(control) { +nodes.add('content', function content(control, N) { if (this.buffers && (this.buffersLength + this.input.length >= this.contentLength)) { @@ -34,13 +34,13 @@ nodes.add('content', function content(control) { this.content = Buffer.concat(this.buffers) this.buffers = null this.buffersLength = 0 - control.next('parse', 'header') + control.next(N.parse, N.header) } else if ((this.index + this.contentLength) <= this.input.length) { this.content = this.input.slice(this.index, this.index + this.contentLength) this.index += this.contentLength - control.next('parse', 'header') + control.next(N.parse, N.header) } else { @@ -55,14 +55,14 @@ nodes.add('content', function content(control) { }) // once content is availble we parse the JSON. -nodes.add('parse', function parse(control) { +nodes.add('parse', function parse(control, N) { this.value = JSON.parse(this.content) this.content = null - control.next('report') + control.next(N.report) }) // report it -nodes.add('report', function report(control) { +nodes.add('report', function report(control, N) { console.log('report:\n ', this.value) this.value = null control.next() diff --git a/examples/strings/counter/index.js b/examples/strings/counter/index.js index c6a8cb7..9d9248b 100644 --- a/examples/strings/counter/index.js +++ b/examples/strings/counter/index.js @@ -6,10 +6,10 @@ console.log('example strings/counter\n------------------------------------------ // add our two nodes nodes.addAll({ - count: function count(control) { + count: function count(control, N) { if (!this.input || this.input.length < 1) { - return control.next(this.value.count > 0 ? 'use' : 'stop') + return control.next(this.value.count > 0 ? N.use : N.stop) } else { this.input = this.trim(0, this.input.length - 1) } @@ -37,11 +37,11 @@ nodes.addAll({ if (index >= this.input.length) { control.wait() } else { - control.next('use') + control.next(N.use) } }, - use: function use(control, context) { + use: function use(control, N, context) { // use our value console.log(context.value.ch, '->', context.value.count) @@ -51,13 +51,13 @@ nodes.addAll({ this.value.count = 0 // go back to count another character - control.next('count') + control.next(N.count) }, - stop: function stop(control) { + stop: function stop(control, N) { if (this.input && this.input.length > 0) { - control.next('count') + control.next(N.count) } else { control.wait() } diff --git a/examples/strings/json/nodes.coffee b/examples/strings/json/nodes.coffee index 70c8bc4..335bee1 100644 --- a/examples/strings/json/nodes.coffee +++ b/examples/strings/json/nodes.coffee @@ -58,61 +58,44 @@ F = 'f'.charCodeAt 0 A = 'a'.charCodeAt 0 S = 's'.charCodeAt 0 +# TODO: +# when building up a string to use +# instead of concatenating the parts when we have to wait +# build up an array of individual strings. +# then join() them when we have them all. -module.exports = - - start: (direct) -> - - value = send = start = null +# TODO: +# check strings to see if they are encoded Date values - direct [ 'value', 'send', 'start' ], (v, se, st) -> - value = v - send = se - start = st +module.exports = - (control) -> control.next value, send, start + start: (control, $) -> control.next $.value, $.send, $.start - send: (direct) -> (control) -> + send: (control) -> value = @value @value = null control.result if value is NIL then null else value control.next() - value: (direct) -> - - pair = element = notQuote = stringValue = number = $true = $false = nil = null - - direct [ - 'pair', 'element', '!"', 'stringValue','number', 'true', 'false', 'nil' - ], (p, e, nq, sv, n, t, f, nl) -> - pair = p - element = e - notQuote = nq - stringValue = sv - number = n - $true = t - $false = f - nil = nl - - (control) -> + value: (control, $) -> switch @code() when LEFT_BRACE @next() # increment by at least one, consume space @pushObject() - control.next pair#, rightBrace # TODO: shorten as i did with array... + control.next $.pair#, rightBrace # TODO: shorten as i did with array... when LEFT_BRACKET @next() # increment by at least one, consume space @pushArray() - control.next element + control.next $.element when DOUBLE_QUOTE @index++ - control.next notQuote, stringValue + control.next $.notQuote, $.stringValue # stopped enforcing perfectly valid number format. # grab a glob of characters allowed in a number at least once @@ -124,62 +107,62 @@ module.exports = when PLUS @string = '' @index++ - control.next number + control.next $.number when MINUS @string = '-' @index++ - control.next number + control.next $.number when ZERO @index++ @string = '0' - control.next number + control.next $.number when ONE @string = '1' @index++ - control.next number + control.next $.number when TWO @string = '2' @index++ - control.next number + control.next $.number when THREE @string = '3' @index++ - control.next number + control.next $.number when FOUR @string = '4' @index++ - control.next number + control.next $.number when FIVE @string = '5' @index++ - control.next number + control.next $.number when SIX @string = '6' @index++ - control.next number + control.next $.number when SEVEN @string = '7' @index++ - control.next number + control.next $.number when EIGHT @string = '8' @index++ - control.next number + control.next $.number when NINE @string = '9' @index++ - control.next number + control.next $.number when T @@ -191,7 +174,7 @@ module.exports = control.next() else - control.next $true # TODO: change next node to 'rue' + control.next $.true # TODO: change next node to 'rue' when F @@ -202,7 +185,7 @@ module.exports = control.next() else - control.next $false # TODO: change next node to 'alse' + control.next $.false # TODO: change next node to 'alse' when N @@ -213,28 +196,24 @@ module.exports = control.next() else - control.next nil # TODO: change next node to 'ull' - - else control.fail 'invalid character' - + control.next $.nil # TODO: change next node to 'ull' - string: (direct) -> + else + console.log 'invalid char:', @input[@index], @code(), N + control.fail 'invalid character' - notQuote = null - direct [ '!"' ], (nq) -> notQuote = nq - - (control) -> + string: (control, $) -> if @consumeSpace() and @empty() then control.wait 'wait in string' else if @code() is DOUBLE_QUOTE @index++ - control.next notQuote + control.next $.notQuote else control.fail 'double quote required' - '!"': (direct) -> (control) -> + notQuote: (control) -> if @empty() then return control.wait 'wait in !"' @@ -266,7 +245,7 @@ module.exports = control.wait 'wait in !"' - stringValue: (direct) -> (control) -> + stringValue: (control) -> if @string? @value = @string @@ -276,7 +255,7 @@ module.exports = else control.fail 'incomplete string' - number: (direct) -> (control) -> + number: (control) -> if @empty() then return control.wait 'wait in number' @@ -296,7 +275,7 @@ module.exports = @index = @input.length control.wait 'wait in number' - true: (direct) -> (control) -> + true: (control) -> unless @has 4 then return control.wait 'wait in true' @@ -310,7 +289,7 @@ module.exports = control.next() - false: (direct) -> (control) -> + false: (control) -> unless @has 5 then return control.wait 'wait in false' @@ -325,7 +304,7 @@ module.exports = control.next() - nil: (direct) -> (control) -> + nil: (control) -> unless @has 4 then return control.wait 'wait in nil' @@ -339,17 +318,7 @@ module.exports = control.next() - pair: (direct) -> - - string = key = value = pairComma = null - - direct [ 'string', 'key', 'value', 'pair ,' ], (s, k, v, p) -> - string = s - key = k - value = v - pairComma = p - - (control) -> + pair: (control, $) -> if @code() is RIGHT_BRACE @@ -361,15 +330,10 @@ module.exports = else control.fail '\'}\' without object' - else control.next string, key, value, pairComma - - - key: (direct) -> + else control.next $.string, $.key, $.value, $.pairComma - colon = null - direct [ ':' ], (c) -> colon = c - (control) -> + key: (control, $) -> if @string? @key = @string # NOTE: @pushObject() handles recursion for @key @@ -381,12 +345,12 @@ module.exports = control.next() # else go to the colon node - else control.next colon + else control.next $.colon else control.fail 'string required for key' - ':': (direct) -> (control) -> + 'colon': (control) -> if @consumeSpace() and @empty() then control.wait 'wait for colon' @@ -398,17 +362,7 @@ module.exports = control.fail '\':\' required' - 'pair ,': (direct) -> - - string = key = value = pairComma = null - - direct [ 'string', 'key', 'value', 'pair ,' ], (s, k, v, p) -> - string = s - key = k - value = v - pairComma = p - - (control) -> + 'pairComma': (control, $) -> if @consumeSpace() and @empty() then return control.wait 'wait in "pair ,"' @@ -424,7 +378,7 @@ module.exports = when COMMA @next() # increment by at least one, consume space - control.next string, key, value, pairComma + control.next $.string, $.key, $.value, $.pairComma # handle right brace here when RIGHT_BRACE @@ -441,15 +395,7 @@ module.exports = # an element is any value, push into array, check for comma for more - element: (direct) -> - - value = elementComma = null - - direct [ 'value', 'element ,' ], (v, e) -> - value = v - elementComma = e - - (control) -> + element: (control, $) -> @consumeSpace() @@ -465,16 +411,10 @@ module.exports = else return control.fail '\']\' without array' - else control.next value, elementComma - - - 'element ,': (direct) -> - - elementComma = value = null + else control.next $.value, $.elementComma - direct [ 'element ,', 'value' ], (e, v) -> elementComma = e ; value = v - (control) -> + elementComma: (control, $) -> @consumeSpace() @@ -484,7 +424,7 @@ module.exports = element = @value @value = null - if value? + if element? @array[@array.length] = if element is NIL then null else element else @@ -495,7 +435,7 @@ module.exports = when COMMA @next() # increment by at least one, consume space - control.next value, elementComma # element + control.next $.value, $.elementComma # element when RIGHT_BRACKET @next() diff --git a/examples/strings/json/parser.js b/examples/strings/json/parser.js index e4c062a..d07773b 100644 --- a/examples/strings/json/parser.js +++ b/examples/strings/json/parser.js @@ -13,9 +13,6 @@ states.addAll(nodes, true) // ensure we start at 'start' states.start('start') -// now make node use direct instead of via names. -states.direct() - // create a strings executor with our custom context executor = states.strings({ context: context diff --git a/examples/transforms/math/index.js b/examples/transforms/math/index.js index b9e32b9..0180b2d 100644 --- a/examples/transforms/math/index.js +++ b/examples/transforms/math/index.js @@ -5,7 +5,7 @@ console.log('example transforms/math') nodes.addAll({ - start: function start(control) { + start: function start(control, N) { if (!this.input) { // transform pipes to process.stdout // so this will be output to the console. @@ -13,50 +13,52 @@ nodes.addAll({ } else if (this.input.op) { - control.next('op') + control.next(N.op) } else if (this.input.value) { - control.next('value') + control.next(N.value) } else { this.error = 'unknown input' - control.next('error') + control.next(N.error) } }, - isOverwrite: function isOverwrite(control) { + isOverwrite: function isOverwrite(control, N) { if (this.op) { this.error = 'overwriting op' - control.next('error') + control.next(N.error) } else { control.next() } }, - op: function op(control) { + op: function op(control, N) { this.op = this.input.op this.input = null control.next() }, - value: function value(control) { + value: function value(control, N) { this.values = this.values || [] this.values.push(this.input.value) this.input = null control.next() }, - 'ready?': function isReady(control) { + 'ready?': function isReady(control, N) { if (this.op && this.values && this.values.length > 1) { - control.next('calculate') + control.next(N.calculate) } else { - control.next('start') + control.next(N.start) } }, - calculate: function calculate(control, context) { + // example of when `context` arg is helpful: + // us it in a closure where `this` is no longer `context`. + calculate: function calculate(control, N, context) { var result = this.values.reduce(function(acc, el) { if (acc) { return context.op(acc, el) @@ -64,8 +66,8 @@ nodes.addAll({ return el } }) - context.$push('result = ' + result + '\n') - control.next('start') + this.$push('result = ' + result + '\n') + control.next(N.start) }, reset: function reset(control) {