Created
March 15, 2015 20:55
-
-
Save zipcode/dc91b54028504f7ebf9a to your computer and use it in GitHub Desktop.
messing about with parsimmon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{string, lazy, alt, regex, optWhitespace, seq, succeed} = require "Parsimmon" | |
lbrace = string '<' | |
rbrace = string '>' | |
slash = string '/' | |
identifier = regex /[a-z-]+/i | |
opentag = lbrace.then(identifier).skip(optWhitespace).skip(rbrace) | |
closetag = (tag) -> lbrace.then(slash).then(string tag).skip(rbrace) | |
selftag = lbrace.then(identifier).skip(optWhitespace).skip(slash).skip(rbrace).map (s) -> "<#{s}/>" | |
atom = regex /[a-z ]+/i | |
term = lazy -> alt(treenode, selftag, atom) | |
treenode = opentag.chain (tag) -> | |
seq(succeed("<#{tag}>"), term.many().skip(closetag tag)) | |
console.log JSON.stringify(term.parse("<hello>hiya<b>hey</b>you are cool</hello>").value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment