Skip to content

Instantly share code, notes, and snippets.

@evan-brass
Created December 25, 2020 22:12
Show Gist options
  • Save evan-brass/652daa90aa6cac97872d09cdde472226 to your computer and use it in GitHub Desktop.
Save evan-brass/652daa90aa6cac97872d09cdde472226 to your computer and use it in GitHub Desktop.
Step 2
function parse_content(cursor) {
let run = true;
while (run && input.length > 0) {
// Parse an open tag
const success = pull(/^<([a-zA-Z][a-zA-Z0-9\-]*)>/, tag => {
const new_tag = { tag, attributes: {}, children: [] };
cursor.children.push(new_tag);
parse_content(new_tag);
}) ||
// Parse close tag
pull(/^<\/([a-zA-Z][a-zA-Z0-9\-]*)>/, tag => {
if (cursor.tag !== tag) {
throw new Error("Unmatched close tag");
}
run = false;
});
if (!success) {
throw new Error("Parse error");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment