package com.github.fge.grappa.trace.parser;

import com.github.fge.grappa.parsers.EventBusParser;
import com.github.fge.grappa.rules.Rule;
import com.github.fge.grappa.trace.TraceEvent;

import java.util.Objects;
import java.util.concurrent.BlockingQueue;

public class TraceEventParser
    extends EventBusParser<TraceEvent>
{
    protected final TraceEventBuilder builder = new TraceEventBuilder();
    private final BlockingQueue<TraceEvent> queue;

    public TraceEventParser(final BlockingQueue<TraceEvent> queue)
    {
        this.queue = Objects.requireNonNull(queue);
    }

    public Rule traceEvent()
    {
        return sequence(
            eventType(), ';',
            index(), ';',
            level(), ';',
            matcher(), ';',
            matcherType(), ';',
            matcherClass(), ';',
            nanoSeconds(), eof(),
            pushToQueue()
        );
    }

    Rule eventType()
    {
        return sequence(digit(), builder.setType(match()));
    }

    Rule index()
    {
        return sequence(oneOrMore(digit()), builder.setIndex(match()));
    }

    Rule level()
    {
        return sequence(oneOrMore(digit()), builder.setLevel(match()));
    }

    Rule matcher()
    {
        return sequence(
            join(oneOrMore(firstOf(noneOf("\\;"), sequence('\\', noneOf(";")))))
                .using("\\;")
                .min(1),
            builder.setMatcher(match())
        );
    }

    Rule matcherType()
    {
        return sequence(digit(), builder.setMatcherType(match()));
    }

    Rule matcherClass()
    {
        return sequence(oneOrMore(noneOf(";")),
            builder.setMatcherClass(match()));
    }

    Rule nanoSeconds()
    {
        return sequence(oneOrMore(digit()), builder.setNanoseconds(match()));
    }

    boolean pushToQueue()
    {
        try {
            queue.put(builder.build());
            return true;
        } catch (InterruptedException ignored) {
            Thread.currentThread().interrupt();
            return false;
        }
    }
}

@@ -574,24 +574,148 @@
 ue);%0A    %7D%0A%0A
+    public Rule traceEvents(final int nrEvents)%0A    %7B%0A        return join(traceEvent()).using('%5Cn').times(nrEvents);%0A    %7D%0A%0A
     public R
@@ -967,15 +967,8 @@
 s(),
- eof(),
 %0A   

allow to read a batch of events