Bladeren bron

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

tags/0.5.0-vsc
Sahab Yazdani 15 jaren geleden
bovenliggende
commit
1af71759e4
2 gewijzigde bestanden met toevoegingen van 27 en 12 verwijderingen
  1. +24
    -9
      mustache.js
  2. +3
    -3
      test/unit.js

+ 24
- 9
mustache.js Bestand weergeven

@@ -118,6 +118,10 @@ var Mustache = (function(undefined) {
return text.replace(escapeCompiledRegex, '\\$1'); return text.replace(escapeCompiledRegex, '\\$1');
} }
function is_newline(token) {
return token.match(/\r?\n/);
}
function is_function(a) { function is_function(a) {
return a && typeof a === 'function'; return a && typeof a === 'function';
} }
@@ -130,6 +134,10 @@ var Mustache = (function(undefined) {
return Object.prototype.toString.call(a) === '[object Array]'; return Object.prototype.toString.call(a) === '[object Array]';
} }
function create_error(line, character, message) {
return new Error('(' + line + ',' + character + '): ' + message);
}
/* END Helpers */ /* END Helpers */


/* BEGIN Compiler */ /* BEGIN Compiler */
@@ -153,6 +161,13 @@ var Mustache = (function(undefined) {
} else { } else {
state.parser.text(state, token); state.parser.text(state, token);
} }
if (is_newline(token)) {
state.character = 1;
state.line++;
} else {
state.character+=token.length;
}
} }
if (!noReturn) { if (!noReturn) {
@@ -193,10 +208,10 @@ var Mustache = (function(undefined) {
tokenizer = new RegExp(parts.join('|')); tokenizer = new RegExp(parts.join('|'));
} }


var code = []; var code = [];
var state = { var state = {
template: template || ''
line: 1, character: 1
, template: template || ''
, partials: partials || {} , partials: partials || {}
, openTag: openTag , openTag: openTag
, closeTag: closeTag , closeTag: closeTag
@@ -251,7 +266,7 @@ var Mustache = (function(undefined) {
for (i=0, n=optionPairs.length; i<n; ++i) { for (i=0, n=optionPairs.length; i<n; ++i) {
scratch = optionPairs[i].split('='); scratch = optionPairs[i].split('=');
if (scratch.length !== 2) { 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]; options[scratch[0]] = scratch[1];
} }
@@ -260,7 +275,7 @@ var Mustache = (function(undefined) {
if (is_function(directives[pragma])) { if (is_function(directives[pragma])) {
directives[pragma](options); directives[pragma](options);
} else { } 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 return ''; // blank out all pragmas
@@ -345,7 +360,7 @@ var Mustache = (function(undefined) {
template, program; template, program;
if (!state.partials[variable]) { 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])) { if (!is_function(state.partials[variable])) {
@@ -394,7 +409,7 @@ var Mustache = (function(undefined) {
return ctx; return ctx;
} }
}
}
var s = state.section, template = s.template_buffer.join(''), var s = state.section, template = s.template_buffer.join(''),
program = compile(create_compiler_state(template, program = compile(create_compiler_state(template,
@@ -447,7 +462,7 @@ var Mustache = (function(undefined) {
'!': noop, '!': noop,
'#': begin_section, '#': begin_section,
'^': 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,
'{': interpolate, '{': interpolate,
'>': partial, '>': 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))); var matches = token.match(new RegExp(escape_regex(state.openTag) + '=(\\S*?)\\s*(\\S*?)=' + escape_regex(state.closeTag)));


if ((matches || []).length!==3) { 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( var new_state = create_compiler_state(
@@ -543,7 +558,7 @@ var Mustache = (function(undefined) {
delete state.section; delete state.section;
state.parser = default_parser; state.parser = default_parser;
} else { } 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 Bestand weergeven

@@ -52,7 +52,7 @@ test("Parser", function() {
{} {}
); );
}, function(e) { }, 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.' 'Malformed tags should be handled correctly.'
); );
@@ -387,7 +387,7 @@ test("'>' (Partials)", function() {
{partal: ''} {partal: ''}
); );
}, function(e) { }, function(e) {
return e.message === "Unknown partial 'partial'";
return e.message === '(1,1): Unknown partial "partial".';
}, },
'Missing partials should be handled correctly.' 'Missing partials should be handled correctly.'
); );
@@ -477,7 +477,7 @@ test("'%' (Pragmas)", function() {
); );
}, },
function(e) { 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' 'Notification of unimplemented pragmas'
); );


Laden…
Annuleren
Opslaan