Skip to content

Instantly share code, notes, and snippets.

@ralekna
Created August 5, 2019 12:10
Show Gist options
  • Save ralekna/807db345cfc52da84be5bcc85c478027 to your computer and use it in GitHub Desktop.
Save ralekna/807db345cfc52da84be5bcc85c478027 to your computer and use it in GitHub Desktop.
Simple XML gramar for PEG.js
Tag = ContainerTag / SelfClosingTag
ContainerTag = OpeningBracket name:Text attributes:Attributes ClosingBracket children:Tag* OpeningBracket Slash closingName:Text ClosingBracket {
if (name !== closingName) {
throw new Error('Missmatching closing tag')
}
return {name, children, attributes}
}
SelfClosingTag = OpeningBracket name:Text attributes:Attributes _ Slash ClosingBracket { return {name, attributes} }
Attributes = (_ attribute:Attribute { return attribute })*
Attribute = name:Text "=" Quote value:Text Quote { return {name, value} }
Text = [a-z]* { return text() }
Quote = "\""
OpeningBracket = "<"
ClosingBracket = ">"
Slash = "/"
_ "whitespace"
= [ \t\n\r]*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment