123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- /// 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.Scanners;
- using Spart.Actions;
- using Spart.Parsers.Composite;
- using Spart.Parsers.NonTerminal;
- /// <summary>
- /// Abstract parser class
- /// </summary>
- public abstract class Parser
- {
- /// <summary>
- /// Default constructor
- /// </summary>
- public Parser()
- {}
- /// <summary>
- /// Inner parse method
- /// </summary>
- /// <param name="scan">scanner</param>
- /// <returns>the match</returns>
- public abstract ParserMatch ParseMain(IScanner scan);
- /// <summary>
- /// Outer parse method
- /// </summary>
- /// <param name="scan"></param>
- /// <returns></returns>
- public virtual ParserMatch Parse(IScanner scan)
- {
- ParserMatch m = ParseMain(scan);
- if (m.Success)
- OnAction(m);
- return m;
- }
- /// <summary>
- /// Action event
- /// </summary>
- public event ActionHandler Act;
- /// <summary>
- /// Action caller method
- /// </summary>
- /// <param name="m"></param>
- public virtual void OnAction(ParserMatch m)
- {
- if (Act != null)
- Act(this,new ActionEventArgs(m));
- }
- #region Operators
- /// <summary>
- /// Positive operator
- /// </summary>
- /// <param name="p"></param>
- /// <returns></returns>
- public static RepetitionParser operator + (Parser p)
- {
- return Ops.Positive(p);
- }
- /// <summary>
- /// Optional operator
- /// </summary>
- /// <param name="p"></param>
- /// <returns></returns>
- public static RepetitionParser operator ! (Parser p)
- {
- return Ops.Optional(p);
- }
- /// <summary>
- /// Alternative operator
- /// </summary>
- /// <param name="left"></param>
- /// <param name="right"></param>
- /// <returns></returns>
- public static AlternativeParser operator | (Parser left, Parser right)
- {
- return Ops.Alternative(left, right);
- }
-
- public static AlternativeParser operator | (char left, Parser right)
- {
- return Ops.Alternative(left, right);
- }
-
- public static AlternativeParser operator | (string left, Parser right)
- {
- return Ops.Alternative(left, right);
- }
- public static AlternativeParser operator | (Parser left, char right)
- {
- return Ops.Alternative(left, right);
- }
-
- public static AlternativeParser operator | (Parser left, string right)
- {
- return Ops.Alternative(left, right);
- }
-
- public static SequenceParser operator + (Parser left, Parser right)
- {
- return Ops.Seq(left, right);
- }
- public static SequenceParser operator + (char left, Parser right)
- {
- return Ops.Seq(left, right);
- }
-
- public static SequenceParser operator + (string left, Parser right)
- {
- return Ops.Seq(left, right);
- }
- public static SequenceParser operator + (Parser left, char right)
- {
- return Ops.Seq(left, right);
- }
-
- public static SequenceParser operator + (Parser left, string right)
- {
- return Ops.Seq(left, right);
- }
- /// <summary>
- /// Intersection operator
- /// </summary>
- /// <param name="left"></param>
- /// <param name="right"></param>
- /// <returns></returns>
- public static IntersectionParser operator & (Parser left, Parser right)
- {
- return Ops.Intersection(left, right);
- }
- /// <summary>
- /// Difference operator
- /// </summary>
- /// <param name="left"></param>
- /// <param name="right"></param>
- /// <returns></returns>
- public static DifferenceParser operator - (Parser left, Parser right)
- {
- return Ops.Difference(left, right);
- }
- /// <summary>
- /// List operator
- /// </summary>
- /// <param name="left"></param>
- /// <param name="right"></param>
- /// <returns></returns>
- public static ListParser operator % (Parser left, Parser right)
- {
- return Ops.List(left,right);
- }
- public Parser this[ActionHandler handler]
- {
- get
- {
- Parser clone = Clone();
- clone.Act = handler;
- return clone;
- }
- }
- public virtual Parser Clone()
- {
- return MemberwiseClone() as Parser;
- }
- #endregion
- }
- }
|