Advertisement
Guest User

Regex Parsing XML

a guest
Feb 12th, 2017
9,389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 43.55 KB | None | 0 0
  1. namespace XmlParser
  2. {
  3.     using System.Collections.Generic;
  4.     using System.Linq;
  5.     using System.Text.RegularExpressions;
  6.  
  7.     public class XmlParser
  8.     {
  9.         public readonly Regex Regex;
  10.         public readonly string[] RegexGroupNames;
  11.  
  12.         public XmlParser(string pattern)
  13.         {
  14.             Regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
  15.             RegexGroupNames = Regex.GetGroupNames().Where(p => p != "0").ToArray();
  16.         }
  17.  
  18.         protected XmlParser(Regex regex)
  19.         {
  20.             Regex = regex;
  21.             RegexGroupNames = Regex.GetGroupNames().Where(p => p != "0").ToArray();
  22.         }
  23.  
  24.         public static implicit operator XmlParser(Regex regex)
  25.         {
  26.             return new XmlParser(regex);
  27.         }
  28.  
  29.         public IList<NamedCapture> ParseXml(string xml)
  30.         {
  31.             Match match = Regex.Match(xml);
  32.  
  33.             if (!match.Success)
  34.             {
  35.                 return new NamedCapture[0];
  36.             }
  37.  
  38.             var groups = from p in RegexGroupNames select new NamedGroup(p, match.Groups[p]);
  39.  
  40.             var captures = groups
  41.                 .SelectMany(p => p.Group.Captures.OfType<Capture>(), (p, q) => new NamedCapture(p.Name, q))
  42.                 .OrderBy(p => p.Capture.Index);
  43.  
  44.             return captures.ToList().AsReadOnly();
  45.         }
  46.  
  47.         public static bool IsError(IList<NamedCapture> captures)
  48.         {
  49.             var last = captures.LastOrDefault();
  50.  
  51.             if (last == null)
  52.             {
  53.                 return true;
  54.             }
  55.  
  56.             return last.Name == "ERROR";
  57.         }
  58.     }
  59. }
  60.  
  61.  
  62. //#define XML11
  63.  
  64. /* Based on (and with quotes from):
  65.  *
  66.  * Extensible Markup Language (XML) 1.0 (Fifth Edition)
  67.  * W3C Recommendation 26 November 2008
  68.  * http://www.w3.org/TR/2008/REC-xml-20081126/
  69.  * Copyright © 2008 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark and document use rules apply.
  70.  *
  71.  * and
  72.  *
  73.  * Extensible Markup Language (XML) 1.1 (Second Edition)
  74.  * W3C Recommendation 16 August 2006, edited in place 29 September 2006
  75.  * http://www.w3.org/TR/2006/REC-xml11-20060816
  76.  * Copyright © 2006 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark and document use rules apply.
  77.  */
  78.  
  79. namespace XmlParser
  80. {
  81.     using System;
  82.  
  83.     public static class XmlChars
  84.     {
  85.         private static readonly string pair = @"[\uD800-\uDBFF][\uDC00-\uDFFF]";
  86.  
  87.         /* Character Range */
  88.  
  89. #if !(XML11)
  90.         private static readonly string charSingle = @"[\x09 \x0A \x0D \x20-\uD7FF \uE000-\uFFFD]".Replace(" ", String.Empty); // 2 of the Xml 1.0
  91. #else
  92.         private static readonly string charSingle = @"[\x09 \x0A \x0D \x20-\x7E \x85 \xA0-\uD7FF \uE000-\uFFFD]".Replace(" ", String.Empty); // 2 and 2a of the Xml 1.1
  93. #endif
  94.  
  95.         private static readonly string charPair = pair; // 2
  96.  
  97.         //[2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] (XML 1.0)
  98.         //[2] Char ::= [#x1-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] (XML 1.1)
  99.         //[2a] RestrictedChar ::= [#x1-#x8] | [#xB-#xC] | [#xE-#x1F] | [#x7F-#x84] | [#x86-#x9F] (XML 1.1)
  100.         public static readonly string Char = "(  " + charSingle + "  |  " + charPair + "  )";
  101.  
  102.         /* White Space */
  103.  
  104.         //[3] S ::= (#x20 | #x9 | #xD | #xA)+
  105.         public static readonly string Space = @"(  [\x20 \x09 \x0D \x0A]+  )".Replace(" ", String.Empty);
  106.  
  107.         /* Names and Tokens */
  108.  
  109.         private static readonly string nameStartCharSingle = @"[: 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]".Replace(" ", String.Empty); // 4
  110.         private static readonly string nameStartCharPair = pair; // 4
  111.  
  112.         //[4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
  113.         public static readonly string NameStartChar = "(  " + nameStartCharSingle + " | " + nameStartCharPair + "  )";
  114.  
  115.         //[4a] NameChar ::=  NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
  116.         public static readonly string NameChar = NameStartChar.Replace(":", @": \- . 0-9 \xB7 \u0300-\u036F \u203F-\u2040".Replace(" ", String.Empty));
  117.  
  118.         /* Literals */
  119.  
  120.         //[13] PubidChar ::= #x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%]
  121.         public static readonly string PubidChar = @"(  [\x20 \x0D \x0A a-zA-Z0-9 \-'()+,./:=?;!*#@$_%]  )".Replace(" ", String.Empty);
  122.  
  123.         public static readonly string PubidCharLessQuote = PubidChar.Replace("'", String.Empty); // Used by 12
  124.  
  125.         /* Attribute Type */
  126.  
  127.         //[55] StringType ::= 'CDATA'
  128.         public static readonly string StringType = "(  CDATA  )";
  129.  
  130.         //[56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' | 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS'
  131.         public static readonly string TokenizedType = "(  IDREFS|IDREF|ID|ENTITY|ENTITIES|NMTOKENS|NMTOKEN  )";
  132.     }
  133. }
  134.  
  135.  
  136. /* Based on (and with quotes from):
  137.  *
  138.  * Extensible Markup Language (XML) 1.0 (Fifth Edition)
  139.  * W3C Recommendation 26 November 2008
  140.  * http://www.w3.org/TR/2008/REC-xml-20081126/
  141.  * Copyright © 2008 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark and document use rules apply.
  142.  *
  143.  * and
  144.  *
  145.  * Extensible Markup Language (XML) 1.1 (Second Edition)
  146.  * W3C Recommendation 16 August 2006, edited in place 29 September 2006
  147.  * http://www.w3.org/TR/2006/REC-xml11-20060816
  148.  * Copyright © 2006 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark and document use rules apply.
  149.  */
  150.  
  151. namespace XmlParser
  152. {
  153.     public static class XmlTypes
  154.     {
  155.         private static readonly string TaggedSpace = @"(?<SPACE>  " + XmlChars.Space + @"  )";
  156.         private static readonly string OptionalTaggedSpace = TaggedSpace + @"?";
  157.  
  158.         /* Names and Tokens */
  159.  
  160.         public static readonly string UntaggedName = @"(  " + XmlChars.NameStartChar + XmlChars.NameChar + @"*  )";
  161.  
  162.         //[5] Name ::= NameStartChar (NameChar)*
  163.         public static readonly string Name = TaggedName("NAME");
  164.  
  165.         //[6] Names ::= Name (#x20 Name)*
  166.         //public static readonly string Names =
  167.         //    @"(?<NAMES>" + Name +
  168.         //    @"  (" +
  169.         //    @"    (?<SPACE>  \x20+  )" + Name +
  170.         //    @"  )*" +
  171.         //    @")"; // Added + after \x20
  172.  
  173.         public static readonly string UntaggedNmToken = @"(  " + XmlChars.NameChar + @"+  )";
  174.  
  175.         //[7] Nmtoken ::= (NameChar)+
  176.         public static readonly string NmToken = TaggedNmToken("NMTOKEN");
  177.  
  178.         //[8] Nmtokens ::= Nmtoken (#x20 Nmtoken)*
  179.         //public static readonly string NmTokens =
  180.         //    @"(?<NMTOKENS>" + NmToken +
  181.         //    @"  (" +
  182.         //    @"    (?<SPACE>  \x20+  )" + NmToken +
  183.         //    @"  )*" +
  184.         //    @")"; // Added + after \x20
  185.  
  186.         /* Character Reference */
  187.  
  188.         /// <summary>
  189.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  190.         /// </summary>
  191.         //[66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';
  192.         public static readonly string CharRef =
  193.             @"(" +
  194.             @"  (?<CHARREFOPEN>  &  \#  )" +
  195.             @"  (  (?<CHARREFVALUE>  [0-9]+  |  x  [0-9A-Fa-f]+  )  |  (?<ERROR>)  )" +
  196.             @"  (?(ERROR)|  (  (?<CHARREFCLOSE>  ;  )  |  (?<ERROR>)  )  )" +
  197.             @")";
  198.  
  199.         /* Entity Reference */
  200.  
  201.         /// <summary>
  202.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  203.         /// </summary>
  204.         //[68] EntityRef ::= '&' Name ';'
  205.         public static readonly string EntityRef =
  206.             @"(" +
  207.             @"  (?<ENTITYREFOPEN>  &  )" +
  208.             @"  (  " + TaggedName("ENTITYREFNAME") + @"  |  (?<ERROR>)  )" +
  209.             @"  (?(ERROR)|  (  (?<ENTITYREFCLOSE>  ;  )  |  (?<ERROR>)  )  )" +
  210.             @")";
  211.  
  212.         /// <summary>
  213.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  214.         /// </summary>
  215.         //[67] Reference ::= EntityRef | CharRef
  216.         public static readonly string Reference = @"(  " + CharRef + @" | " + EntityRef + @"  )";
  217.  
  218.         /// <summary>
  219.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  220.         /// </summary>
  221.         //[69] PEReference ::= '%' Name ';'
  222.         public static readonly string PEReference =
  223.             @"(" +
  224.             @"  (?<PEREFOPEN>  %  )" +
  225.             @"  (  " + TaggedName("PEREFNAME") + @"  |  (?<ERROR>)  )" +
  226.             @"  (?(ERROR)|  (  (?<PEREFCLOSE>  ;  )  |  (?<ERROR>)  )  )" +
  227.             @")";
  228.  
  229.         /* Literals */
  230.  
  231.         private static readonly string charLessPercentAmpQuote = XmlChars.Char.Replace("]  |  [", "-[%&']]  |  [");
  232.  
  233.         /// <summary>
  234.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  235.         /// </summary>
  236.         private static readonly string entityValueSingleQuotes =
  237.             @"(" +
  238.             @"  (?<ENTITYVALUEQUOTEOPEN>  '  )" +
  239.             @"  (?(ERROR)|  " +
  240.             @"    (  (?<TEXT>  " + charLessPercentAmpQuote + @"+  )  |  " + PEReference + @"  |  " + Reference + @"  )" +
  241.             @"  )*" +
  242.             @"  (?(ERROR)|  (  (?<ENTITYVALUEQUOTECLOSE>  '  )  |  (?<ERROR>)  )  )" +
  243.             @")";
  244.  
  245.         /// <summary>
  246.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  247.         /// </summary>
  248.         private static readonly string entityValueDoubleQuotes = entityValueSingleQuotes.Replace("'", "\"");
  249.  
  250.         /// <summary>
  251.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  252.         /// </summary>
  253.         //[9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"'  |  "'" ([^%&'] | PEReference | Reference)* "'"
  254.         public static readonly string EntityValue = @"(  " + entityValueSingleQuotes + @"  |  " + entityValueDoubleQuotes + @"  )";
  255.  
  256.         private static readonly string charLessLtAmpQuote = XmlChars.Char.Replace("]  |  [", "-[<&']]  |  [");
  257.  
  258.         /// <summary>
  259.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  260.         /// </summary>
  261.         private static readonly string attValueSingleQuotes =
  262.             @"(" +
  263.             @"  (?<ATTRVALUEQUOTEOPEN>  '  )" +
  264.             @"  (?(ERROR)|  " +
  265.             @"    (  (?<TEXT>  " + charLessLtAmpQuote + @"+  )  |  " + Reference + @"  )" +
  266.             @"  )*" +
  267.             @"  (?(ERROR)|  (  (?<ATTRVALUEQUOTECLOSE>  '  )  |  (?<ERROR>)  )  )" +
  268.             @")";
  269.  
  270.         /// <summary>
  271.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  272.         /// </summary>
  273.         private static readonly string attValueDoubleQuotes = attValueSingleQuotes.Replace("'", "\"");
  274.  
  275.         /// <summary>
  276.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  277.         /// </summary>
  278.         //[10] AttValue ::= '"' ([^<&"] | Reference)* '"'  |  "'" ([^<&'] | Reference)* "'"
  279.         public static readonly string AttValue = @"(  " + attValueSingleQuotes + @"  |  " + attValueDoubleQuotes + @"  )";
  280.  
  281.         private static readonly string charLessQuote = XmlChars.Char.Replace("]  |  [", "-[']]  |  [");
  282.  
  283.         /// <summary>
  284.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  285.         /// </summary>
  286.         private static readonly string systemLiteralSingleQuotes = @"(  (?<SYSTEMLITERALQUOTEOPEN>  '  )  (?<SYSTEMLITERAL>  " + charLessQuote + @"+  )?  (  (?<SYSTEMLITERALQUOTECLOSE>  '  )  |  (?<ERROR>)  )  )";
  287.  
  288.         /// <summary>
  289.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  290.         /// </summary>
  291.         private static readonly string systemLiteralDoubleQuotes = systemLiteralSingleQuotes.Replace("'", "\"");
  292.  
  293.         /// <summary>
  294.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  295.         /// </summary>
  296.         //[11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
  297.         public static readonly string SystemLiteral = @"(  " + systemLiteralSingleQuotes + @"  |  " + systemLiteralDoubleQuotes + @"  )";
  298.  
  299.         /// <summary>
  300.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  301.         /// </summary>
  302.         //[12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
  303.         public static readonly string PubidLiteral =
  304.             @"(" +
  305.             "   (?<PUBIDLITERALQUOTEOPEN>  \"  )  (?<PUBID>" + XmlChars.PubidChar + "+  )?  (  (?<PUBIDLITERALQUOTECLOSE>  \"  )  |  (?<ERROR>)  )" +
  306.             @"  |  " +
  307.             @"  (?<PUBIDLITERALQUOTEOPEN>  '  )  (?<PUBID>" + XmlChars.PubidCharLessQuote + @"+  )?  (  (?<PUBIDLITERALQUOTECLOSE>  \'  )  |  (?<ERROR>)  )" +
  308.             @")";
  309.  
  310.         /* Character Data */
  311.  
  312.         private static readonly string charLessLtAmp = XmlChars.Char.Replace("]  |  [", "-[<&]]  |  [");
  313.  
  314.         //[14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) // (XML 1.0)
  315.         //[14] CharData ::= (Char* - (Char* ']]>' Char*)) // (XML 1.1)
  316.         public static readonly string CharData =
  317.             @"(" +
  318.             @"  (?<CDATA>" +
  319.             @"    (  (?!\]\]>  )  " + charLessLtAmp + @"  )+" +
  320.             @"  )" +
  321.             @")";
  322.  
  323.         /* Comments */
  324.  
  325.         private static readonly string charLessMinus = XmlChars.Char.Replace("]  |  [", "-[-]]  |  [");
  326.  
  327.         /// <summary>
  328.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  329.         /// </summary>
  330.         //[15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
  331.         public static readonly string Comment =
  332.             @"(" +
  333.             @"  (?<COMMENTOPEN>  <!--  )" +
  334.             @"  (?<COMMENT>" +
  335.             @"    (" +
  336.             @"    " + charLessMinus + @"  |  " + @"  -  " + charLessMinus +
  337.             @"    )+" +
  338.             @"  )?" +
  339.             @"  (  (?<COMMENTCLOSE>  -->  )  |  (?<ERROR>)  )" +
  340.             @")";
  341.  
  342.         /* Processing Instructions */
  343.  
  344.         /// <summary>
  345.         /// <remarks>Up to the caller to check for [Xx][Mm][Ll].</remarks>
  346.         /// </summary>
  347.         //[17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
  348.         public static readonly string PITarget = TaggedName("PINAME");
  349.  
  350.         /// <summary>
  351.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  352.         /// </summary>
  353.         //[16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
  354.         public static readonly string PI =
  355.             @"(" +
  356.             @"  (?<PIOPEN>  <\?  )" +
  357.             @"  (?!  [Xx][Mm][Ll]  (  " + XmlChars.Space + @"  |  \?>  )  )" +
  358.             @"  " + PITarget +
  359.             @"  (" +
  360.             @"  " + TaggedSpace +
  361.             @"    (?<OTHER>  (  (?!  \?>  )" + XmlChars.Char + @"  )+  )?" +
  362.             @"  )?" +
  363.             @"  (  (?<PICLOSE>  \?>  )  |  (?<ERROR>)  )" +
  364.             @")";
  365.  
  366.         /* CDATA Sections */
  367.  
  368.         //[19] CDStart ::= '<![CDATA['
  369.         public static readonly string CDStart = @"(?<CDATAOPEN>  <!\[CDATA\[  )";
  370.  
  371.         //[20] CData ::= (Char* - (Char* ']]>' Char*))
  372.         public static readonly string CData =
  373.             @"(?<CDATA>" +
  374.             @"  (" +
  375.             @"    (?!  ]]>  )" + XmlChars.Char +
  376.             @"  )+" +
  377.             @")?";
  378.  
  379.         /// <summary>
  380.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  381.         /// </summary>
  382.         //[21] CDEnd ::= ']]>'
  383.         public static readonly string CDEnd = @"(  (?<CDATACLOSE>  ]]>  )  |  (?<ERROR>)  )";
  384.  
  385.         /// <summary>
  386.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  387.         /// </summary>
  388.         //[18] CDSect ::= CDStart CData CDEnd
  389.         public static readonly string CDSect = @"(  " + CDStart + CData + CDEnd + @"  )";
  390.  
  391.         /* Prolog (part 1) */
  392.  
  393.         /// <summary>
  394.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  395.         /// </summary>
  396.         //[25] Eq ::= S? '=' S?
  397.         public static readonly string Eq =
  398.             @"(  " + OptionalTaggedSpace +
  399.             @"  (" +
  400.             @"    (?<EQUAL>  =  )" + OptionalTaggedSpace + @"  |  (?<ERROR>)" +
  401.             @"  )" +
  402.             @")";
  403.  
  404.         /* Encoding Declaration */
  405.  
  406.         /// <summary>
  407.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  408.         /// </summary>
  409.         //[81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
  410.         public static readonly string EncName = @"(  (?<ENCODING>  [A-Za-z][A-Za-z0-9._\-]*  )  |  (?<ERROR>)  )";
  411.  
  412.         /// <summary>
  413.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  414.         /// </summary>
  415.         private static readonly string encodingDeclSingleQuotes =
  416.             @"(" +
  417.             @"  (?<ENCODINGDECLQUOTEOPEN>  '  )" + EncName +
  418.             @"  (?(ERROR)|  (  (?<ENCODINGDECLQUOTECLOSE>  '  )  |  (?<ERROR>)  )  )" +
  419.             @")";
  420.  
  421.         /// <summary>
  422.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  423.         /// </summary>
  424.         private static readonly string encodingDeclDoubleQuotes = encodingDeclSingleQuotes.Replace("'", "\"");
  425.  
  426.         /// <summary>
  427.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  428.         /// </summary>
  429.         //[80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'" )
  430.         public static readonly string EncodingDecl =
  431.             @"(  " + TaggedSpace +
  432.             @"  (?<XMLDECLATTRNAME>  encoding  )" + Eq +
  433.             @"  (?(ERROR)|  (  " + encodingDeclSingleQuotes + @"  |  " + encodingDeclDoubleQuotes + @"  |  (?<ERROR>)  )  )" +
  434.             @")";
  435.  
  436.         /* Standalone Document Declaration */
  437.  
  438.         /// <summary>
  439.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  440.         /// </summary>
  441.         private static readonly string sdDeclSingleQuotes =
  442.             @"(" +
  443.             @"  (?<SDDECLQUOTEOPEN>  '  )" + @"(  (?<STANDALONE>  yes  |  no  )  |  (?<ERROR>)  )" +
  444.             @"  (?(ERROR)|  (  (?<SDDECLQUOTECLOSE>  '  )  |  (?<ERROR>)  )  )" +
  445.             @")";
  446.  
  447.         /// <summary>
  448.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  449.         /// </summary>
  450.         private static readonly string sdDeclDoubleQuotes = sdDeclSingleQuotes.Replace("'", "\"");
  451.  
  452.         /// <summary>
  453.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  454.         /// </summary>
  455.         //[32] SDDecl ::= S 'standalone' Eq (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no') '"'))
  456.         public static readonly string SDDecl =
  457.             @"(  " + TaggedSpace +
  458.             @"  (?<XMLDECLATTRNAME>  standalone  )" + Eq +
  459.             @"  (?(ERROR)|  (  " + sdDeclSingleQuotes + @"  |  " + sdDeclDoubleQuotes + @"  |  (?<ERROR>)  )  )" +
  460.             @")";
  461.  
  462.         /* Prolog (part 2) */
  463.  
  464.         /// <summary>
  465.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  466.         /// </summary>
  467.         //[26] VersionNum ::= '1.[0-9]+'
  468.         public static readonly string VersionNum = @"(  (?<VERSION>  1\.[0-9]+  )  |  (?<ERROR>)  )";
  469.  
  470.         /// <summary>
  471.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  472.         /// </summary>
  473.         //[27] Misc ::= Comment | PI | S
  474.         public static readonly string Misc = @"(  " + Comment + @"  |  " + PI + @"  |  " + TaggedSpace + @"  )";
  475.  
  476.         /// <summary>
  477.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  478.         /// </summary>
  479.         private static readonly string versionNumSingleQuotes =
  480.             @"(" +
  481.             @"  (?<VERSIONNUMQUOTEOPEN>  '  )" + VersionNum +
  482.             @"  (?(ERROR)|  (  (?<VERSIONNUMQUOTECLOSE>  '  )  |  (?<ERROR>)  )  )" +
  483.             @")";
  484.  
  485.         /// <summary>
  486.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  487.         /// </summary>
  488.         private static readonly string versionNumDoubleQuotes = versionNumSingleQuotes.Replace("'", "\"");
  489.  
  490.         /// <summary>
  491.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  492.         /// </summary>
  493.         //[24] VersionInfo ::= S 'version' Eq ("'" VersionNum "'" | '"' VersionNum '"')
  494.         public static readonly string VersionInfo =
  495.             @"(" + TaggedSpace +
  496.             @"  (?<XMLDECLATTRNAME>  version  )" + Eq +
  497.             @"  (?(ERROR)|  (" + versionNumSingleQuotes + @"  |  " + versionNumDoubleQuotes + @"  |  (?<ERROR>)  )  )" +
  498.             @")";
  499.  
  500.         /// <summary>
  501.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  502.         /// </summary>
  503.         //[23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
  504.         public static readonly string XmlDecl =
  505.             @"(" +
  506.             @"  (?<XMLDECLOPEN>  <\?xml  )" + VersionInfo +
  507.             @"  (?(ERROR)|  " + EncodingDecl + @"?  )" +
  508.             @"  (?(ERROR)|  " + SDDecl + @"?  )" +
  509.             @"  (?(ERROR)|  " + OptionalTaggedSpace +
  510.             @"    (  (?<XMLDECLCLOSE>  \?>  )  |  (?<ERROR>)  )" +
  511.             @"  )" +
  512.             @")";
  513.  
  514.         /* Element-content Models */
  515.  
  516.         private static readonly string cpOpen = @"(?<DTDCPBRACKETOPEN>  \(  (?<_DTDCPDEPTH>)  (?<_DTDCPCURRENTSIGN>)  )";
  517.  
  518.         /// <summary>
  519.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  520.         /// </summary>
  521.         //[48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
  522.         public static readonly string CP =
  523.             @"(" +
  524.             @"  " + cpOpen +
  525.             @"  (?<_DTDSTATENEEDCP>)" +
  526.             @"  (?(ERROR)| (?(_DTDCPDEPTH)" +
  527.             @"    (" +
  528.  
  529.             @"      " + TaggedSpace +
  530.             @"      |  " +
  531.  
  532.             @"      (?(_DTDSTATENEEDCP)" +
  533.             @"        (" +
  534.             @"          " + cpOpen +
  535.             @"          |  " +
  536.  
  537.             @"          (" + TaggedName("DTDELEMENTCHILDNAME") + @"  |  " + PEReference + @"  )" +
  538.             @"          (?(ERROR)|  (?<DTDCONTENTQUANTITY>  [?*+]  )?  (?<-_DTDSTATENEEDCP>)  )" +
  539.             @"          |  " +
  540.  
  541.             @"          (?<ERROR>)" +
  542.             @"        )" +
  543.  
  544.             @"      |  " +
  545.             @"        (" +
  546.             @"          (?<DTDCPBRACKETCLOSE>  \)  (?<-_DTDCPDEPTH>)  (?<-_DTDCPCURRENTSIGN>)  )" +
  547.             @"          (?<DTDCONTENTQUANTITY>  [?*+]  )?" +
  548.             @"          |  " +
  549.  
  550.             @"          (?=  [|,]  )" +
  551.             @"          (" +
  552.             @"            (?<=  \k<_DTDCPCURRENTSIGN>  )  (?<-_DTDCPCURRENTSIGN>)  (?<_DTDCPCURRENTSIGN>(?<DTDCPCONNECTOR>  [|,]  )  )" +
  553.             @"            |  " +
  554.             @"            (?<DTDCPCONNECTOR>  \k<_DTDCPCURRENTSIGN>  )" +
  555.             @"          )" +
  556.             @"          (?<_DTDSTATENEEDCP>)" +
  557.             @"          |  " +
  558.             @"          (?<ERROR>)" +
  559.             @"        )" +
  560.             @"      )" +
  561.             @"    )  )" +
  562.             @"  )*" +
  563.             @"  (?(ERROR)  |  (?(_DTDCPDEPTH)  (?<ERROR>)  )  )" +
  564.             @"  (?<-_DTDSTATENEEDCP>)*  (?<-_DTDCPDEPTH>)*  (?<-_DTDCPCURRENTSIGN>)*" +
  565.             @")";
  566.  
  567.         //[47] children ::= (choice | seq) ('?' | '*' | '+')?
  568.         //public static readonly string Children;
  569.  
  570.         //[49] choice ::= '(' S? cp ( S? '|' S? cp )+ S? ')'
  571.         //public static readonly string Choice;
  572.  
  573.         //[50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
  574.         //public static readonly string Seq;
  575.  
  576.         /* Start-tag (part 1) */
  577.  
  578.         /// <summary>
  579.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  580.         /// </summary>
  581.         //[41] Attribute ::= Name Eq AttValue
  582.         public static readonly string Attribute = @"(  " + TaggedName("ATTRNAME") + Eq + @"(?(ERROR)|  " + AttValue + @"  )  )";
  583.  
  584.         /* Tags for Empty Elements */
  585.  
  586.         //[44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
  587.         //public static readonly string EmptyElemTag = Attribute;
  588.  
  589.         /* Start-tag (part 2) */
  590.  
  591.         /// <summary>
  592.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  593.         /// </summary>
  594.         //[40] STag ::= '<' Name (S Attribute)* S? '>'
  595.         public static readonly string STag =
  596.             @"(" +
  597.             @"  (?<STARTELEMENTTAGOPEN>  (?<STARTEMPTYELEMENTTAGOPEN>  <  )  )" +
  598.             @"  (?<_ELDEPTH>  (?<EMPTYELEMENTNAME>" + TaggedName("ELEMENTNAME") + @"  )  )" +
  599.             @"  (?(ERROR)|  " + TaggedSpace + Attribute + @"  )*" +
  600.             @"  (?(ERROR)|  " + OptionalTaggedSpace +
  601.             @"    (" +
  602.             @"      (?<STARTEMPTYELEMENTTAGCLOSE>  />  (?<-STARTELEMENTTAGOPEN>)  (?<-_ELDEPTH>)  (?<-ELEMENTNAME>)  )" +
  603.             @"      |  " +
  604.             @"      (?<STARTELEMENTTAGCLOSE>  >  (?<-STARTEMPTYELEMENTTAGOPEN>)  (?<-EMPTYELEMENTNAME>)  )" +
  605.             @"      |  " +
  606.             @"      (?<ERROR>)" +
  607.             @"    )" +
  608.             @"  )" +
  609.             @")";
  610.  
  611.         /* End-tag */
  612.  
  613.         /// <summary>
  614.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  615.         /// </summary>
  616.         //[42] ETag ::= '</' Name S? '>'
  617.         //public static readonly string ETag;
  618.  
  619.         /* Content of Elements */
  620.  
  621.         //[43] content ::= CharData? ((element | Reference | CDSect | PI | Comment) CharData?)*
  622.         //public static readonly string Content = Element + Reference + CDSect + "";
  623.  
  624.         /* Element */
  625.  
  626.         /// <summary>
  627.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  628.         /// </summary>
  629.         //[39] element ::= EmptyElemTag | STag content ETag
  630.         public static readonly string Element =
  631.             @"(" +
  632.             @"  (" + STag + @"  |  (?<ERROR>)  )" +
  633.             @"  (?(ERROR)| (?(_ELDEPTH)" +
  634.             @"    (" +
  635.             @"    " + CharData +
  636.             @"      |  " +
  637.             @"    " + Comment +
  638.             @"      |  " +
  639.             @"    " + STag +
  640.             @"      |  " +
  641.             @"    " + Reference +
  642.             @"      |  " +
  643.             @"    " + CDSect +
  644.             @"      |  " +
  645.             @"    " + PI +
  646.             @"      |  " +
  647.             @"      (" +
  648.             @"        (?<ENDELEMENTTAGOPEN>  </  )" +
  649.             @"        (  (?<ENDTAGGEDNAME>\k<_ELDEPTH>  (?<-_ELDEPTH>)  )  |  (?<ERROR>)  )" +
  650.             @"        (?(ERROR)|  " + OptionalTaggedSpace +
  651.             @"          (?<ENDELEMENTTAGCLOSE>  >  )" +
  652.             @"        )" +
  653.             @"      )" +
  654.             @"    )" +
  655.             @"  )  )*" +
  656.             @"  (?(ERROR)|  (?(_ELDEPTH)  (?<ERROR>)  ) )" +
  657.             @"  (?<-_ELDEPTH>)*" +
  658.             @")";
  659.  
  660.         /* Mixed-content Declaration */
  661.  
  662.         /// <summary>
  663.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  664.         /// </summary>
  665.         //[51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' | '(' S? '#PCDATA' S? ')'
  666.         public static readonly string Mixed =
  667.             @"(" +
  668.             @"  (?<DTDMIXEDBRACKETOPEN>  \(  )" + OptionalTaggedSpace + @"(?<DTDCONTENTTYPE>  \#PCDATA  )" +
  669.             @"  (?(ERROR)|  (" + OptionalTaggedSpace +
  670.             @"    (?<DTDCPCONNECTOR>  \|  (?(_DTDCURRENTCPCONNECTOR)|  (?<_DTDCURRENTCPCONNECTOR>)  )  )" + OptionalTaggedSpace + TaggedName("DTDELEMENTCHILDNAME") +
  671.             @"  )  )*" + OptionalTaggedSpace +
  672.             @"  (  (?<DTDMIXEDBRACKETCLOSE>  \)  )  |  (?<ERROR>)  )" +
  673.             @"  (?(ERROR)|  (?(_DTDCURRENTCPCONNECTOR)  (?<-_DTDCURRENTCPCONNECTOR>)  (  (?<DTDCONTENTQUANTITY>  \*  )  |  (?<ERROR>)  )  |  (?<DTDCONTENTQUANTITY>  \*  )?  )  )" +
  674.             @"  (?<-_DTDCURRENTCPCONNECTOR>)*" +
  675.             @")";
  676.  
  677.         /* Element Type Declaration */
  678.  
  679.         /// <summary>
  680.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  681.         /// </summary>
  682.         //[46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children
  683.         public static readonly string ContentSpec =
  684.             @"(" +
  685.             @"  (?<CONTENTTYPE>  EMPTY  |  ANY)" +
  686.             @"  |  " +
  687.             @"" + Mixed +
  688.             @"  |  " +
  689.             @"" + CP +
  690.             @")";
  691.  
  692.         /// <summary>
  693.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  694.         /// </summary>
  695.         //[45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'
  696.         public static readonly string ElementDecl =
  697.             @"(" +
  698.             @"  (?<DTDELEMENTDECLBRACKETOPEN>  <!  )" +
  699.             @"  (?<DTDNAME>  ELEMENT  )" + TaggedSpace +
  700.             @"  (  " + TaggedName("DTDELEMENTNAME") + @"  |  (?<ERROR>)  )" +
  701.             @"  (?(ERROR)|  " + @"(  " + TaggedSpace + @"  |  (?<ERROR>)  )  )" +
  702.             @"  (?(ERROR)|  " + @"(  " + ContentSpec + @"  |  (?<ERROR>)  )  )" +
  703.             @"  (?(ERROR)|  " + OptionalTaggedSpace +
  704.             @"    (  (?<DTDELEMENTDECLBRACKETCLOSE>  >  )  |  (?<ERROR>)  )" +
  705.             @"  )" +
  706.             @")";
  707.  
  708.         /* Enumerated Attribute Types */
  709.  
  710.         /// <summary>
  711.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  712.         /// </summary>
  713.         //[58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'  
  714.         public static readonly string NotationType =
  715.             @"(" +
  716.             @"  (?<DTDATTRTYPE>  NOTATION  )" + TaggedSpace +
  717.             @"  (  (?<DTDNOTATIONATTRTYPEBRACKETOPEN>  \(  )  |  (?<ERROR>)  )" +
  718.             @"  (?(ERROR)|  (" + OptionalTaggedSpace + TaggedName("DTDNOTATIONATTRTYPENAME") + @"  |  (?<ERROR>)  )  )" +
  719.             @"  (?(ERROR)|  (" + OptionalTaggedSpace +
  720.             @"    (?<DTDNOTATIONATTRTYPECONNECTOR>  \|  )" + OptionalTaggedSpace + TaggedName("DTDELEMENTCHILDNAME") +
  721.             @"  )  )*" + OptionalTaggedSpace +
  722.             @"  (?(ERROR)|  (  (?<DTDNOTATIONATTRTYPEBRACKETCLOSE>  \)  )  |  (?<ERROR>)  )  )" +
  723.             @")";
  724.  
  725.         /// <summary>
  726.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  727.         /// </summary>
  728.         //[59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
  729.         public static readonly string Enumeration =
  730.             @"(" +
  731.             @"  (?<DTDENUMERATIONATTRTYPEBRACKETOPEN>  \(  )" +
  732.             @"  (" + OptionalTaggedSpace + TaggedNmToken("DTDENUMERATIONNMTOKEN") + @"  |  (?<ERROR>)  )" +
  733.             @"  (?(ERROR)|  (" + OptionalTaggedSpace +
  734.             @"    (?<DTDENUMERATIONATTRTYPECONNECTOR>  \|  )" + OptionalTaggedSpace + TaggedNmToken("DTDENUMERATIONNMTOKEN") +
  735.             @"  )  )*" + OptionalTaggedSpace +
  736.             @"  (?(ERROR)|  (  (?<DTDENUMERATIONATTRTYPEBRACKETCLOSE>  \)  )  |  (?<ERROR>)  )  )" +
  737.             @")";
  738.  
  739.         /// <summary>
  740.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  741.         /// </summary>
  742.         //[57] EnumeratedType ::= NotationType | Enumeration
  743.         public static readonly string EnumeratedType =
  744.             @"(" +
  745.             @"" + NotationType +
  746.             @"  |  " +
  747.             @"" + Enumeration +
  748.             @")";
  749.  
  750.         /* Attribute Types */
  751.  
  752.         /// <summary>
  753.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  754.         /// </summary>
  755.         //[54] AttType ::= StringType | TokenizedType | EnumeratedType
  756.         public static readonly string AttType =
  757.             @"(" +
  758.             @"  (?<DTDATTRTYPE>  CDATA  )" +
  759.             @"  |  " +
  760.             @"  (?<DTDATTRTYPE>  IDREFS  |  IDREF  |  ID  |  ENTITIES  |  ENTITY  |  NMTOKENS  |  NMTOKEN  )" +
  761.             @"  |  " +
  762.             @"" + EnumeratedType +
  763.             @")";
  764.  
  765.         /* Attribute Defaults */
  766.  
  767.         /// <summary>
  768.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  769.         /// </summary>
  770.         //[60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)
  771.         public static readonly string DefaultDecl =
  772.             @"(" +
  773.             @"  (?<DTDATTRDECLDEFAULT>  \#REQUIRED  |  \#IMPLIED  )" +
  774.             @"  |  " +
  775.             @"  (  (?<DTDATTRDECLDEFAULT>  \#FIXED  )" + TaggedSpace + @"  )?" +
  776.             @"" + AttValue +
  777.             @")";
  778.  
  779.         /* Attribute-list Declaration */
  780.  
  781.         /// <summary>
  782.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  783.         /// </summary>
  784.         //[53] AttDef ::= S Name S AttType S DefaultDecl
  785.         public static readonly string AttDef =
  786.             @"(" +
  787.             @"" + TaggedSpace +
  788.             @"" + TaggedName("DTDATTRLISTNAME") +
  789.             @"  (" + TaggedSpace + @"  |  (?<ERROR>)  )" +
  790.             @"  (?(ERROR)|  (" + AttType + @"  |  (?<ERROR>)  )  )" +
  791.             @"  (?(ERROR)|  (" + TaggedSpace + @"  |  (?<ERROR>)  )  )" +
  792.             @"  (?(ERROR)|  (" + DefaultDecl + @"  |  (?<ERROR>)  )  )" +
  793.             @")";
  794.  
  795.         /// <summary>
  796.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  797.         /// </summary>
  798.         //[52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
  799.         public static readonly string AttlistDecl =
  800.             @"(" +
  801.             @"  (?<DTDATTRLISTDECLBRACKETOPEN>  <!  )" +
  802.             @"  (?<DTDNAME>  ATTLIST  )" + TaggedSpace +
  803.             @"  (  " + TaggedName("DTDATTLISTNAME") + @"  |  (?<ERROR>)  )" +
  804.             @"  (?(ERROR)|  " + AttDef + @"  )*" +
  805.             @"  (?(ERROR)|  " + OptionalTaggedSpace +
  806.             @"    (  (?<DTDATTRLISTDECLBRACKETCLOSE>  >  )  |  (?<ERROR>)  )" +
  807.             @"  )" +
  808.             @")";
  809.  
  810.         /* External Entity Declaration */
  811.  
  812.         /// <summary>
  813.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  814.         /// </summary>
  815.         //[75] ExternalID ::= 'SYSTEM' S SystemLiteral | 'PUBLIC' S PubidLiteral S SystemLiteral
  816.         public static readonly string ExternalID =
  817.             @"(" +
  818.             @"  (" +
  819.             @"    (?<DTDIDTYPE>  SYSTEM  )" + TaggedSpace +
  820.             @"    (" + SystemLiteral + @"  |  (?<ERROR>)  )" +
  821.             @"  )" +
  822.             @"  |  " +
  823.             @"  (" +
  824.             @"    (?<DTDIDTYPE>  PUBLIC  )" + TaggedSpace +
  825.             @"    (" + PubidLiteral + @"  |  (?<ERROR>)  )" +
  826.             @"    (?(ERROR)|  (  " + TaggedSpace + @"  |  (?<ERROR>)  )  )" +
  827.             @"    (?(ERROR)|  (  " + SystemLiteral + @"  |  (?<ERROR>)  )  )" +
  828.             @"  )" +
  829.             @")";
  830.  
  831.         /// <summary>
  832.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  833.         /// </summary>
  834.         //[76] NDataDecl ::= S 'NDATA' S Name
  835.         public static readonly string NDataDecl =
  836.             @"(" +
  837.             @"" + TaggedSpace +
  838.             @"  (?<DTDNDATA>  NDATA  )" +
  839.             @"" + TaggedSpace +
  840.             @"  (  " + TaggedName("DTDNDATANAME") + @"  |  (?<ERROR>)  )" +
  841.             @")";
  842.  
  843.         /* Entity Declaration */
  844.  
  845.         /// <summary>
  846.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  847.         /// </summary>
  848.         //[73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
  849.         public static readonly string EntityDef =
  850.             @"(" +
  851.             @"" + EntityValue +
  852.             @"  |  " +
  853.             @"  (" +
  854.             @"  " + ExternalID +
  855.             @"    (?(ERROR)|  " + NDataDecl + @"?  )" +
  856.             @"  )" +
  857.             @")";
  858.  
  859.         /// <summary>
  860.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  861.         /// </summary>
  862.         //[71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
  863.         // Moved the "'<!ENTITY' S" and the "S? '>'" to EntityDecl
  864.         public static readonly string GEDecl =
  865.             @"(" +
  866.             @"" + TaggedName("DTDENTITYNAME") +
  867.             @"  (?(ERROR)|  " + @"(  " + TaggedSpace + @"  |  (?<ERROR>)  )  )" +
  868.             @"  (?(ERROR)|  " + @"(  " + EntityDef + @"  |  (?<ERROR>)  )  )" +
  869.             @")";
  870.  
  871.         /// <summary>
  872.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  873.         /// </summary>
  874.         //[74] PEDef ::= EntityValue | ExternalID
  875.         public static readonly string PEDef =
  876.             @"(" +
  877.             @"" + EntityValue +
  878.             @"  |  " +
  879.             @"" + ExternalID +
  880.             @")";
  881.  
  882.         /// <summary>
  883.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  884.         /// </summary>
  885.         //[72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
  886.         // Moved the "'<!ENTITY' S" and the "S? '>'" to EntityDecl
  887.         public static readonly string PEDecl =
  888.             @"(" +
  889.             @"  (?<DTDENTITYTYPE>  %  )" + TaggedSpace +
  890.             @"  (  " + TaggedName("DTDENTITYNAME") + @"  |  (?<ERROR>)  )" +
  891.             @"  (?(ERROR)|  " + @"(  " + TaggedSpace + @"  |  (?<ERROR>)  )  )" +
  892.             @"  (?(ERROR)|  " + @"(  " + PEDef + @"  |  (?<ERROR>)  )  )" +
  893.             @")";
  894.  
  895.         /// <summary>
  896.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  897.         /// </summary>
  898.         //[70] EntityDecl ::= GEDecl | PEDecl
  899.         public static readonly string EntityDecl =
  900.             @"(" +
  901.             @"  (?<DTDENTITYBRACKETOPEN>  <!  )" +
  902.             @"  (?<DTDNAME>  ENTITY  )" + TaggedSpace +
  903.             @"  (" + GEDecl + @"  |  " + PEDecl + @"  |  (?<ERROR>)  )" +
  904.             @"  (?(ERROR)|  " + OptionalTaggedSpace +
  905.             @"    (  (?<DTDENTITYBRACKETCLOSE>  >  )  |  (?<ERROR>)  )" +
  906.             @"  )" +
  907.             @")";
  908.  
  909.         /* Text Declaration */
  910.  
  911.         //[77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>'
  912.         //public static readonly string TextDecl;
  913.  
  914.         /* Notation Declarations */
  915.  
  916.         //[83] PublicID ::= 'PUBLIC' S PubidLiteral
  917.         public static readonly string PublicID =
  918.             @"(" +
  919.             @"  (?<DTDIDTYPE>  PUBLIC  )" + TaggedSpace +
  920.             @"  (" + PubidLiteral + @"  |  (?<ERROR>)  )" +
  921.             @")";
  922.  
  923.         /// <summary>
  924.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  925.         /// </summary>
  926.         //[82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>'
  927.         public static readonly string NotationDecl =
  928.             @"(" +
  929.             @"  (?<DTDNOTATIONBRACKETOPEN>  <!  )" +
  930.             @"  (?<DTDNAME>  NOTATION  )" + TaggedSpace +
  931.             @"  (  " + TaggedName("DTDNOTATIONNAME") + @"  |  (?<ERROR>)  )" +
  932.             @"  (?(ERROR)|  (  " + TaggedSpace + @"  |  (?<ERROR>)  )  )" +
  933.             @"  (?(ERROR)|  (" +
  934.             @"  " + PublicID + @"  (?=  " + XmlChars.Space + @"?  >  )" +
  935.             @"    |  " +
  936.             @"  " + ExternalID +
  937.             @"    |  " +
  938.             @"    (?<ERROR>)" +
  939.             @"  )  )" +
  940.             @"  (?(ERROR)|  " + OptionalTaggedSpace +
  941.             @"    (  (?<DTDNOTATIONBRACKETCLOSE>  >  )  |  (?<ERROR>)  )" +
  942.             @"  )" +
  943.             @")";
  944.  
  945.         /* Document Type Definition */
  946.  
  947.         /// <summary>
  948.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  949.         /// </summary>
  950.         //[28a] DeclSep ::= PEReference | S
  951.         public static readonly string DeclSep = @"(  " + PEReference + @"  |  " + TaggedSpace + @"  )";
  952.  
  953.         /// <summary>
  954.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  955.         /// </summary>
  956.         //[29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl | NotationDecl | PI | Comment 
  957.         public static readonly string MarkupDecl =
  958.             @"(" +
  959.             @"" + ElementDecl +
  960.             @"  |  " +
  961.             @"" + AttlistDecl +
  962.             @"  |  " +
  963.             @"" + EntityDecl +
  964.             @"  |  " +
  965.             @"" + NotationDecl +
  966.             @"  |  " +
  967.             @"" + PI +
  968.             @"  |  " +
  969.             @"" + Comment +
  970.             @")";
  971.  
  972.         /// <summary>
  973.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  974.         /// </summary>
  975.         //[28b]intSubset ::= (markupdecl | DeclSep)*
  976.         public static readonly string IntSubset =
  977.             @"(?(ERROR)|  (" +
  978.             @"" + MarkupDecl +
  979.             @"  |  " +
  980.             @"" + DeclSep +
  981.             @")  )*";
  982.  
  983.         /// <summary>
  984.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  985.         /// </summary>
  986.         //[28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? ('[' intSubset ']' S?)? '>'
  987.         public static readonly string DocTypeDecl =
  988.             @"(" +
  989.             @"  (?<DTDDOCTYPEBRACKETOPEN>  <!  )" +
  990.             @"  (?<DTDNAME>  DOCTYPE  )" + TaggedSpace +
  991.             @"  (  " + TaggedName("DTDDOCTYPENAME") + @"  |  (?<ERROR>)  )" +
  992.             @"  (?(ERROR)|  (  " +
  993.             @"  " + TaggedSpace +
  994.             @"  " + ExternalID +
  995.             @"  )  )?" +
  996.             @"  (?(ERROR)|  " + OptionalTaggedSpace +
  997.             @"    (" +
  998.             @"      (?<DTDINTSUBSETBRACKETOPEN>  \[  )" +
  999.             @"    " + IntSubset +
  1000.             @"      (?(ERROR)|  (  (?<DTDINTSUBSETBRACKETCLOSE>  \]  )  |  (?<ERROR>)  )  )" +
  1001.             @"      (?(ERROR)|  " + OptionalTaggedSpace + @"  )" +
  1002.             @"    )?" +
  1003.             @"  )" +
  1004.             @"  (?(ERROR)|  " + @"  (  (?<DTDDOCTYPEBRACKETCLOSE>  >  )  |  (?<ERROR>)  )  )" +
  1005.             @")";
  1006.  
  1007.         /* Prolog (part 3) */
  1008.  
  1009.         /// <summary>
  1010.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  1011.         /// </summary>
  1012.         //[22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?
  1013.         public static readonly string Prolog =
  1014.             @"(" + XmlDecl + @"?" +
  1015.             @"  (?(ERROR)|  " + Misc + @"  )*" +
  1016.             @"  (?(ERROR)|  " + DocTypeDecl +
  1017.             @"    (?(ERROR)|  " + Misc + @"  )*" +
  1018.             @"  )?" +
  1019.             @")";
  1020.  
  1021.         /* Document */
  1022.  
  1023.         /// <summary>
  1024.         /// <remarks>Can set &lt;ERROR&gt;.</remarks>
  1025.         /// </summary>
  1026.         //[1] document ::= prolog element Misc* (XML 1.0)
  1027.         //[1] document ::= ( prolog element Misc* ) - ( Char* RestrictedChar Char* )  (XML 1.1)
  1028.         public static readonly string Document =
  1029.             @"(?>" +
  1030.             @"  \A" +
  1031.             @"  (" +
  1032.             @"    " + Prolog +
  1033.             @"    (?(ERROR)|  " +
  1034.             @"      (" + Element + @"  |  (?<ERROR>)  )" +
  1035.             @"    )" +
  1036.             @"    (?(ERROR)|  " + Misc + @"*  )" +
  1037.             @"  )" +
  1038.             @"  (  \z  |  (?(ERROR)|  (?<ERROR>)  )  )" +
  1039.             @")";
  1040.  
  1041.         /* External Subset */
  1042.  
  1043.         //[31] extSubsetDecl ::= ( markupdecl | conditionalSect | DeclSep)*
  1044.         //public static readonly string ExtSubsetDecl = MarkupDecl + ConditionalSect + DeclSep + "";
  1045.  
  1046.         //[30] extSubset ::= TextDecl? extSubsetDecl
  1047.         //public static readonly string ExtSubset = TextDecl + ExtSubsetDecl + "";
  1048.  
  1049.         /* Conditional Section */
  1050.  
  1051.         //[62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>'
  1052.         //public static readonly string IncludeSect = ExtSubsetDecl + "";
  1053.  
  1054.         //[65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*)
  1055.         //public static readonly string Ignore;
  1056.  
  1057.         //[64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)*
  1058.         //public static readonly string IgnoreSectContents = Ignore + "";
  1059.  
  1060.         //[63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>'
  1061.         //public static readonly string IgnoreSect = IgnoreSectContents + "";
  1062.  
  1063.         //[61] conditionalSect ::= includeSect | ignoreSect
  1064.         //public static readonly string ConditionalSect = IncludeSect + IgnoreSect + "";
  1065.  
  1066.         /* Well-Formed External Parsed Entity */
  1067.  
  1068.         //[78] extParsedEnt ::= ( TextDecl? content ) - ( Char* RestrictedChar Char* )
  1069.         //public static readonly string ExtParsedEnt = TextDecl + Content + "";
  1070.  
  1071.         public static string TaggedName(string name)
  1072.         {
  1073.             return @"(?<" + name + @">" + UntaggedName + @"  )";
  1074.         }
  1075.  
  1076.         public static string TaggedNmToken(string name)
  1077.         {
  1078.             return @"(?<" + name + @">" + UntaggedNmToken + @"  )";
  1079.         }
  1080.     }
  1081. }
  1082.  
  1083.  
  1084. namespace XmlParser
  1085. {
  1086.     using System.Text.RegularExpressions;
  1087.  
  1088.     public class NamedCapture
  1089.     {
  1090.         public readonly string Name;
  1091.         public readonly Capture Capture;
  1092.  
  1093.         public NamedCapture(string name, Capture capture)
  1094.         {
  1095.             Name = name;
  1096.             Capture = capture;
  1097.         }
  1098.  
  1099.         public override string ToString()
  1100.         {
  1101.             return Name + ": " + Capture;
  1102.         }
  1103.     }
  1104. }
  1105.  
  1106.  
  1107. namespace XmlParser
  1108. {
  1109.     using System.Text.RegularExpressions;
  1110.  
  1111.     public class NamedGroup
  1112.     {
  1113.         public readonly string Name;
  1114.         public readonly Group Group;
  1115.  
  1116.         public NamedGroup(string name, Group group)
  1117.         {
  1118.             Name = name;
  1119.             Group = group;
  1120.         }
  1121.  
  1122.         public override string ToString()
  1123.         {
  1124.             return Name + ": " + Group.Success;
  1125.         }
  1126.     }
  1127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement