Failing to see how to grab the context... Help needed!

classic Classic list List threaded Threaded
1 message Options
fge
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Failing to see how to grab the context... Help needed!

fge
OK, I have tried to debug this for the last hours and for now I can't do it.

I want to simplify value collecting in parsers; the goal is to write:

Rule someRule()
{
    return consume(ruleHere).withName(someName).into(consumer);
}

so I tried and did this:

public abstract class ConsumingMatchParser<V>
    extends BaseParser<V>
{

    protected ConsumerBuilder<V> consume(final Rule rule)
    {
        return new ConsumerBuilder<V>(rule);
    }

    public class ConsumerBuilder<E>
        implements ContextAware<E>
    {
        protected Context<E> context;
        private final Rule rule;

        protected ConsumerBuilder(@Nonnull final Rule rule)
        {
            this.rule = Preconditions.checkNotNull(rule);
        }

        public ConsumerBuilder withName(@Nullable final String label)
        {
            if (label != null)
                rule.label(label);
            return this;
        }

        public Rule into(@Nonnull final MatchConsumer consumer)
        {
            final Action<E> action = new Action<E>()
            {
                @Override
                public boolean run(final Context<E> context)
                {
                    consumer.consume(context.getMatch());
                    return true;
                }
            };
            return Sequence(rule, ACTION(action.run(context)));
        }

        @Override
        public void setContext(@Nonnull final Context<E> context)
        {
            this.context = Preconditions.checkNotNull(context);
        }
    }
}

But it doesn't work. Context is null.

I also tried and do it in a custom Matcher where I try and:

    @Override
    public <V> boolean match(final MatcherContext<V> context)
    {
        if (!matcher.match(context))
            return false;
        consumer.consume(context.getMatch());
        return true;
    }

but parboiled will complain that I can't .getMatch() here :/

What am I missing?
Loading...