I have a list of strings that are magical tokens in the pre-existing
language I am writing a grammar for.
Right now, my rules for parsing them look something like:
rule(:regular_label) { match('[a-z0-9.]').repeat(1) }
magic_words = ["magic1","moremagic","evenmoremagic"]
magic_words.each do |m|
rule("magic_{#m}".to_sym) { str(m) }
end
rule(:magic_label) { :magic_magic1 | :magic_moremagic |
:magic_evenmoremagic }
rule(:any_label) { :magic_label | :regular_label }
Is there a way I can dynamically generate the :magic_label rule? I'd
like to avoid having to update two places if the list of magic strings
changes.
I.e. in pseudocode, I'd like to be able to do something like:
rule(:magic_label) {
something_wonderful(magic_words.map |m| { "magic_#{m}.to_sym" }.join('
| '))
}
Where the something_wonderful() method would take the string it is
passed and turn it into a rule body.
Thanks,
David