From 845abaaefaf68ca004c09cdfabc72b77edd196d2 Mon Sep 17 00:00:00 2001 From: Tom Yandell Date: Wed, 20 Apr 2011 21:39:13 +0100 Subject: [PATCH] check syntax --- lib/breakbeat.js | 71 +++++++++++++++++++++++++++++++++++------------- tests/tree.js | 16 ++++------- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/lib/breakbeat.js b/lib/breakbeat.js index 5e766c9..ad01bbb 100644 --- a/lib/breakbeat.js +++ b/lib/breakbeat.js @@ -5,40 +5,70 @@ var sys = require('sys'), spawn = require('child_process').spawn, promise = require('promised-io/lib/promise'); -exports.parseString = function (code) { - var php = spawn('php', [ __dirname + '/../bin/breakbeat.php' ]), - json = '', +function runPhp (code, args) { + var php = spawn('php', args), errors = '', + output = '', done = new promise.Promise(); - php.stdout.on('data', function (data) { - json += data; - }); - php.stderr.on('data', function (data) { errors += data; }); + php.stdout.on('data', function (data) { + output += data; + }); + php.on('exit', function (code) { - if (code !== 0) { - return done.reject(errors); - } - var data; - try { - data = JSON.parse(json); + if (code === 0) { + done.resolve(output); } - catch (e) { - return done.reject(e + '\n' + json); + else { + done.reject(errors); } - var parser = new Parser(data); - done.resolve(parser.parse()); }); - + php.stdin.end(code); return done; -}; +} +exports.parseString = function (code) { + var done = new promise.Promise(); + + runPhp(code, [ '-l' ]).then( + function () { + runPhp(code, [ __dirname + '/../bin/breakbeat.php' ]).then( + function (json) { + var data; + try { + data = JSON.parse(json); + } + catch (e) { + done.reject(new Error('invalid json: ' + e + '\n' + json)); + return; + } + try { + var parser = new Parser(data); + done.resolve(parser.parse()); + } + catch (e) { + done.reject(e); + } + }, + function (e) { + done.reject(new Error('could not get tokens: ' + e)); + } + ); + }, + function (e) { + done.reject(e); + } + ); + + return done; +}; + function extend (child, parent) { var proto = child.prototype, p = function () {}; @@ -233,6 +263,9 @@ Parser.prototype.run = function (end) { break; case 'T_WHITESPACE': break; + case 'T_ABSTRACT': + this.ast.addModifier('abstract'); + break; case 'T_PUBLIC': this.ast.addModifier('public'); break; diff --git a/tests/tree.js b/tests/tree.js index 00c9f7c..2123167 100644 --- a/tests/tree.js +++ b/tests/tree.js @@ -6,7 +6,7 @@ var litmus = require('litmus'), sys = require('sys'); exports.test = new litmus.Test('ast tests', function () { - this.plan(61); + this.plan(47); var test = this; @@ -25,10 +25,6 @@ exports.test = new litmus.Test('ast tests', function () { test.ok(node.leftOperand, against.name + ' has left operand'); checkNode(node.leftOperand, against.leftOperand); } - if ('rightOperand' in against) { - test.ok(node.rightOperand, against.name + ' has right operand'); - checkNode(node.rightOperand, against.rightOperand); - } if ('type' in against) { test.isa(node, against.type, against.name + ' type'); } @@ -63,16 +59,16 @@ exports.test = new litmus.Test('ast tests', function () { ] }); - testAst('