Skip to content

Commit

Permalink
Added lesson 3 exercise 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ffesseler committed Apr 30, 2012
1 parent 22ee98e commit 0a80cf7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lesson3_parsing/src/exercise2.pegjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
start =
countrycode

alpha =
[a-z]

countrycode =
first:alpha second:alpha
{return first + second}
32 changes: 32 additions & 0 deletions lesson3_parsing/test/exercise2test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var PEG = require("pegjs");
var expect = require('expect.js');
var fs = require('fs');
var grammar;



var wrapExceptions = function(f) {
return function(x) {
try {
return f(x);
}
catch(err) {
return undefined;
}
};
};

grammar = fs.readFileSync("src/exercise2.pegjs").toString();
parser = wrapExceptions(PEG.buildParser(grammar).parse);


describe('Parser Actions', function(){
describe('#countrycode', function(){
it('should parse canada', function() {
expect(parser("ca")).to.eql('ca');
}),
it('should not parse uae', function() {
expect(parser("uae")).to.be(undefined);
})
})
})

0 comments on commit 0a80cf7

Please sign in to comment.