picoe / Eto.Parse

Recursive descent LL(k) parser for .NET with Fluent API, BNF, EBNF and Gold Grammars
MIT License
148 stars 30 forks source link

Is there any reason why Name is not preserved when cloning a Parser #46

Closed olivr70 closed 3 years ago

olivr70 commented 3 years ago

Hello,

I just discovered Eto.Parse and I really enjoy it. Great piece of work.

I would like use it to build a library of reusable parsers which can be combined in multiple small grammars for scraping items in documents.

The problem is that when cloning a Parser, the names are lost and it does not properly fill the Matches property of the result

The following test fails

            var p = Eto.Parse.Terminals.Literal("hello");
            var helloNamed = p.Named("greetings");
            Assert.AreEqual("greetings", helloNamed.Name);
            var helloClone = helloNamed.Clone();
            Assert.AreEqual("greetings", helloClone.Name); // FAILS helloClone.Name is null

I just checked Parser copy constructor, and it does not set the name of the new instance to the one of the source.

Is there any reason for this, or can I make a Pull request ?

olivr70 commented 3 years ago

Just discovered that AddMatch property is not copied either in Parser copy constructor.

I connot see a reason why, and from a newcomer perspective I do not predict any negative impacts.

cwensley commented 3 years ago

The main reason is having two parsers with the same name in the same grammar is a conflict. It could very well copy the name, however as you can just overwrite it with the WithName() extension.

Thanks for the feedback! (and sorry for the late response)