maetl / calyx

A Ruby library for generating text with recursive template grammars.
MIT License
61 stars 5 forks source link

Program hangs if you use request more unique rules than they are actual unique values #24

Closed tra38 closed 6 years ago

tra38 commented 6 years ago

To replicate:

grammar = Calyx::Grammar.new do
  start "{$medal}, {$medal}, {$medal}…and then {$medal}?"
  medal 'Gold', 'Silver', 'Bronze'
end

grammar.generate

The expected behavior (at least to me) is that the medal count would restart - that you would again randomly choose between Gold, Silver, and Bronze. But it's possible that maybe you don't want that sort of behavior, since $ is supposed to represent unique values, and repeating values again might be seen as somewhat confusing. So maybe it's okay to throw an error if this behavior occurs.

maetl commented 6 years ago

Thanks, this is a useful summary. I ran into this question when making the feature, but didn’t make a call on it then—wanted to see what it was like to use in practice first. So the behaviour is currently undefined. It definitely needs to be documented and communicated through either an error message, or recycling the values (I lean towards the former, but the latter might make sense).

maetl commented 6 years ago

I’ve attempted to address this problem in 9fa89cfedb7015ed5484bf9188f2f9307b8794ac

It cycles by default for now. I may add a toggle that throws an error in strict mode, but unsure yet if that will be useful or not.

For now, this change ensures that expansion of unique rules will never hang.