Rule.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /// Spart License (zlib/png)
  2. ///
  3. ///
  4. /// Copyright (c) 2003 Jonathan de Halleux
  5. ///
  6. /// This software is provided 'as-is', without any express or implied warranty.
  7. /// In no event will the authors be held liable for any damages arising from
  8. /// the use of this software.
  9. ///
  10. /// Permission is granted to anyone to use this software for any purpose,
  11. /// including commercial applications, and to alter it and redistribute it
  12. /// freely, subject to the following restrictions:
  13. ///
  14. /// 1. The origin of this software must not be misrepresented; you must not
  15. /// claim that you wrote the original software. If you use this software in a
  16. /// product, an acknowledgment in the product documentation would be
  17. /// appreciated but is not required.
  18. ///
  19. /// 2. Altered source versions must be plainly marked as such, and must not be
  20. /// misrepresented as being the original software.
  21. ///
  22. /// 3. This notice may not be removed or altered from any source distribution.
  23. ///
  24. /// Author: Jonathan de Halleuxnamespace Spart.Parsers.NonTerminal
  25. namespace Spart.Parsers.NonTerminal
  26. {
  27. using System;
  28. using Spart.Actions;
  29. using Spart.Scanners;
  30. /// <summary>
  31. /// A rule is a parser holder.
  32. /// </summary>
  33. public class Rule : NonTerminalParser
  34. {
  35. private Parser m_Parser;
  36. /// <summary>
  37. /// Empty rule creator
  38. /// </summary>
  39. public Rule()
  40. : base()
  41. {
  42. Parser = null;
  43. }
  44. /// <summary>
  45. /// Creates a rule and assign parser
  46. /// </summary>
  47. /// <param name="p"></param>
  48. public Rule(Parser p)
  49. : base()
  50. {
  51. Parser = p;
  52. }
  53. /// <summary>
  54. /// Rule parser
  55. /// </summary>
  56. public Parser Parser
  57. {
  58. get
  59. {
  60. return m_Parser;
  61. }
  62. set
  63. {
  64. if( m_Parser != value )
  65. m_Parser = value;
  66. }
  67. }
  68. /// <summary>
  69. /// Assign a parser to a rule, if r is null, a new rule is created
  70. /// </summary>
  71. /// <param name="r"></param>
  72. /// <param name="p"></param>
  73. /// <returns></returns>
  74. public static Rule AssignParser(Rule r, Parser p)
  75. {
  76. if (r==null)
  77. {
  78. r = new Rule(p);
  79. return r;
  80. }
  81. r.Parser = p;
  82. return r;
  83. }
  84. /// <summary>
  85. /// Parse the input
  86. /// </summary>
  87. /// <param name="scanner"></param>
  88. /// <returns></returns>
  89. public override ParserMatch Parse(IScanner scanner)
  90. {
  91. if (scanner == null)
  92. throw new ArgumentNullException("scanner");
  93. OnPreParse(scanner);
  94. ParserMatch match = ParseMain(scanner);
  95. OnPostParse(match,scanner);
  96. if (!match.Success)
  97. return match;
  98. OnAction(match);
  99. return match;
  100. }
  101. /// <summary>
  102. /// Inner parse method
  103. /// </summary>
  104. /// <param name="scanner"></param>
  105. /// <returns></returns>
  106. public override ParserMatch ParseMain(IScanner scanner)
  107. {
  108. if (scanner == null)
  109. throw new ArgumentNullException("scanner");
  110. if (Parser != null)
  111. return Parser.Parse(scanner);
  112. else
  113. return scanner.NoMatch;
  114. }
  115. public override Parser Clone()
  116. {
  117. Rule clone = base.Clone() as Rule;
  118. clone.m_Parser = m_Parser.Clone();
  119. return clone;
  120. }
  121. }
  122. }