sliekens / Txt

A text parsing framework for .NET.
MIT License
2 stars 4 forks source link

Recursive rules #14

Open sliekens opened 8 years ago

sliekens commented 8 years ago

It's not possible right now to construct a rule that references itself.

grouping = "(" *(ALPHA / grouping) ")"
         ; matches ()
         ; matches (abc)
         ; matches (abc())
         ; matches (abc(abc))
         ; etc.

The reason is that, for this example, the GroupingLexer must be materialized before it can be added to its own list of lexer rules. This is not supported right now because of an oversight.

sliekens commented 8 years ago

Probably the best option at this time is to add a delegating lexer class that can be configured after materializing the lexer rule.

var proxy = new DelegatingLexer<Grouping>();
var groupingLexer = new GroupingLexer(..., proxy);
proxy.Rule = groupingLexer;

All problems in computer science can be solved by another level of indirection