123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- /// Spart License (zlib/png)
- ///
- ///
- /// Copyright (c) 2003 Jonathan de Halleux
- ///
- /// This software is provided 'as-is', without any express or implied warranty.
- /// In no event will the authors be held liable for any damages arising from
- /// the use of this software.
- ///
- /// Permission is granted to anyone to use this software for any purpose,
- /// including commercial applications, and to alter it and redistribute it
- /// freely, subject to the following restrictions:
- ///
- /// 1. The origin of this software must not be misrepresented; you must not
- /// claim that you wrote the original software. If you use this software in a
- /// product, an acknowledgment in the product documentation would be
- /// appreciated but is not required.
- ///
- /// 2. Altered source versions must be plainly marked as such, and must not be
- /// misrepresented as being the original software.
- ///
- /// 3. This notice may not be removed or altered from any source distribution.
- ///
- /// Author: Jonathan de Halleuxnamespace Spart.Parsers
- namespace Spart.Parsers
- {
- using System;
- using Spart.Parsers.Composite;
- /// <summary>
- /// Static helper class to create new parser operators
- /// </summary>
- public class Ops
- {
- /// <summary>
- /// >> operator
- /// </summary>
- /// <param name="first"></param>
- /// <param name="second"></param>
- /// <returns></returns>
- public static SequenceParser Seq(Parser first, Parser second)
- {
- if (first == null)
- throw new ArgumentNullException("first parser is null");
- if (second== null)
- throw new ArgumentNullException("second parser is null");
- return new SequenceParser(first,second);
- }
- public static SequenceParser Seq(char first, Parser second)
- {
- return Seq(Prims.Ch(first),second);
- }
- public static SequenceParser Seq(Parser first, char second)
- {
- return Seq(first,Prims.Ch(second));
- }
- public static SequenceParser Seq(String first, Parser second)
- {
- return Seq(Prims.Str(first),second);
- }
- public static SequenceParser Seq(Parser first, String second)
- {
- return Seq(first,Prims.Str(second));
- }
- /// <summary>
- /// * operators
- /// </summary>
- /// <param name="parser"></param>
- /// <returns></returns>
- public static RepetitionParser Klenee(Parser parser)
- {
- if (parser == null)
- throw new ArgumentNullException("parser is null");
- return new RepetitionParser(parser,0,uint.MaxValue);
- }
- public static RepetitionParser Klenee(char c)
- {
- return Klenee(Prims.Ch(c));
- }
- public static RepetitionParser Klenee(String s)
- {
- return Klenee(Prims.Str(s));
- }
- /// <summary>
- /// + operator
- /// </summary>
- /// <param name="parser"></param>
- /// <returns></returns>
- public static RepetitionParser Positive(Parser parser)
- {
- if (parser == null)
- throw new ArgumentNullException("parser is null");
- return new RepetitionParser(parser,1,uint.MaxValue);
- }
- public static RepetitionParser Positive(char c)
- {
- return Positive(Prims.Ch(c));
- }
- public static RepetitionParser Positive(String s)
- {
- return Positive(Prims.Str(s));
- }
- /// <summary>
- /// ! operator
- /// </summary>
- /// <param name="parser"></param>
- /// <returns></returns>
- public static RepetitionParser Optional(Parser parser)
- {
- if (parser == null)
- throw new ArgumentNullException("parser is null");
- return new RepetitionParser(parser,0,1);
- }
- public static RepetitionParser Optional(char c)
- {
- return Optional(Prims.Ch(c));
- }
- public static RepetitionParser Optional(String s)
- {
- return Optional(Prims.Str(s));
- }
- /// <summary>
- /// | operator
- /// </summary>
- /// <param name="first"></param>
- /// <param name="second"></param>
- /// <returns></returns>
- public static AlternativeParser Alternative(Parser first, Parser second)
- {
- if (first == null)
- throw new ArgumentNullException("first parser is null");
- if (second== null)
- throw new ArgumentNullException("second parser is null");
- return new AlternativeParser(first, second);
- }
- public static AlternativeParser Alternative(char first, Parser second)
- {
- return Alternative(Prims.Ch(first),second);
- }
- public static AlternativeParser Alternative(Parser first, char second)
- {
- return Alternative(first,Prims.Ch(second));
- }
- public static AlternativeParser Alternative(String first, Parser second)
- {
- return Alternative(Prims.Str(first),second);
- }
- public static AlternativeParser Alternative(Parser first, String second)
- {
- return Alternative(first,Prims.Str(second));
- }
- /// <summary>
- /// & operator
- /// </summary>
- /// <param name="first"></param>
- /// <param name="second"></param>
- /// <returns></returns>
- public static IntersectionParser Intersection(Parser first, Parser second)
- {
- if (first == null)
- throw new ArgumentNullException("first parser is null");
- if (second== null)
- throw new ArgumentNullException("second parser is null");
- return new IntersectionParser(first, second);
- }
- public static IntersectionParser Intersection(char first, Parser second)
- {
- return Intersection(Prims.Ch(first),second);
- }
- public static IntersectionParser Intersection(Parser first, char second)
- {
- return Intersection(first,Prims.Ch(second));
- }
- public static IntersectionParser Intersection(String first, Parser second)
- {
- return Intersection(Prims.Str(first),second);
- }
- public static IntersectionParser Intersection(Parser first, String second)
- {
- return Intersection(first,Prims.Str(second));
- }
- /// <summary>
- /// - operator
- /// </summary>
- /// <param name="first"></param>
- /// <param name="second"></param>
- /// <returns></returns>
- public static DifferenceParser Difference(Parser first, Parser second)
- {
- if (first == null)
- throw new ArgumentNullException("first parser is null");
- if (second== null)
- throw new ArgumentNullException("second parser is null");
- return new DifferenceParser(first, second);
- }
- public static DifferenceParser Difference(char first, Parser second)
- {
- return Difference(Prims.Ch(first),second);
- }
- public static DifferenceParser Difference(Parser first, char second)
- {
- return Difference(first,Prims.Ch(second));
- }
- public static DifferenceParser Difference(String first, Parser second)
- {
- return Difference(Prims.Str(first),second);
- }
- public static DifferenceParser Difference(Parser first, String second)
- {
- return Difference(first,Prims.Str(second));
- }
- /// <summary>
- /// % operator
- /// </summary>
- /// <param name="first"></param>
- /// <param name="second"></param>
- /// <returns></returns>
- public static ListParser List(Parser first, Parser second)
- {
- if (first == null)
- throw new ArgumentNullException("first parser is null");
- if (second== null)
- throw new ArgumentNullException("second parser is null");
- return new ListParser(first, second);
- }
- public static ListParser List(char first, Parser second)
- {
- return List(Prims.Ch(first),second);
- }
- public static ListParser List(Parser first, char second)
- {
- return List(first,Prims.Ch(second));
- }
- public static ListParser List(String first, Parser second)
- {
- return List(Prims.Str(first),second);
- }
- public static ListParser List(Parser first, String second)
- {
- return List(first,Prims.Str(second));
- }
- }
- }
|