I am building a small framework that reads my custom made HTML syntax and converts it into HTML code. However, I am stuck at tokenising my code and creating an AST. I understand that the algorithm requires recursive approach but I cannot figure out how to do it properly.
This is my custom code in app.txt file:
View {
Heading {
}
Text {
}
}
And here is my recursive parser so far:
function parse(source) {
let tag = "";
let children = [];
for (let i = 0; i < source.length; i++) {
const char = source[i];
if (char === "{") {
const child = parse(source.substring(i + 1, source.length));
children.push(child);
} else if (char === "}") {
return {
tag: tag,
children: children
};
} else {
tag += char;
}
}
return;
}
The parses is expected to produce something like this (and should be able to go to any depth):
{
tag: "View",
children: [
{
tag: "Heading",
children: []
},
{
tag: "Text",
children: []
}
]
}
What am I doing wrong? I'll be thankful for any help.