Skip to content

Instantly share code, notes, and snippets.

@4mitch
Created July 14, 2017 14:35
Show Gist options
  • Save 4mitch/dd16b4172741669ad963933e1af027fb to your computer and use it in GitHub Desktop.
Save 4mitch/dd16b4172741669ad963933e1af027fb to your computer and use it in GitHub Desktop.
PEG.JS html parser
start = root
root = xml:xml { return {xml} }
xml = tag
tag = tagopen:tagopen innerHTML:(tag / text)* tagclose
{ let a = {}; a[tagopen.valueOf()]= innerHTML; return a }
tagopen = _ "<" text:text ">" _ { return text }
tagclose = _ "</" text:text ">" _ { return text }
text = all+ { return text() }
all = _ text:n _ { return text }
n "notWhitespace" =
[^<>/]+ {return ""}
_ "whitespace" =
[ \t\n\r]* {return ""}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment