Skip to content

Commit

Permalink
Add line/character counters and clean-up messaging a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
thegrandpoobah committed Apr 27, 2011
1 parent 0f4d8b5 commit 1af7175
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
33 changes: 24 additions & 9 deletions mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ var Mustache = (function(undefined) {
return text.replace(escapeCompiledRegex, '\\$1');
}

function is_newline(token) {
return token.match(/\r?\n/);
}

function is_function(a) {
return a && typeof a === 'function';
}
Expand All @@ -130,6 +134,10 @@ var Mustache = (function(undefined) {
return Object.prototype.toString.call(a) === '[object Array]';
}

function create_error(line, character, message) {
return new Error('(' + line + ',' + character + '): ' + message);
}

/* END Helpers */

/* BEGIN Compiler */
Expand All @@ -153,6 +161,13 @@ var Mustache = (function(undefined) {
} else {
state.parser.text(state, token);
}

if (is_newline(token)) {
state.character = 1;
state.line++;
} else {
state.character+=token.length;
}
}

if (!noReturn) {
Expand Down Expand Up @@ -193,10 +208,10 @@ var Mustache = (function(undefined) {
tokenizer = new RegExp(parts.join('|'));
}


var code = [];
var state = {
template: template || ''
line: 1, character: 1
, template: template || ''
, partials: partials || {}
, openTag: openTag
, closeTag: closeTag
Expand Down Expand Up @@ -251,7 +266,7 @@ var Mustache = (function(undefined) {
for (i=0, n=optionPairs.length; i<n; ++i) {
scratch = optionPairs[i].split('=');
if (scratch.length !== 2) {
throw new Error('Malformed pragma options:' + optionPairs[i]);
throw create_error(state.line, state.character, 'Malformed pragma option "' + optionPairs[i] + '".');
}
options[scratch[0]] = scratch[1];
}
Expand All @@ -260,7 +275,7 @@ var Mustache = (function(undefined) {
if (is_function(directives[pragma])) {
directives[pragma](options);
} else {
throw new Error('This implementation of mustache does not implement the "' + pragma + '" pragma');
throw create_error(state.line, state.character, 'This implementation of mustache does not implement the "' + pragma + '" pragma.');
}

return ''; // blank out all pragmas
Expand Down Expand Up @@ -345,7 +360,7 @@ var Mustache = (function(undefined) {
template, program;

if (!state.partials[variable]) {
throw new Error('Unknown partial \'' + variable + '\'');
throw create_error(state.line, state.character, 'Unknown partial "' + variable + '".');
}

if (!is_function(state.partials[variable])) {
Expand Down Expand Up @@ -394,7 +409,7 @@ var Mustache = (function(undefined) {

return ctx;
}
}
}

var s = state.section, template = s.template_buffer.join(''),
program = compile(create_compiler_state(template,
Expand Down Expand Up @@ -447,7 +462,7 @@ var Mustache = (function(undefined) {
'!': noop,
'#': begin_section,
'^': begin_section,
'/': function(state, token) { throw new Error('Unbalanced End Section tag: ' + token); },
'/': function(state, token) { throw create_error(state.line, state.character, 'Unbalanced End Section tag "' + token + '".'); },
'&': interpolate,
'{': interpolate,
'>': partial,
Expand Down Expand Up @@ -487,7 +502,7 @@ var Mustache = (function(undefined) {
var matches = token.match(new RegExp(escape_regex(state.openTag) + '=(\\S*?)\\s*(\\S*?)=' + escape_regex(state.closeTag)));

if ((matches || []).length!==3) {
throw new Error('Malformed change delimiter token: ' + token);
throw create_error(state.line, state.character, 'Malformed change delimiter token: "' + token + '".');
}

var new_state = create_compiler_state(
Expand Down Expand Up @@ -543,7 +558,7 @@ var Mustache = (function(undefined) {
delete state.section;
state.parser = default_parser;
} else {
throw new Error('Unexpected section end tag "' + variable + '". Expected: ' + state.section.variable);
throw create_error(state.line, state.character, 'Unexpected section end tag "' + variable + '", expected "' + state.section.variable + '".');
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ test("Parser", function() {
{}
);
}, function(e) {
return e.message === 'Malformed change delimiter token: {{=tag1}}';
return e.message === '(1,1): Malformed change delimiter token: "{{=tag1}}".';
},
'Malformed tags should be handled correctly.'
);
Expand Down Expand Up @@ -387,7 +387,7 @@ test("'>' (Partials)", function() {
{partal: ''}
);
}, function(e) {
return e.message === "Unknown partial 'partial'";
return e.message === '(1,1): Unknown partial "partial".';
},
'Missing partials should be handled correctly.'
);
Expand Down Expand Up @@ -477,7 +477,7 @@ test("'%' (Pragmas)", function() {
);
},
function(e) {
return e.message === 'This implementation of mustache does not implement the "I-HAVE-THE-GREATEST-MUSTACHE" pragma';
return e.message === '(1,1): This implementation of mustache does not implement the "I-HAVE-THE-GREATEST-MUSTACHE" pragma.';
},
'Notification of unimplemented pragmas'
);
Expand Down

0 comments on commit 1af7175

Please sign in to comment.