Ops.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.Parsers.Composite;
  29. /// <summary>
  30. /// Static helper class to create new parser operators
  31. /// </summary>
  32. public class Ops
  33. {
  34. /// <summary>
  35. /// &gt;&gt; operator
  36. /// </summary>
  37. /// <param name="first"></param>
  38. /// <param name="second"></param>
  39. /// <returns></returns>
  40. public static SequenceParser Seq(Parser first, Parser second)
  41. {
  42. if (first == null)
  43. throw new ArgumentNullException("first parser is null");
  44. if (second== null)
  45. throw new ArgumentNullException("second parser is null");
  46. return new SequenceParser(first,second);
  47. }
  48. public static SequenceParser Seq(char first, Parser second)
  49. {
  50. return Seq(Prims.Ch(first),second);
  51. }
  52. public static SequenceParser Seq(Parser first, char second)
  53. {
  54. return Seq(first,Prims.Ch(second));
  55. }
  56. public static SequenceParser Seq(String first, Parser second)
  57. {
  58. return Seq(Prims.Str(first),second);
  59. }
  60. public static SequenceParser Seq(Parser first, String second)
  61. {
  62. return Seq(first,Prims.Str(second));
  63. }
  64. /// <summary>
  65. /// * operators
  66. /// </summary>
  67. /// <param name="parser"></param>
  68. /// <returns></returns>
  69. public static RepetitionParser Klenee(Parser parser)
  70. {
  71. if (parser == null)
  72. throw new ArgumentNullException("parser is null");
  73. return new RepetitionParser(parser,0,uint.MaxValue);
  74. }
  75. public static RepetitionParser Klenee(char c)
  76. {
  77. return Klenee(Prims.Ch(c));
  78. }
  79. public static RepetitionParser Klenee(String s)
  80. {
  81. return Klenee(Prims.Str(s));
  82. }
  83. /// <summary>
  84. /// + operator
  85. /// </summary>
  86. /// <param name="parser"></param>
  87. /// <returns></returns>
  88. public static RepetitionParser Positive(Parser parser)
  89. {
  90. if (parser == null)
  91. throw new ArgumentNullException("parser is null");
  92. return new RepetitionParser(parser,1,uint.MaxValue);
  93. }
  94. public static RepetitionParser Positive(char c)
  95. {
  96. return Positive(Prims.Ch(c));
  97. }
  98. public static RepetitionParser Positive(String s)
  99. {
  100. return Positive(Prims.Str(s));
  101. }
  102. /// <summary>
  103. /// ! operator
  104. /// </summary>
  105. /// <param name="parser"></param>
  106. /// <returns></returns>
  107. public static RepetitionParser Optional(Parser parser)
  108. {
  109. if (parser == null)
  110. throw new ArgumentNullException("parser is null");
  111. return new RepetitionParser(parser,0,1);
  112. }
  113. public static RepetitionParser Optional(char c)
  114. {
  115. return Optional(Prims.Ch(c));
  116. }
  117. public static RepetitionParser Optional(String s)
  118. {
  119. return Optional(Prims.Str(s));
  120. }
  121. /// <summary>
  122. /// | operator
  123. /// </summary>
  124. /// <param name="first"></param>
  125. /// <param name="second"></param>
  126. /// <returns></returns>
  127. public static AlternativeParser Alternative(Parser first, Parser second)
  128. {
  129. if (first == null)
  130. throw new ArgumentNullException("first parser is null");
  131. if (second== null)
  132. throw new ArgumentNullException("second parser is null");
  133. return new AlternativeParser(first, second);
  134. }
  135. public static AlternativeParser Alternative(char first, Parser second)
  136. {
  137. return Alternative(Prims.Ch(first),second);
  138. }
  139. public static AlternativeParser Alternative(Parser first, char second)
  140. {
  141. return Alternative(first,Prims.Ch(second));
  142. }
  143. public static AlternativeParser Alternative(String first, Parser second)
  144. {
  145. return Alternative(Prims.Str(first),second);
  146. }
  147. public static AlternativeParser Alternative(Parser first, String second)
  148. {
  149. return Alternative(first,Prims.Str(second));
  150. }
  151. /// <summary>
  152. /// & operator
  153. /// </summary>
  154. /// <param name="first"></param>
  155. /// <param name="second"></param>
  156. /// <returns></returns>
  157. public static IntersectionParser Intersection(Parser first, Parser second)
  158. {
  159. if (first == null)
  160. throw new ArgumentNullException("first parser is null");
  161. if (second== null)
  162. throw new ArgumentNullException("second parser is null");
  163. return new IntersectionParser(first, second);
  164. }
  165. public static IntersectionParser Intersection(char first, Parser second)
  166. {
  167. return Intersection(Prims.Ch(first),second);
  168. }
  169. public static IntersectionParser Intersection(Parser first, char second)
  170. {
  171. return Intersection(first,Prims.Ch(second));
  172. }
  173. public static IntersectionParser Intersection(String first, Parser second)
  174. {
  175. return Intersection(Prims.Str(first),second);
  176. }
  177. public static IntersectionParser Intersection(Parser first, String second)
  178. {
  179. return Intersection(first,Prims.Str(second));
  180. }
  181. /// <summary>
  182. /// - operator
  183. /// </summary>
  184. /// <param name="first"></param>
  185. /// <param name="second"></param>
  186. /// <returns></returns>
  187. public static DifferenceParser Difference(Parser first, Parser second)
  188. {
  189. if (first == null)
  190. throw new ArgumentNullException("first parser is null");
  191. if (second== null)
  192. throw new ArgumentNullException("second parser is null");
  193. return new DifferenceParser(first, second);
  194. }
  195. public static DifferenceParser Difference(char first, Parser second)
  196. {
  197. return Difference(Prims.Ch(first),second);
  198. }
  199. public static DifferenceParser Difference(Parser first, char second)
  200. {
  201. return Difference(first,Prims.Ch(second));
  202. }
  203. public static DifferenceParser Difference(String first, Parser second)
  204. {
  205. return Difference(Prims.Str(first),second);
  206. }
  207. public static DifferenceParser Difference(Parser first, String second)
  208. {
  209. return Difference(first,Prims.Str(second));
  210. }
  211. /// <summary>
  212. /// % operator
  213. /// </summary>
  214. /// <param name="first"></param>
  215. /// <param name="second"></param>
  216. /// <returns></returns>
  217. public static ListParser List(Parser first, Parser second)
  218. {
  219. if (first == null)
  220. throw new ArgumentNullException("first parser is null");
  221. if (second== null)
  222. throw new ArgumentNullException("second parser is null");
  223. return new ListParser(first, second);
  224. }
  225. public static ListParser List(char first, Parser second)
  226. {
  227. return List(Prims.Ch(first),second);
  228. }
  229. public static ListParser List(Parser first, char second)
  230. {
  231. return List(first,Prims.Ch(second));
  232. }
  233. public static ListParser List(String first, Parser second)
  234. {
  235. return List(Prims.Str(first),second);
  236. }
  237. public static ListParser List(Parser first, String second)
  238. {
  239. return List(first,Prims.Str(second));
  240. }
  241. }
  242. }