Skip to content

Instantly share code, notes, and snippets.

@zipcode
Created March 15, 2015 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zipcode/dc91b54028504f7ebf9a to your computer and use it in GitHub Desktop.
Save zipcode/dc91b54028504f7ebf9a to your computer and use it in GitHub Desktop.
messing about with parsimmon
{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