Skip to content

Commit

Permalink
check syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyan committed Apr 20, 2011
1 parent bcd4148 commit 845abaa
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
71 changes: 52 additions & 19 deletions lib/breakbeat.js
Expand Up @@ -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 () {};
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 6 additions & 10 deletions tests/tree.js
Expand Up @@ -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;

Expand All @@ -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');
}
Expand Down Expand Up @@ -63,16 +59,16 @@ exports.test = new litmus.Test('ast tests', function () {
]
});

testAst('<?php public class Public_Class_Name { }', {
'name' : 'empty public class ast',
testAst('<?php abstract class Abstract_Class_Name { }', {
'name' : 'empty abstract class ast',
'children' : [
{
'name' : 'empty public class',
'name' : 'empty abstract class',
'type' : breakbeat.ast.Class,
'children' : [],
'props' : {
'name' : 'Public_Class_Name',
'modifiers' : { 'public' : true }
'name' : 'Abstract_Class_Name',
'modifiers' : { 'abstract' : true }
}
}
]
Expand Down

0 comments on commit 845abaa

Please sign in to comment.