Skip to content

Instantly share code, notes, and snippets.

@kageroh
Created June 28, 2010 00:51
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 kageroh/455315 to your computer and use it in GitHub Desktop.
Save kageroh/455315 to your computer and use it in GitHub Desktop.
(function() {
var gin = new Gin.Grammar({
document : / prolog element Misc* /,
Char : / <(?:[\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD]|[\uD800-\uDBFF][\uDC00-\uDFFF])> /,
S : / <[\x20\x09\x0D\x0A]+> /,
NameStartChar : / <[:A-Z_a-z\xC0-\xD6\xD8-\xF6\xF8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]> /,
NameChar : / NameStartChar | <[\-.0-9\xB7\u0300-\u036F\u203F-\u2040]> /,
Name : / NameStartChar NameChar* /,
Nmtoken : / NameChar+ /,
EntityValue : / ( '"' ( <[^%&\"]> | PEReference | Reference )* '"' ) | ( "'" ( <[^%&\']> | PEReference | Reference )* "'" ) /,
AttValue : / ( '"' ( <[^<&\"]> | Reference )* '"' ) | ( "'" ( <[^<&\']> | Reference )* "'" ) /,
SystemLiteral : / <(?:"[^\"]*"|'[^\']*')> /,
PubidLiteral : / ( '"' PubidChar* '"' ) | ( "'" ( PubidChar - "'" )* "'" ) /,
PubidChar : / <[\x20\x0D\x0Aa-zA-Z0-9\-\'()+,.\/:=?;!*#@$_%]> /,
CharData : / <[^<&]*> - ( <[^<&]*> '\]\]>' <[^<&]*> ) /,
Comment : / '<!--' ( ( Char - '-' ) | ( '-' ( Char - '-' ) ) )* '-->' /,
PI : / '<?' PITarget ( S ( Char* - ( Char* '?>' Char* ) ) )? '?>' /,
PITarget : / Name - <[Xx][Mm][Ll]> /,
CDSect : / CDStart CData CDEnd /,
CDStart : / '<!\[CDATA\[' /,
CData : / ( Char* - ( Char* '\]\]>' Char* ) ) /,
CDEnd : / '\]\]>' /,
prolog : / XMLDecl? Misc* ( doctypedecl Misc* )? /,
XMLDecl : / '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' /,
VersionInfo : / S 'version' Eq ( ( "'" VersionNum "'" ) | ( '"' VersionNum '"' ) ) /,
Eq : / S? '=' S? /,
VersionNum : / <1\.[0-9]+> /,
Misc : / Comment | PI | S /,
doctypedecl : / '<!DOCTYPE' S Name ( S ExternalID )? S? ( '\[' intSubset '\]' S? )? '>' /,
DeclSep : / PEReference | S /,
intSubset : / ( markupdecl | DeclSep )* /,
markupdecl : / elementdecl | AttlistDecl | EntityDecl | NotationDecl | PI | Comment /,
extSubset : / TextDecl? extSubsetDecl /,
extSubsetDecl : / ( markupdecl | conditionalSect | DeclSep )* /,
SDDecl : / S 'standalone' Eq <(?:'(?:yes|no)'|"(?:yes|no)")> /,
element : / EmptyElemTag | ( STag content ETag ) /,
STag : / '<' Name ( S Attribute )* S? '>' /,
Attribute : / Name Eq AttValue /,
ETag : / '<\/' Name S? '>' /,
content : / CharData? ( ( element | Reference | CDSect | PI | Comment ) CharData? )* /,
EmptyElemTag : / '<' Name ( S Attribute )* S? '\/>' /,
elementdecl : / '<!ELEMENT' S Name S contentspec S? '>' /,
contentspec : / <(?:EMPTY|ANY)> | Mixed | children /,
children : / ( choice | seq ) <[?*+]?> /,
cp : / ( Name | choice | seq ) <[?*+]?> /,
choice : / '(' S? cp ( S? '|' S? cp )+ S? ')' /,
seq : / '(' S? cp ( S? ',' S? cp )* S? ')' /,
Mixed : / ( '(' S? '#PCDATA' ( S? '|' S? Name )* S? ')*' ) | ( '(' S? '#PCDATA' S? ')' ) /,
AttlistDecl : / '<!ATTLIST' S Name AttDef* S? '>' /,
AttDef : / S Name S AttType S DefaultDecl /,
AttType : / StringType | TokenizedType | EnumeratedType /,
StringType : / 'CDATA' /,
TokenizedType : / <(?:ID|IDREF|IDREFS|ENTITY|ENTITIES|NMTOKEN|NMTOKENS)> /,
EnumeratedType : / NotationType | Enumeration /,
NotationType : / 'NOTATION' S '(' S? Name ( S? '|' S? Name )* S? ')' /,
Enumeration : / '(' S? Nmtoken ( S? '|' S? Nmtoken )* S? ')' /,
DefaultDecl : / <(?:#REQUIRED|#IMPLIED)> | ( ( '#FIXED' S )? AttValue ) /,
conditionalSect : / includeSect | ignoreSect /,
includeSect : / '<!\[' S? 'INCLUDE' S? '\[' extSubsetDecl '\]\]>' /,
ignoreSect : / '<!\[' S? 'IGNORE' S? '\[' ignoreSectContents* '\]\]>' /,
ignoreSectContents : / Ignore ( '<!\[' ignoreSectContents '\]\]>' Ignore )* /,
Ignore : / Char* - ( Char* <(?:\<!\[|\]\]\>)> Char* ) /,
CharRef : / <&#[0-9]+;> | <&#x[0-9a-fA-F]+;> /,
Reference : / EntityRef | CharRef /,
EntityRef : / '&' Name ';' /,
PEReference : / '%' Name ';' /,
EntityDecl : / GEDecl | PEDecl /,
GEDecl : / '<!ENTITY' S Name S EntityDef S? '>' /,
PEDecl : / '<!ENTITY' S '%' S Name S PEDef S? '>' /,
EntityDef : / EntityValue | ( ExternalID NDataDecl? ) /,
PEDef : / EntityValue | ExternalID /,
ExternalID : / ( 'SYSTEM' S SystemLiteral ) | ( 'PUBLIC' S PubidLiteral S SystemLiteral ) /,
NDataDecl : / S 'NDATA' S Name /,
TextDecl : / '<?xml' VersionInfo? EncodingDecl S? '?>' /,
extParsedEnt : / TextDecl? content /,
EncodingDecl : / S 'encoding' Eq ( '"' EncName '"' ) | ( "'" EncName "'" ) /,
EncName : / <[A-Za-z][A-Za-z0-9._\-]*> /,
NotationDecl : / '<!NOTATION' S Name S ( ExternalID | PublicID ) S? '>' /,
PublicID : / 'PUBLIC' S PubidLiteral /
}, 'document', Gin.NOTHING);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment