Parser.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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
  25. namespace Spart.Parsers
  26. {
  27. using System;
  28. using Spart.Scanners;
  29. using Spart.Actions;
  30. using Spart.Parsers.Composite;
  31. using Spart.Parsers.NonTerminal;
  32. /// <summary>
  33. /// Abstract parser class
  34. /// </summary>
  35. public abstract class Parser
  36. {
  37. /// <summary>
  38. /// Default constructor
  39. /// </summary>
  40. public Parser()
  41. {}
  42. /// <summary>
  43. /// Inner parse method
  44. /// </summary>
  45. /// <param name="scan">scanner</param>
  46. /// <returns>the match</returns>
  47. public abstract ParserMatch ParseMain(IScanner scan);
  48. /// <summary>
  49. /// Outer parse method
  50. /// </summary>
  51. /// <param name="scan"></param>
  52. /// <returns></returns>
  53. public virtual ParserMatch Parse(IScanner scan)
  54. {
  55. ParserMatch m = ParseMain(scan);
  56. if (m.Success)
  57. OnAction(m);
  58. return m;
  59. }
  60. /// <summary>
  61. /// Action event
  62. /// </summary>
  63. public event ActionHandler Act;
  64. /// <summary>
  65. /// Action caller method
  66. /// </summary>
  67. /// <param name="m"></param>
  68. public virtual void OnAction(ParserMatch m)
  69. {
  70. if (Act != null)
  71. Act(this,new ActionEventArgs(m));
  72. }
  73. #region Operators
  74. /// <summary>
  75. /// Positive operator
  76. /// </summary>
  77. /// <param name="p"></param>
  78. /// <returns></returns>
  79. public static RepetitionParser operator + (Parser p)
  80. {
  81. return Ops.Positive(p);
  82. }
  83. /// <summary>
  84. /// Optional operator
  85. /// </summary>
  86. /// <param name="p"></param>
  87. /// <returns></returns>
  88. public static RepetitionParser operator ! (Parser p)
  89. {
  90. return Ops.Optional(p);
  91. }
  92. /// <summary>
  93. /// Alternative operator
  94. /// </summary>
  95. /// <param name="left"></param>
  96. /// <param name="right"></param>
  97. /// <returns></returns>
  98. public static AlternativeParser operator | (Parser left, Parser right)
  99. {
  100. return Ops.Alternative(left, right);
  101. }
  102. public static AlternativeParser operator | (char left, Parser right)
  103. {
  104. return Ops.Alternative(left, right);
  105. }
  106. public static AlternativeParser operator | (string left, Parser right)
  107. {
  108. return Ops.Alternative(left, right);
  109. }
  110. public static AlternativeParser operator | (Parser left, char right)
  111. {
  112. return Ops.Alternative(left, right);
  113. }
  114. public static AlternativeParser operator | (Parser left, string right)
  115. {
  116. return Ops.Alternative(left, right);
  117. }
  118. public static SequenceParser operator + (Parser left, Parser right)
  119. {
  120. return Ops.Seq(left, right);
  121. }
  122. public static SequenceParser operator + (char left, Parser right)
  123. {
  124. return Ops.Seq(left, right);
  125. }
  126. public static SequenceParser operator + (string left, Parser right)
  127. {
  128. return Ops.Seq(left, right);
  129. }
  130. public static SequenceParser operator + (Parser left, char right)
  131. {
  132. return Ops.Seq(left, right);
  133. }
  134. public static SequenceParser operator + (Parser left, string right)
  135. {
  136. return Ops.Seq(left, right);
  137. }
  138. /// <summary>
  139. /// Intersection operator
  140. /// </summary>
  141. /// <param name="left"></param>
  142. /// <param name="right"></param>
  143. /// <returns></returns>
  144. public static IntersectionParser operator & (Parser left, Parser right)
  145. {
  146. return Ops.Intersection(left, right);
  147. }
  148. /// <summary>
  149. /// Difference operator
  150. /// </summary>
  151. /// <param name="left"></param>
  152. /// <param name="right"></param>
  153. /// <returns></returns>
  154. public static DifferenceParser operator - (Parser left, Parser right)
  155. {
  156. return Ops.Difference(left, right);
  157. }
  158. /// <summary>
  159. /// List operator
  160. /// </summary>
  161. /// <param name="left"></param>
  162. /// <param name="right"></param>
  163. /// <returns></returns>
  164. public static ListParser operator % (Parser left, Parser right)
  165. {
  166. return Ops.List(left,right);
  167. }
  168. public Parser this[ActionHandler handler]
  169. {
  170. get
  171. {
  172. Parser clone = Clone();
  173. clone.Act = handler;
  174. return clone;
  175. }
  176. }
  177. public virtual Parser Clone()
  178. {
  179. return MemberwiseClone() as Parser;
  180. }
  181. #endregion
  182. }
  183. }