static string BuildErrorMessages(ParseContext context) { var newLine = Environment.NewLine; var text = new StringBuilder(); var textLength = context.BaseText.Length; if (context.ErrorCount > 1) text.Append(newLine); for (var i = 0; i < context.ErrorCount; ++i) { var token = context.GetError(i); if (i > 0) text.Append(newLine); if (context.ErrorCount > 1) text.Append(' '); var found = context.BaseText.Substring(token.Offset); // Truncate the "found" text before the start of the next error if (i + 1 < context.ErrorCount) { var nextError = context.GetError(i + 1); var length = nextError.Offset - token.Offset; if (found.Length > length) found = found.Substring(0, length); } if (found.Length > 0) { text.Append("Expected " + context.Expected + " but found " + Truncate(found) + " when matching " + GetConstructName(token.GrammarConstruct)); } else if (token.Offset + found.Length >= textLength) { text.Append("Expected " + context.Expected + " but reached end of input when matching " + GetConstructName(token.GrammarConstruct)); } else { text.Append("Expected " + context.Expected + " but was missing when matching " + GetConstructName(token.GrammarConstruct)); } } return text.ToString(); }