Skip to content

Commit

Permalink
avoid many lookups in lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed May 20, 2017
1 parent d695684 commit c5805ba
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
26 changes: 16 additions & 10 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -1064,15 +1064,17 @@
};

var lexText = function (state) {
var current = state.current;
var input = state.input;
var len = input.length;
var endOfText = input.substring(state.current).search(tagOrCommentStartRE);

var endOfText = input.substring(current).search(tagOrCommentStartRE);

// Only Text
if (endOfText === -1) {
state.tokens.push({
type: "text",
value: input.slice(state.current)
value: input.slice(current)
});
state.current = len;
return;
Expand All @@ -1084,25 +1086,28 @@
}

// End of Text Found
endOfText += state.current;
endOfText += current;
state.tokens.push({
type: "text",
value: input.slice(state.current, endOfText)
value: input.slice(current, endOfText)
});
state.current = endOfText;
};

var lexComment = function (state) {
var current = state.current;
var input = state.input;
var len = input.length;
state.current += 4;
var endOfComment = input.indexOf("-->", state.current);

current += 4;

var endOfComment = input.indexOf("-->", current);

// Only an unclosed comment
if (endOfComment === -1) {
state.tokens.push({
type: "comment",
value: input.slice(state.current)
value: input.slice(current)
});
state.current = len;
return;
Expand All @@ -1111,25 +1116,26 @@
// End of Comment Found
state.tokens.push({
type: "comment",
value: input.slice(state.current, endOfComment)
value: input.slice(current, endOfComment)
});
state.current = endOfComment + 3;
};

var lexTag = function (state) {
var current = state.current;
var input = state.input;
var len = input.length;

// Lex Starting of Tag
var isClosingStart = input.charAt(state.current + 1) === "/";
var isClosingStart = input.charAt(current + 1) === "/";
state.current += isClosingStart ? 2 : 1;

// Lex type and attributes
var tagToken = lexTagType(state);
lexAttributes(tagToken, state);

// Lex ending tag
var isClosingEnd = input.charAt(state.current) === "/";
var isClosingEnd = input.charAt(current) === "/";
state.current += isClosingEnd ? 2 : 1;

// Check if Closing Start
Expand Down
Loading

0 comments on commit c5805ba

Please sign in to comment.