better recursion?

This commit is contained in:
Christopher Jeffrey 2011-08-13 16:53:15 -05:00
parent 2f6396e217
commit a2cccfaf1d

View File

@ -32,32 +32,7 @@ var links;
* Lexer * Lexer
*/ */
var lex = function(str) { var lex_ = function lex(str, tokens, line) {
var tokens = []
, line = 0;
// normalize whitespace
str = str.replace(/\r\n/g, '\n')
.replace(/\r/g, '\n');
str = str.replace(/\t/g, ' ');
//str = str.replace(/(^|\n) +(\n|$)/g, '$1$2');
// unfortunately, this is the most
// performant method of getting link
// definitions out of the way.
links = {};
str = str.replace(
/^ {0,3}\[([^\]]+)\]: *([^ ]+)(?: +"([^"]+)")?/gm,
function(_, id, href, title) {
links[id] = {
href: href,
title: title
};
return '';
});
(function lex(str) {
var i var i
, key , key
, rule; , rule;
@ -134,7 +109,7 @@ var lex = function(str) {
type: 'list_item_start', type: 'list_item_start',
line: line line: line
}); });
lex(item, tokens); lex(item, tokens, line);
tokens.push({ tokens.push({
type: 'list_item_end', type: 'list_item_end',
line: line line: line
@ -159,7 +134,7 @@ var lex = function(str) {
line: line line: line
}); });
cap = cap[0].replace(/^ *>/gm, ''); cap = cap[0].replace(/^ *>/gm, '');
lex(cap, tokens); lex(cap, tokens, line);
tokens.push({ tokens.push({
type: 'blockquote_end', type: 'blockquote_end',
line: line line: line
@ -168,7 +143,34 @@ var lex = function(str) {
} }
break; break;
} }
})(str); };
var lex = function(str) {
var tokens = []
, line = 0;
// normalize whitespace
str = str.replace(/\r\n/g, '\n')
.replace(/\r/g, '\n');
str = str.replace(/\t/g, ' ');
//str = str.replace(/(^|\n) +(\n|$)/g, '$1$2');
// unfortunately, this is the most
// performant method of getting link
// definitions out of the way.
links = {};
str = str.replace(
/^ {0,3}\[([^\]]+)\]: *([^ ]+)(?: +"([^"]+)")?/gm,
function(_, id, href, title) {
links[id] = {
href: href,
title: title
};
return '';
});
lex_(str, tokens, line);
return tokens; return tokens;
}; };