Created
November 28, 2013 08:49
-
-
Save dmajda/7688943 to your computer and use it in GitHub Desktop.
Simple PEG.js grammar to parse a markup language with nested elements (like XML)
This file contains hidden or 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
Content = | |
(Element / Text)* | |
Element = | |
startTag:StartTag content:Content endTag:EndTag { | |
if (startTag != endTag) { | |
throw new Error( | |
"Expected </" + startTag + "> but </" + endTag + "> found." | |
); | |
} | |
return { | |
name: startTag, | |
content: content | |
}; | |
} | |
StartTag = | |
"<" name:TagName ">" { return name; } | |
EndTag = | |
"</" name:TagName ">" { return name; } | |
TagName = chars:[a-z]+ { return chars.join(""); } | |
Text = chars:[^<]+ { return chars.join(""); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment