Beispiel #1
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);
        }
Beispiel #2
0
        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 #3
0
        protected override int InnerParse(ParseArgs args)
        {
            if (args.IsRoot)
            {
                Scanner scanner = args.Scanner;
                int     pos     = scanner.Position;
                args.Push();
                int             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++)
                    {
                        Parser 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 #4
0
        protected override int InnerParse(ParseArgs args)
        {
            if (!HasNamedChildren)
            {
                var match = Inner.Parse(args);

                return(match < 0 ? 0 : match);
            }
            else
            {
                args.Push();
                var match = Inner.Parse(args);

                if (match >= 0)
                {
                    args.PopSuccess();
                    return(match);
                }
                args.PopFailed();
                return(0);
            }
        }
		protected override int InnerParse(ParseArgs args)
		{
			if (!HasNamedChildren)
			{
				var match = Inner.Parse(args);

				return match < 0 ? 0 : match;
			}
			else
			{
				args.Push();
				var match = Inner.Parse(args);

				if (match >= 0)
				{
					args.PopSuccess();
					return match;
				}
				args.PopFailed();
				return 0;
			}
		}
Beispiel #6
0
        protected override int InnerParse(ParseArgs args)
        {
            var scanner = args.Scanner;
            var pos     = scanner.Position;
            int length  = 0;
            int ch;

            ch = scanner.ReadChar();
            length++;
            if (ch == '<')
            {
                ch = scanner.ReadChar();
                length++;
                if (ch == '!')
                {
                    length = 7;
                    // read comment
                    if (scanner.ReadString("--", true))
                    {
                        while (!scanner.ReadString("-->", true))
                        {
                            if (scanner.ReadChar() == -1)
                            {
                                scanner.Position = pos;
                                return(-1);
                            }
                            length++;
                        }
                        return(length);
                    }
                    scanner.Position = pos;
                    return(-1);
                }
                tagNameBuilder.Clear();
                var first = true;
                while (ch != -1 && char.IsLetter((char)ch))
                {
                    if (first && !char.IsLetter((char)ch))
                    {
                        scanner.Position = pos;
                        return(-1);
                    }
                    first = false;
                    tagNameBuilder.Append((char)ch);
                    ch = scanner.ReadChar();
                    length++;
                }
                if (ch == ' ' || ch == '>' || ch == '/')
                {
                    var isVoid = voidTags.Contains(tagNameBuilder.ToString());
                    int prev   = ch;
                    while (ch != '>' && ch != -1)
                    {
                        prev = ch;
                        if (ch == '\'' || ch == '"')
                        {
                            var quote = ch;
                            ch = scanner.ReadChar();
                            length++;
                            while (ch != quote && ch != -1)
                            {
                                if (ch == '\n')
                                {
                                    scanner.Position = pos;
                                    return(-1);
                                }
                                ch = scanner.ReadChar();
                                length++;
                            }
                        }
                        ch = scanner.ReadChar();
                        length++;
                    }
                    if (ch != '>')
                    {
                        scanner.Position = pos;
                        return(-1);
                    }
                    if (isVoid || prev == '/')                     // self closing, or a tag with no content
                    {
                        return(length);
                    }

                    // now, read till the end tag
                    if (MatchContent && Name != null)
                    {
                        args.Push();
                    }
                    var start         = pos + length;
                    var contentLength = 0;
                    tagNameBuilder.Insert(0, "</");
                    tagNameBuilder.Append(">");
                    var tagName = tagNameBuilder.ToString();
                    while (!scanner.ReadString(tagName, true))
                    {
                        ch = scanner.Peek();
                        if (ch == '<')
                        {
                            if (MatchContent && contentLength > 0)
                            {
                                args.AddMatch(this, start, contentLength, "content");
                            }
                            var inner = Parse(args);
                            if (inner < 0)
                            {
                                scanner.Position = pos;
                                return(-1);
                            }
                            length       += inner;
                            start         = pos + length;
                            contentLength = 0;
                            continue;
                        }
                        if (scanner.Advance(1) >= 0)
                        {
                            length++;
                            contentLength++;
                        }
                        else
                        {
                            scanner.Position = pos;
                            return(-1);
                        }
                    }
                    length += tagName.Length;
                    if (MatchContent)
                    {
                        if (contentLength > 0)
                        {
                            args.AddMatch(this, start, contentLength, "content");
                        }
                        if (Name != null)
                        {
                            args.PopMatch(this, pos, length);
                        }
                    }
                    return(length);
                }
            }
            scanner.Position = pos;
            return(-1);
        }
        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)
		{
			var scanner = args.Scanner;
			var pos = scanner.Position;
			int length = 0;
			int ch;

			ch = scanner.ReadChar();
			length++;
			if (ch == '<')
			{
				ch = scanner.ReadChar();
				length++;
				if (ch == '!')
				{
					length = 7;
					// read comment
					if (scanner.ReadString("--", true))
					{
						while (!scanner.ReadString("-->", true))
						{
							if (scanner.ReadChar() == -1)
							{
								scanner.Position = pos;
								return -1;
							}
							length++;
						}
						return length;
					}
					scanner.Position = pos;
					return -1;
				}
				tagNameBuilder.Clear();
				var first = true;
				while (ch != -1 && char.IsLetter((char)ch))
				{
					if (first && !char.IsLetter((char)ch))
					{
						scanner.Position = pos;
						return -1;
					}
					first = false;
					tagNameBuilder.Append((char)ch);
					ch = scanner.ReadChar();
					length++;
				}
				if (ch == ' ' || ch == '>' || ch == '/')
				{
					var isVoid = voidTags.Contains(tagNameBuilder.ToString());
					int prev = ch;
					while (ch != '>' && ch != -1)
					{
						prev = ch;
						if (ch == '\'' || ch == '"')
						{
							var quote = ch;
							ch = scanner.ReadChar();
							length++;
							while (ch != quote && ch != -1)
							{
								if (ch == '\n')
								{
									scanner.Position = pos;
									return -1;
								}
								ch = scanner.ReadChar();
								length++;
							}
						}
						ch = scanner.ReadChar();
						length++;
					}
					if (ch != '>')
					{
						scanner.Position = pos;
						return -1;
					}
					if (isVoid || prev == '/') // self closing, or a tag with no content
					{
						return length;
					}

					// now, read till the end tag
					if (MatchContent && Name != null)
						args.Push();
					var start = pos + length;
					var contentLength = 0;
					tagNameBuilder.Insert(0, "</");
					tagNameBuilder.Append(">");
					var tagName = tagNameBuilder.ToString();
					while (!scanner.ReadString(tagName, true))
					{
						ch = scanner.Peek();
						if (ch == '<')
						{
							if (MatchContent && contentLength > 0)
							{
								args.AddMatch(this, start, contentLength, "content");
							}
							var inner = Parse(args);
							if (inner < 0)
							{
								scanner.Position = pos;
								return -1;
							}
							length += inner;
							start = pos + length;
							contentLength = 0;
							continue;
						}
							if (scanner.Advance(1) >= 0)
						{
							length++;
							contentLength++;
						}
						else
						{
							scanner.Position = pos;
							return -1;
						}
					}
					length += tagName.Length;
					if (MatchContent)
					{
						if (contentLength > 0)
						{
							args.AddMatch(this, start, contentLength, "content");
						}
						if (Name != null)
							args.PopMatch(this, pos, length);
					}
					return length;
				}
			}
			scanner.Position = pos;
			return -1;
		}