Skip to content

Commit

Permalink
Fixes issues with grammar
Browse files Browse the repository at this point in the history
As reported in #66 there was a
flaw with our grammar parsing for high level tokens.

This fixes the issue by removing whitespace from areas where it is
insignificant.
  • Loading branch information
tbranyen committed May 3, 2015
1 parent 705cf8a commit f9b0ca3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/grammar.js
Expand Up @@ -25,11 +25,11 @@ define(function(require, exports, module) {
this.delimiters = delimiters;

this.internal = [
makeEntry("START_IF", "if "),
makeEntry("START_IF", "if"),
makeEntry("ELSE", "else"),
makeEntry("ELSIF", "elsif "),
makeEntry("ELSIF", "elsif"),
makeEntry("END_IF", "endif"),
makeEntry("NOT", "not "),
makeEntry("NOT", "not"),
makeEntry("EQUALITY", "=="),
makeEntry("NOT_EQUALITY", "!="),
makeEntry("GREATER_THAN_EQUAL", ">="),
Expand Down
9 changes: 7 additions & 2 deletions lib/tree.js
Expand Up @@ -557,9 +557,15 @@ define(function(require, exports, module) {
break LOOP;

default:
var prevCondition = root.conditions[root.conditions.length - 1];

// If it's a whitespace we need the non-trimmed value
if (node.name == "WHITESPACE") {
if (node.name === "WHITESPACE") {
value = node.capture[0];

if (!root.conditions.length || prevCondition.type !== "Literal") {
break;
}
}

// Parse out the argument property to whether it's a Literal value
Expand All @@ -573,7 +579,6 @@ define(function(require, exports, module) {
prev.value += value;
}
else {

if (node.name != "START_RAW" && node.name != "START_PROP") {
// this value needs to be processed by constructProperty so put
// it back on the top of the stack
Expand Down
19 changes: 17 additions & 2 deletions test/tests/expressions.js
Expand Up @@ -22,11 +22,26 @@ define(function(require, exports, module) {
describe("if statement", function() {
it("must have at least one condition", function() {
assert.throws(function() {
var tmpl = combyne("{%if%}{%endif%}");
var output = tmpl.render();
combyne("{%if%}{%endif%}");
});
});

it("will report the correct error for missing conditions", function() {
try {
combyne("{%if%}{%endif%}");
}
catch (ex) {
assert.equal(ex.message, "Missing conditions to if statement.");
}
});

it("can contain an empty body", function() {
var tmpl = combyne("{%if true%}{%endif%}");
var output = tmpl.render()

assert.equal(output, "");
});

it("can evaluate basic truthy", function() {
var tmpl = combyne("{%if test%}hello world{%endif%}");
var output = tmpl.render({ test: true })
Expand Down

0 comments on commit f9b0ca3

Please sign in to comment.