Skip to content

Commit

Permalink
added exercise 6
Browse files Browse the repository at this point in the history
  • Loading branch information
ffesseler committed May 2, 2012
1 parent cce29d6 commit 3b819c4
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions lesson3_parsing/test/allExercises.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,54 @@ describe('Scheme Syntax', function(){
expect(parser("Black DoG")).to.be(undefined);
})
})
})

describe('Write a Scheem Parser', function(){
var parser;
before(function () {
grammar = fs.readFileSync("src/exercise5.pegjs").toString();
parser = wrapExceptions(PEG.buildParser(grammar).parse);
});
describe('#lowercase words separated by space', function(){
it('should not parse empty string', function() {
expect(parser("")).to.be(undefined);
}),
it('should parse "atom"', function() {
expect(parser('atom')).to.eql('atom');
}),
it('should parse "+"', function() {
expect(parser('+')).to.eql('+');
}),
it('should parse "(+ x 3)"', function() {
expect(parser('(+ x 3)')).to.eql(['+', 'x', '3']);
}),
it('should parse "(+ 1 (f x 3 y))"', function() {
expect(parser("(+ 1 (f x 3 y))")).to.eql(["+", "1", ["f", "x", "3", "y"]]);
})
})
})

describe('Parsing arithmetic in PEGs', function(){
var parser;
before(function () {
grammar = fs.readFileSync("src/exercise6.pegjs").toString();
parser = wrapExceptions(PEG.buildParser(grammar).parse);
});
describe('#comma" infix operator that is lower precedence than addition', function(){
it('should parse 1+2', function() {
expect(parser("1+2")).to.eql({tag:"+", left:1, right:2});
}),
it('should parse 1+2*3', function() {
expect(parser("1+2*3")).to.eql({tag:"+", left:1, right:{tag:"*", left:2, right:3}});
}),
it('should parse 1,2', function() {
expect(parser("1,2")).to.eql({tag:",", left:1, right:2});
}),
it('should parse 1,2+3', function() {
expect(parser("1,2+3")).to.eql({tag:",", left:1, right:{tag:"+", left:2, right:3}});
}),
it('should parse 1*2,3', function() {
expect(parser("1*2,3")).to.eql({tag:",", left:{tag:"*", left:1, right:2}, right:3});
})
})
})

0 comments on commit 3b819c4

Please sign in to comment.