Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
asnewman committed Dec 26, 2024
1 parent 3016ea7 commit e12c3b5
Showing 1 changed file with 52 additions and 44 deletions.
96 changes: 52 additions & 44 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
"dialog",
"script",
"template",
"slot",
"slot"
]);
var Tokenizer = class {
cursor = 0;
Expand Down Expand Up @@ -174,20 +174,27 @@
this.cursor++;
continue;
}
if (this.markup[this.cursor] === "{") {
this.result.push({ type: 5 /* L_CURLY */, value: "{" });
this.cursor++;
continue;
}
if (this.markup[this.cursor] === "}") {
this.result.push({ type: 6 /* R_CURLY */, value: "}" });
this.cursor++;
continue;
}
if (this.markup[this.cursor] === "\n") {
this.result.push({ type: 6 /* NEW_LINE */, value: "\n" });
this.result.push({ type: 8 /* NEW_LINE */, value: "\n" });
this.cursor++;
continue;
}
if (this.markup[this.cursor] === " ") {
this.result.push({ type: 8 /* SPACE */, value: " " });
this.result.push({ type: 10 /* SPACE */, value: " " });
this.cursor++;
continue;
}
if (
this.markup[this.cursor] === '"' ||
this.markup[this.cursor] === "'"
) {
if (this.markup[this.cursor] === '"' || this.markup[this.cursor] === "'") {
this.tokenizeQuote();
continue;
}
Expand All @@ -202,7 +209,7 @@
this.result.push(tokenizedHtmlTag);
continue;
} else {
this.result.push({ type: 7 /* WORD */, value: word });
this.result.push({ type: 9 /* WORD */, value: word });
continue;
}
}
Expand All @@ -212,26 +219,15 @@
const startingQuoteSymbol = this.markup[this.cursor];
this.cursor++;
const strArr = [];
while (
this.markup[this.cursor] !== startingQuoteSymbol &&
this.cursor < this.markup.length
) {
while (this.markup[this.cursor] !== startingQuoteSymbol && this.cursor < this.markup.length) {
strArr.push(this.markup[this.cursor]);
this.cursor++;
}
this.result.push({ type: 5 /* STRING */, value: strArr.join("") });
this.result.push({ type: 7 /* STRING */, value: strArr.join("") });
this.cursor++;
}
isNotSymbol() {
return (
this.markup[this.cursor] !== " " &&
this.markup[this.cursor] !== "-" &&
this.markup[this.cursor] !== "=" &&
this.markup[this.cursor] !== "(" &&
this.markup[this.cursor] !== ")" &&
this.markup[this.cursor] !== "\n" &&
this.cursor < this.markup.length
);
return this.markup[this.cursor] !== " " && this.markup[this.cursor] !== "-" && this.markup[this.cursor] !== "=" && this.markup[this.cursor] !== "(" && this.markup[this.cursor] !== ")" && this.markup[this.cursor] !== "{" && this.markup[this.cursor] !== "}" && this.markup[this.cursor] !== "\n" && this.cursor < this.markup.length;
}
};
var Parser = class {
Expand All @@ -242,16 +238,31 @@
// This variable tracks last seen expressions for each dash level to know
// where children should end up
currLevels = {};
// A stack tracking any active curly brackets
activeCurly = [];
constructor(tokens) {
const tokensNoSpaces = tokens.filter(
(t) => t.type !== 8 /* SPACE */ && t.type !== 6 /* NEW_LINE */,
(t) => t.type !== 10 /* SPACE */ && t.type !== 8 /* NEW_LINE */
);
this.tokens = tokensNoSpaces;
}
parse() {
while (this.cursor < this.tokens.length) {
if (this.tokens[this.cursor].type === 5 /* L_CURLY */) {
this.activeCurly.push(this.currDashLevel);
this.cursor++;
continue;
}
if (this.tokens[this.cursor].type === 6 /* R_CURLY */) {
this.activeCurly.pop();
this.cursor++;
continue;
}
if (this.tokens[this.cursor].type === 0 /* DASH */) {
this.currDashLevel = 0;
if (this.activeCurly.length > 0) {
this.currDashLevel = this.activeCurly[this.activeCurly.length - 1];
}
while (this.tokens[this.cursor].type === 0 /* DASH */) {
this.currDashLevel++;
this.cursor++;
Expand All @@ -268,14 +279,12 @@
this.cursor++;
continue;
}
if (this.tokens[this.cursor].type === 5 /* STRING */) {
if (this.tokens[this.cursor].type === 7 /* STRING */) {
const newStringExpression = {
type: 3 /* STRING_LITERAL */,
body: this.tokens[this.cursor].value,
body: this.tokens[this.cursor].value
};
this.currLevels[this.currDashLevel - 1].body.push(
newStringExpression,
);
this.currLevels[this.currDashLevel - 1].body.push(newStringExpression);
this.cursor++;
}
}
Expand All @@ -286,16 +295,13 @@
type: 2 /* TAG */,
tagName: this.tokens[this.cursor].value,
attributes: {},
body: [],
body: []
};
this.cursor++;
if (this.tokens[this.cursor].type === 3 /* L_PAREN */) {
this.cursor++;
while (
this.tokens[this.cursor].type !== 4 /* R_PAREN */ &&
this.cursor < this.tokens.length
) {
if (this.tokens[this.cursor].type !== 7 /* WORD */) {
while (this.tokens[this.cursor].type !== 4 /* R_PAREN */ && this.cursor < this.tokens.length) {
if (this.tokens[this.cursor].type !== 9 /* WORD */) {
throw new Error("Expected attribute for tag");
}
const attributeName = this.tokens[this.cursor].value;
Expand All @@ -312,29 +318,29 @@
parseAttributeValue() {
const token = this.tokens[this.cursor];
this.cursor++;
if (token.type === 5 /* STRING */) {
if (token.type === 7 /* STRING */) {
const value = token.value;
if (value.includes("(") && value[value.length - 1] === ")") {
const parts = value.split("(");
return {
type: 4 /* EVENT_FUNCTION */,
name: parts[0],
arg: parts[1].replace(")", ""),
arg: parts[1].replace(")", "")
};
} else {
return token.value;
}
}
throw new Error(
"On event function must be a string. Instead found " +
JSON.stringify(token),
"On event function must be a string. Instead found " + JSON.stringify(token)
);
}
};
var Transformer = class {
ast = { type: 0 /* ROOT */, body: [] };
cursor = 0;
emit = (e, d) => {};
emit = (e, d) => {
};
constructor(ast, emit) {
this.ast = ast;
this.emit = emit;
Expand All @@ -351,14 +357,16 @@
transformTag(tagExpression) {
if (tagExpression.type !== 2 /* TAG */) {
throw new Error(
"Expected tag expression, instead received: " + tagExpression.type,
"Expected tag expression, instead received: " + tagExpression.type
);
}
const jsonTag = {
[tagExpression.tagName]: tagExpression.body.map((element) => {
if (element.type === 2 /* TAG */) return this.transformTag(element);
if (element.type === 3 /* STRING_LITERAL */) return element.body;
}),
if (element.type === 2 /* TAG */)
return this.transformTag(element);
if (element.type === 3 /* STRING_LITERAL */)
return element.body;
})
};
const onEventAttributes = [];
for (const [key, value] of Object.entries(tagExpression.attributes)) {
Expand Down Expand Up @@ -390,7 +398,7 @@
go: (path) => {
const removedSlash = path.startsWith("/") ? path.substring(1) : path;
window.location.hash = `#${removedSlash}`;
},
}
};
this.render();
window.onhashchange = () => {
Expand Down

0 comments on commit e12c3b5

Please sign in to comment.