dotnet/wpf · error · InvalidOperationException

SR.Format(SR.TokenizerHelperPrematureStringTermination…

Error message

SR.Format(SR.TokenizerHelperPrematureStringTermination, _str)

What it means

TokenizerHelper.NextTokenRequired throws InvalidOperationException when the string ends before another token can be read. It is used by WPF's MiniLanguage parsers (points, vectors, colors) to guarantee a token exists before parsing. The message embeds the offending input string.

Solutions

  1. Supply the full token sequence expected by the MiniLanguage (every point/coordinate pair present)
  2. Verify the string is not truncated or ends with a dangling separator before passing it to the converter
  3. Ensure the IFormatProvider/culture matches the separator used in the string
  4. Wrap the Parse/ConvertFrom call in try-catch for InvalidOperationException and surface a user-friendly parse error

Example fix

// before
pc.Parse("10,20 30,40 50,");
// after
pc.Parse("10,20 30,40 50,60");
Defensive patterns

Strategy: validation

Validate before calling

static bool CanParsePointCollection(string s, IFormatProvider culture) {
    if (string.IsNullOrWhiteSpace(s)) return false;
    var sep = culture.TextInfo.ListSeparator[0];
    return !s.TrimEnd().EndsWith(sep) && !s.Contains(new string(sep, 2));
}

Try / catch

try { var pc = PointCollection.Parse(s); }
catch (InvalidOperationException ex) { /* show ex.Message + raw s */ }

Prevention

When it happens

Trigger: Calling ParsePointCollection, ParseThreeDoublesCollection, ConvertFrom, or Color parser methods with a string that stops mid-sequence, e.g. '0,0 1,1' followed by a trailing separator or '1,2,' so NextToken returns false.

Common situations: Hand-written XAML attribute values in MiniLanguage (e.g. StrokeDashCollection, PointCollection markup) with missing coordinate values; truncated clipboard-pasted geometry strings; localized input where the numeric separator differs from the culture.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/9106695d29e29cb9. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/TokenizerHelper.cs:123

        /// <summary>
        /// Advances to the NextToken
        /// </summary>
        /// <returns>true if next token was found, false if at end of string</returns>
        internal bool NextToken()
        {
            return NextToken(false);
        }

        /// <summary>
        /// Advances to the NextToken, throwing an exception if not present
        /// </summary>
        /// <returns>The next token found</returns>
        internal string NextTokenRequired()
        {
            if (!NextToken(false))
            {
                throw new System.InvalidOperationException(SR.Format(SR.TokenizerHelperPrematureStringTermination, _str));
            }

            return GetCurrentToken();
        }

        /// <summary>
        /// Advances to the NextToken, throwing an exception if not present
        /// </summary>
        /// <returns>The next token found</returns>
        internal string NextTokenRequired(bool allowQuotedToken)
        {
            if (!NextToken(allowQuotedToken))
            {
                throw new System.InvalidOperationException(SR.Format(SR.TokenizerHelperPrematureStringTermination, _str));
            }

            return GetCurrentToken();
        }

View on GitHub (pinned to 81131a70a4)