Beispiel #1
0
        /// <summary>
        /// Parses the input at the current position
        /// </summary>
        /// <remarks>
        /// Implementors of a Parser should implement <see cref="InnerParse"/> to perform the logic of their parser.
        /// </remarks>
        /// <param name="args">Parsing arguments</param>
        /// <returns>The length of the successfully matched value (can be zero), or -1 if not matched</returns>
        public int Parse(ParseArgs args)
        {
            if (mode == ParseMode.Simple)
            {
                var match = InnerParse(args);
                if (match >= 0)
                {
                    return(match);
                }

                args.SetChildError();
                return(match);
            }
            else if (mode == ParseMode.NamedChildren)
            {
                args.Push();
                var pos   = args.Scanner.Position;
                var match = InnerParse(args);
                if (match < 0)
                {
                    args.PopFailed();
                    if (AddError)
                    {
                        args.AddError(this);
                        return(-1);
                    }
                    args.SetChildError();
                    return(-1);
                }
                if (AddMatch)
                {
                    args.PopMatch(this, pos, match);
                    return(match);
                }
                args.PopSuccess();
                return(match);
            }
            else             // if (mode == ParseMode.NameOrError)
            {
                var pos   = args.Scanner.Position;
                var match = InnerParse(args);
                if (match < 0)
                {
                    if (!AddError)
                    {
                        args.SetChildError();
                    }
                    else
                    {
                        args.AddError(this);
                    }
                }
                else if (AddMatch)
                {
                    args.AddMatch(this, pos, match);
                }
                return(match);
            }
        }
Beispiel #2
0
 protected override int InnerParse(ParseArgs args)
 {
     if (Inner != null)
     {
         return(Inner.Parse(args));
     }
     return(0);
 }
		protected override int InnerParse(ParseArgs args)
		{
			var scanner = args.Scanner;
			int count = 0;
			var pos = scanner.Position;
			int length = 0;

			while (count < Maximum)
			{
				int curPos = scanner.Position;
				int stopMatch;
				if (skipMatches)
				{
					args.Push();
					stopMatch = Inner.Parse(args);
					args.PopFailed();
				}
				else
				{
					stopMatch = Inner.Parse(args);
				}
				if (stopMatch >= 0)
				{
					if (Capture)
						length += stopMatch;
					else if (!Skip)
						scanner.Position = curPos;
					break;
				}

				var ofs = scanner.Advance(1);
				if (ofs >= 0)
				{
					length++;
					count++;
				}
				else
				{
					scanner.Position = curPos;
					break;
				}
			}

			if (count < Minimum)
			{
				scanner.Position = pos;
				return -1;
			}

			return length;
		}
		protected override int InnerParse(ParseArgs args)
		{
			var count = Items.Count;
			if (HasNamedChildren)
			{
				args.Push();
				for (int i = 0; i < count; i++)
				{
					var parser = Items[i];
					if (parser != null)
					{
						var match = parser.Parse(args);
						if (match < 0)
						{
							args.ClearMatches();
						}
						else
						{
							args.PopSuccess();
							return match;
						}
					}
					else
					{
						args.PopFailed();
						return 0;
					}
				}
				args.PopFailed();
			}
			else
			{
				for (int i = 0; i < count; i++)
				{
					var parser = Items[i];
					if (parser != null)
					{
						var match = parser.Parse(args);
						if (match >= 0)
							return match;
					}
					else
					{
						return 0;
					}
				}
			}
			return -1;
		}
Beispiel #5
0
        public GrammarMatch Match(Scanner scanner)
        {
            //scanner.ThrowIfNull("scanner");
            var args = new ParseArgs(this, scanner);

            if (!initialized)
            {
                Initialize();
            }
            Parse(args);
            var root = args.Root;

            if (root.Success && EnableMatchEvents)
            {
                root.TriggerPreMatch();
                root.TriggerMatch();
            }
            return(root);
        }
Beispiel #6
0
        protected override int InnerParse(ParseArgs args)
        {
            if (args.IsRoot)
            {
                var scanner = args.Scanner;
                var pos     = scanner.Position;
                args.Push();
                var             match   = Inner.Parse(args);
                MatchCollection matches = null;
                if (match >= 0 && !AllowPartialMatch && !scanner.IsEof)
                {
                    args.PopFailed();
                    scanner.Position = pos;
                    match            = -1;
                }
                else
                {
                    matches = args.Pop();
                }

                IEnumerable <Parser> errors = null;
                if (args.Errors != null)
                {
                    var errorList = new List <Parser>(args.Errors.Count);
                    for (int i = 0; i < args.Errors.Count; i++)
                    {
                        var error = args.Errors[i];
                        if (!errorList.Contains(error))
                        {
                            errorList.Add(error);
                        }
                    }
                    errors = errorList;
                }

                args.Root = new GrammarMatch(this, scanner, pos, match, matches, args.ErrorIndex, args.ChildErrorIndex, errors);
                return(match);
            }
            return(base.InnerParse(args));
        }
Beispiel #7
0
        protected override int InnerParse(ParseArgs args)
        {
            var scanner = args.Scanner;
            int count = 0;
            var pos = scanner.Position;
            int length = 0;

            // retrieve up to the maximum number
            var sepMatch = 0;
            if (Inner != null)
            {
                while (count < Maximum)
                {
                    int curPos = scanner.Position;
                    if (Until != null && count >= Minimum)
                    {
                        int stopMatch;
                        if (skipUntilMatches)
                        {
                            args.Push();
                            stopMatch = Until.Parse(args);
                            args.PopFailed();
                        }
                        else
                        {
                            stopMatch = Until.Parse(args);
                        }
                        if (stopMatch >= 0)
                        {
                            if (CaptureUntil)
                                length += stopMatch;
                            else if (!SkipUntil)
                                scanner.Position = curPos;
                            return length;
                        }
                    }

                    if (separator != null && count > 0)
                    {
                        sepMatch = separator.Parse(args);
                        if (sepMatch < 0)
                            break;
                    }

                    var childMatch = Inner.Parse(args);
                    if (childMatch > 0)
                    {
                        length += childMatch + sepMatch;
                        count++;
                    }
                    else
                    {
                        if (sepMatch > 0)
                            scanner.Position = curPos;
                        break;
                    }

                }
            }
            else
            {
                while (count < Maximum)
                {
                    int curPos = scanner.Position;
                    if (Until != null && count >= Minimum)
                    {
                        int stopMatch;
                        if (skipUntilMatches)
                        {
                            args.Push();
                            stopMatch = Until.Parse(args);
                            args.PopFailed();
                        }
                        else
                        {
                            stopMatch = Until.Parse(args);
                        }
                        if (stopMatch >= 0)
                        {
                            if (CaptureUntil)
                                length += stopMatch;
                            else if (!SkipUntil)
                                scanner.Position = curPos;
                            return length;
                        }
                    }

                    if (separator != null && count > 0)
                    {
                        sepMatch = separator.Parse(args);
                        if (sepMatch < 0)
                            break;
                    }

                    var ofs = scanner.Advance(1);
                    if (ofs >= 0)
                    {
                        length += sepMatch;
                        length++;
                        count++;
                    }
                    else
                    {
                        if (sepMatch > 0)
                            scanner.Position = curPos;
                        break;
                    }
                }
            }

            if (count < Minimum)
            {
                scanner.Position = pos;
                return -1;
            }

            return length;
        }
		protected override int InnerParse(ParseArgs args)
		{
			return base.InnerParse(args);
		}
Beispiel #9
0
 /// <summary>
 /// Override to implement the main parsing logic for this parser
 /// </summary>
 /// <remarks>
 /// Never call this method directly, always call <see cref="Parse"/> when calling parse routines.
 /// </remarks>
 /// <returns>The length of the successfully matched value (can be zero), or -1 if not matched</returns>
 /// <param name="args">Parsing arguments</param>
 protected abstract int InnerParse(ParseArgs args);
Beispiel #10
0
		public GrammarMatch Match(Scanner scanner)
		{
			//scanner.ThrowIfNull("scanner");
			var args = new ParseArgs(this, scanner);

			if (!initialized)
				Initialize();
			Parse(args);
			var root = args.Root;

			if (root.Success && EnableMatchEvents)
			{
				root.TriggerPreMatch();
				root.TriggerMatch();
			}
			return root;
		}
Beispiel #11
0
		protected override int InnerParse(ParseArgs args)
		{
			if (args.IsRoot)
			{
				var scanner = args.Scanner;
				var pos = scanner.Position;
				args.Push();
				var match = Inner.Parse(args);
				var matches = args.Pop();
				if (match >= 0 && !AllowPartialMatch && !scanner.IsEof)
				{
					scanner.Position = pos;
					match = -1;
				}

				var errorIndex = -1;
				var childErrorIndex = -1;
				IEnumerable<Parser> errors = null;
				if (match < 0 || match == args.ErrorIndex)
				{
					var errorList = new List<Parser>(args.Errors.Count);
					for (int i = 0; i < args.Errors.Count; i++)
					{
						var error = args.Errors[i];
						if (!errorList.Contains(error))
							errorList.Add(error);
					}
					errors = errorList;
					errorIndex = args.ErrorIndex;
					childErrorIndex = args.ChildErrorIndex;
				}

				args.Root = new GrammarMatch(this, scanner, pos, match, matches, errorIndex, childErrorIndex, errors);
				return match;
			}
			return base.InnerParse(args);
		}
		protected override int InnerParse(ParseArgs args)
		{
			return !args.Scanner.ReadString(Value, caseSensitive) ? -1 : Value.Length;
		}
Beispiel #13
0
 protected override int InnerParse(ParseArgs args)
 {
     if (Inner != null)
         return Inner.Parse(args);
     return 0;
 }
Beispiel #14
0
		protected override int InnerParse(ParseArgs args)
		{
			return !args.Scanner.IsEof ? -1 : 0;
		}
Beispiel #15
0
        /// <summary>
        /// Parses the input at the current position
        /// </summary>
        /// <remarks>
        /// Implementors of a Parser should implement <see cref="InnerParse"/> to perform the logic of their parser.
        /// </remarks>
        /// <param name="args">Parsing arguments</param>
        /// <param name="noError">if true, no errors are set</param>
        /// <returns>The length of the successfully matched value (can be zero), or -1 if not matched</returns>
        public int Parse(ParseArgs args, bool noErrors = false)
        {
            try
            {
                args.Begin(this);

                if (mode == ParseMode.Simple)
                {
                    var match = InnerParse(args);
                    if (match >= 0)
                    {
                        return(match);
                    }

                    if (!noErrors)
                    {
                        args.SetChildError();
                    }
                    return(match);
                }
                else if (mode == ParseMode.NamedChildren)
                {
                    args.Push();
                    var pos   = args.Scanner.Position;
                    var match = InnerParse(args);
                    if (match < 0)
                    {
                        args.PopFailed();
                        if (!noErrors)
                        {
                            if (AddError)
                            {
                                args.AddError(this);
                                return(-1);
                            }
                            args.SetChildError();
                        }
                        return(-1);
                    }
                    if (AddMatch)
                    {
                        args.PopMatch(this, pos, match);
                        return(match);
                    }
                    args.PopSuccess();
                    return(match);
                }
                else                 // if (mode == ParseMode.NameOrError)
                {
                    var pos   = args.Scanner.Position;
                    var match = InnerParse(args);
                    if (match < 0)
                    {
                        if (!noErrors)
                        {
                            if (!AddError)
                            {
                                args.SetChildError();
                            }
                            else
                            {
                                args.AddError(this);
                            }
                        }
                    }
                    else if (AddMatch)
                    {
                        args.AddMatch(this, pos, match);
                    }
                    return(match);
                }
            } finally
            {                   // end this parser
                args.End();
            }
        }
		protected override int InnerParse(ParseArgs args)
		{
			var pos = args.Scanner.Position;
			return pos <= 0 ? 0 : -1;
		}