/// <summary>
        /// If the result is empty, format the fragment of text describing the error.
        /// </summary>
        /// <returns>The error fragment.</returns>
        public string FormatErrorMessageFragment()
        {
            if (ErrorMessage != null)
            {
                return(ErrorMessage);
            }

            string message;

            if (Remainder.IsAtEnd)
            {
                message = "unexpected end of input";
            }
            else
            {
                var next       = Remainder.ConsumeToken().Value;
                var appearance = Presentation.FormatAppearance(next.Kind, next.ToStringValue());
                message = $"unexpected {appearance}";
            }

            if (Expectations != null)
            {
                var expected = Friendly.List(Expectations);
                message += $", expected {expected}";
            }

            return(message);
        }
Esempio n. 2
0
        /// <inheritdoc />
        public override string ToString()
        {
            if (Remainder == TokenList <TKind> .Empty)
            {
                return("(Empty result.)");
            }

            if (HasValue)
            {
                return($"Successful parsing of {Value}.");
            }

            var message  = FormatErrorMessageFragment();
            var location = "";

            if (!Remainder.IsAtEnd)
            {
                var next           = Remainder.ConsumeToken().Value;
                var sourcePosition = ErrorPosition.HasValue ? ErrorPosition : next.Position;
                location = $" (line {sourcePosition.Line}, column {sourcePosition.Column})";
            }

            return($"Syntax error{location}: {message}.");
        }
        /// <inheritdoc />
        public override string ToString()
        {
            if (Remainder == TokenList <TKind> .Empty)
            {
                return("(Empty result.)");
            }

            if (HasValue)
            {
                return($"Successful parsing of {Value}.");
            }

            var message  = FormatErrorMessageFragment();
            var location = "";

            if (!Remainder.IsAtEnd)
            {
                // Since the message notes `end of input`, don't report line/column here.
                var sourcePosition = SubTokenErrorPosition.HasValue ? SubTokenErrorPosition : Remainder.ConsumeToken().Value.Position;
                location = $" (line {sourcePosition.Line}, column {sourcePosition.Column})";
            }

            return($"Syntax error{location}: {message}.");
        }