Skip to content

InvalidCastException when using .Then<T>() where T is not IConvertible #235

Description

@nicro950

Hi, after we updated to version 1.4.0 of parlot we started to get System.InvalidCastException : Object must implement IConvertible. exceptions in the code. It looks like it happens when using .Then<T>() where T is not IConvertible. Was this intentional, that Then now requires a IConvertible to discard the result?

Here is a snippet of test code which now fails after 1.4.0. The first case with the // A comment fails, but the other one is ok.

using Parlot.Fluent;
using static Parlot.Fluent.Parsers;

public class TestClass
{
    [Theory]
    [InlineData("""
        // A Comment
        var a = 'test'
        """)]
    [InlineData("""
        var a = 'test'
        """)]
    public void SimpleTest(string testString)
    {
        var commentParser = Terms.Text("//").And(AnyCharBefore(OneOf(Literals.Char('\n'), Literals.Char('\r'))));
        var varKeyword = Terms.Text("var");
        var identifier = Terms.Identifier();
        var equal = Terms.Char('=');
        var str = Terms.String();

        var expressionParser = varKeyword.And(identifier).And(equal).And(str).Then<BaseExp?>(a => new AssignExpression(a.Item2.ToString()!, a.Item4.ToString()!));

        var simpleParser = OneOf(commentParser.Then<BaseExp>(), expressionParser);

        var result = simpleParser.Parse(testString);
    }

    public abstract record BaseExp;

    public record AssignExpression(string Name, string Value) : BaseExp;
}

We were able to work around it by calling .Then(default(BaseExp)) instead, for ignoring the value

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions