enhancement thought for MakeStar and MakePlus

Sep 26, 2012 at 9:12 AM
Edited Sep 26, 2012 at 9:23 AM

Have you considered enhancing MakeListRule to something like this?

protected BnfExpression MakeListRule(NonTerminal list, BnfTerm delimiter, BnfTerm listMember, TermListOptions options = TermListOptions.PlusList) {
return MakeList(list,delimiter, listMember, options).Rule;
}
protected NonTerminal MakeList(NonTerminal list, BnfTerm delimiter, BnfTerm listMember, TermListOptions options = TermListOptions.PlusList) {
//body identical to that of current MakeListRule except for:
return list;
}//method

 
Sep 26, 2012 at 9:21 AM
Edited Sep 26, 2012 at 9:22 AM

My apologies for the mangled first post.

The advantage of this enhancement is of enabling definition of the following methods, which I have found useful by implementing this change in a sub-class of Grammar:

public NonTerminal MakeStarList(BnfTerm listMember, BnfTerm delimiter = null, TermListOptions options = TermListOptions.PlusList) {
    return MakeList(new NonTerminal(listMember.Name + "*"), delimiter, listMember, options);
}
public NonTerminal MakeStarList(BnfTerm listMember, Type nodeType, BnfTerm delimiter = null, TermListOptions options = TermListOptions.PlusList) {
    return MakeList(new NonTerminal(listMember.Name + "*", nodeType), delimiter, listMember, options);
}
public NonTerminal MakePlusList(BnfTerm listMember, BnfTerm delimiter = null, TermListOptions options = TermListOptions.PlusList) {
    return MakeList(new NonTerminal(listMember.Name + "+"), delimiter, listMember, options);
}
public NonTerminal MakePlusList(BnfTerm listMember, Type nodeType, BnfTerm delimiter = null, TermListOptions options = TermListOptions.PlusList) {
    return MakeList(new NonTerminal(listMember.Name + "+", nodeType), delimiter, listMember, options);
}

This allows definition of grammars without the explicit of single-use lists, so that the result is even closer to an ABNF specification.

Coordinator
Sep 26, 2012 at 4:40 PM

don't quite see the point. Looks like minor style-type enhancements; quite possible to cause confusion for many users - more than one way to do things, or more than one method to create a list is always a risk of confusion. One returns rule, another returns non-terminal. 

And you can always create some helper methods for yourself if you feel like it

Roman

Sep 26, 2012 at 4:58 PM

I was typing my way through a rather lengthy ABNF spec, and about the fourth time I had to go back and create a new dummy non-terminal just to hold a phantom plus/star list, I dug into your code to see if there was a better way. I have sub-classed Grammar in my own utility, but that implementation requires that I be attentive to all changes you might make to MakeListRule, or my subclass will degrade.

ABNF here for reference: http://www.norbeck.nu/abc/abcbnf.htm#text

Hope to eventually tackle this one: http://www.norbeck.nu/abc/bnf/abc20bnf.htm

 

Sep 26, 2012 at 9:24 PM

The enhanced methods shoudl look more like this, of course.

protected NonTerminal MakeStarList(BnfTerm listMember, BnfTerm delimiter = null, TermListOptions options = TermListOptions.None) {
    return MakeList(new NonTerminal(listMember.Name + "*"),delimiter,listMember,options |= TermListOptions.AllowEmpty);
}
protected NonTerminal MakeStarList(BnfTerm listMember, Type nodeType, BnfTerm delimiter = null, TermListOptions options = TermListOptions.None) {
    return MakeList(new NonTerminal(listMember.Name + "*", nodeType),delimiter,listMember,options |= TermListOptions.AllowEmpty);
}
protected NonTerminal MakePlusList(BnfTerm listMember, BnfTerm delimiter = null, TermListOptions options = TermListOptions.PlusList) {
    return MakeList(new NonTerminal(listMember.Name + "+"),delimiter,listMember,options |= TermListOptions.PlusList);
}
protected NonTerminal MakePlusList(BnfTerm listMember, Type nodeType, BnfTerm delimiter = null, TermListOptions options = TermListOptions.PlusList) {
    return MakeList(new NonTerminal(listMember.Name + "+", nodeType),delimiter,listMember,options |= TermListOptions.PlusList);
}