Skip to content

Commit

Permalink
split up parser
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Apr 29, 2018
1 parent 612b95a commit 03a0590
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 214 deletions.
52 changes: 25 additions & 27 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@
}(this, function() {
"use strict";

var parseComment = function (index, input, length) {
while (index < length) {
var char0 = input[index];
var char1 = input[index + 1];
var char2 = input[index + 2];

if (char0 === "<" && char1 === "!" && char2 === "-" && input[index + 3] === "-") {
index = parseComment(index + 4, input, length);
} else if (char0 === "-" && char1 === "-" && char2 === ">") {
index += 3;
break;
} else {
index += 1;
}
}

return index;
};

var config = {
silent: ("development" === "production") || (typeof console === "undefined")
};
Expand All @@ -38,31 +57,10 @@
'\n': "\\n"
};

var parseIndex;

var pushChild = function (child, stack) {
stack[stack.length - 1].children.push(child);
};

var parseComment = function (index, input, length) {
while (index < length) {
var char0 = input[index];
var char1 = input[index + 1];
var char2 = input[index + 2];

if (char0 === "<" && char1 === "!" && char2 === "-" && input[index + 3] === "-") {
index = parseComment(index + 4, input, length);
} else if (char0 === "-" && char1 === "-" && char2 === ">") {
index += 3;
break;
} else {
index += 1;
}
}

return index;
};

var parseOpeningTag = function (index, input, length, stack) {
var type = "";

Expand All @@ -71,7 +69,7 @@

if (char === ">") {
var element = {
index: parseIndex++,
index: stack.parseIndex++,
type: type,
children: []
};
Expand All @@ -83,7 +81,7 @@
break;
} else if (char === "/" && input[index + 1] === ">") {
pushChild({
index: parseIndex++,
index: stack.parseIndex++,
type: type,
children: []
}, stack);
Expand Down Expand Up @@ -134,7 +132,7 @@
}

pushChild({
index: parseIndex++,
index: stack.parseIndex++,
type: "m-text",
content: content.replace(escapeRE, function (match) { return escapeMap[match]; })
}, stack);
Expand Down Expand Up @@ -165,7 +163,7 @@
}

pushChild({
index: parseIndex++,
index: stack.parseIndex++,
type: "m-expression",
content: expression
}, stack);
Expand All @@ -177,7 +175,6 @@
var length = input.length;
var dependencies = [];
var locals = ["NaN", "false", "in", "m", "null", "true", "typeof", "undefined"];
parseIndex = 0;

var root = {
type: "m-fragment",
Expand All @@ -186,6 +183,7 @@
};

var stack = [root];
stack.parseIndex = 0;

for (var i = 0; i < length;) {
var char = input[i];
Expand Down Expand Up @@ -247,7 +245,7 @@
var generateUpdate = function (element) {
switch (element.type) {
case "m-expression":
return ("m.ut(m[" + (element.index) + "], " + (element.content) + ");")
return ("m.ut(m[" + (element.index) + "], " + (element.content) + ");");
break;
case "m-text":
return "";
Expand Down
2 changes: 1 addition & 1 deletion dist/moon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/compiler/compiler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse } from "./parser";
import { parse } from "./parser/parser";
import { generate } from "./generator";

export const compile = (input) => {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const generateMount = (element, parent) => {
const generateUpdate = (element) => {
switch (element.type) {
case "m-expression":
return `m.ut(m[${element.index}], ${element.content});`
return `m.ut(m[${element.index}], ${element.content});`;
break;
case "m-text":
return "";
Expand Down
184 changes: 0 additions & 184 deletions src/compiler/parser.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/compiler/parser/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const parseComment = (index, input, length) => {
while (index < length) {
const char0 = input[index];
const char1 = input[index + 1];
const char2 = input[index + 2];

if (char0 === "<" && char1 === "!" && char2 === "-" && input[index + 3] === "-") {
index = parseComment(index + 4, input, length);
} else if (char0 === "-" && char1 === "-" && char2 === ">") {
index += 3;
break;
} else {
index += 1;
}
}

return index;
};
32 changes: 32 additions & 0 deletions src/compiler/parser/expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expressionRE, pushChild } from "./util";

export const parseExpression = (index, input, length, stack, dependencies, locals) => {
let expression = "";

for (; index < length; index++) {
const char = input[index];

if (char === "}") {
index += 1;
break;
} else {
expression += char;
}
}

let info;
while ((info = expressionRE.exec(expression)) !== null) {
let name = info[1];
if (name !== undefined && locals.indexOf(name) === -1) {
dependencies.push(name);
}
}

pushChild({
index: stack.parseIndex++,
type: "m-expression",
content: expression
}, stack);

return index;
};
Loading

0 comments on commit 03a0590

Please sign in to comment.