Example #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);
            }
        }
Example #2
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();
            }
        }