Browse Source

Add line/character counters and clean-up messaging a bit.

tags/0.5.0-vsc
Sahab Yazdani 15 years ago
parent
commit
1af71759e4
2 changed files with 27 additions and 12 deletions
  1. +24
    -9
      mustache.js
  2. +3
    -3
      test/unit.js

+ 24
- 9
mustache.js View File

@@ -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';
}
@@ -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 */
@@ -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) {
@@ -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
@@ -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];
}
@@ -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
@@ -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])) {
@@ -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,
@@ -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,
@@ -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(
@@ -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 + '".');
}
}


+ 3
- 3
test/unit.js View File

@@ -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.'
);
@@ -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.'
);
@@ -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'
);


Loading…
Cancel
Save