sorry for the spam...
turns out you can skip the initial value and inject will use the first
value in the list as result and the second as element, before folding the
rest of the list...so ...
rule(:magic_label) { magic_words.map{|m| "magic_#{ m }".to_sym}.inject{
|result,element| result = result | element } }
should work.
---
"No man is an island... except Philip"
On 22 February 2012 11:15, Nigel Thorne <[email protected]> wrote:
> to clarify.. something like this...
>
> rule(:magic_label) { magic_words.inject(false){ |result,element| result =
> result | "magic_#{ element }".to_sym }
>
>
> I think the 'false' should really be an instance of
> Parslet::Atom::Base::Fail
>
> ---
> "No man is an island... except Philip"
>
>
> On 22 February 2012 11:09, Nigel Thorne <[email protected]> wrote:
>
>> You want to use 'inject' or 'reduce' not map.
>>
>> You are building up a matcher by 'or'ing it with the previous matcher.
>>
>>
>> ---
>> "No man is an island... except Philip"
>>
>>
>>
>> On 22 February 2012 04:16, David Sklar <[email protected]> wrote:
>>
>>> 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
>>>
>>>
>>
>