/*
 * The MIT License
 *
 * Copyright 2014 gburdell.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package apfe.maze.runtime;

import apfe.maze.runtime.graph.Vertex;

/**
 * An Acceptor with repetition semantics.
 *
 * @author gburdell
 */
public class Repetition extends Acceptor {

    /**
     * Accept 0 or more.
     *
     * @param rep element to repetitively accept.
     */
    public Repetition(Acceptor rep) {
        this(rep, false);
    }

    /**
     * Accept 0/1 or more.
     *
     * @param rep element to repetitively accept.
     * @param oneOrMore specify (true) for rep+ semantics, else rep*.
     */
    public Repetition(Acceptor rep, boolean oneOrMore) {
        m_rep = rep;
        m_oneOrMore = oneOrMore;
    }

    @Override
    public Acceptor create() {
        return new Repetition(m_rep, m_oneOrMore);
    }

    @Override
    protected boolean acceptImpl() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private final Acceptor m_rep;
    private final boolean m_oneOrMore;
}

@@ -1191,16 +1191,68 @@
 .Vertex;
+%0Aimport java.util.Collection;%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -1899,16 +1899,25 @@
 on(m_rep
+.create()
 , m_oneO
@@ -1994,135 +1994,1055 @@
     
-throw new UnsupportedOperationException(%22Not supported yet.%22); //To change body of generated methods, choose Tools %7C Templates.
+int acceptedCnt = 0;%0A        boolean matched = true;%0A        Graph subg;%0A        Vertex%3CState%3E dest;%0A        Collection%3CVertex%3CState%3E%3E srcs = Util.asCollection(getSubgraphRoot());%0A        List%3CVertex%3CState%3E%3E nextSrcs;%0A        while (matched) %7B%0A            nextSrcs = null;%0A            matched = false;%0A            for (Vertex%3CState%3E src : srcs) %7B%0A                dest = new Vertex%3C%3E(src);%0A                //always accept nothing%0A                getSubgraph().addEdge(src, dest, new Optional.Epsilon());%0A                subg = m_rep.accept(src);%0A                if (null != subg) %7B%0A                    //String dbg = subg.toString();%0A                    addEdge(src, m_rep, subg);%0A                    //dbg = getSubgraph().toString();%0A                    nextSrcs = Util.addToList(nextSrcs, getSubgraph().getLeafs());%0A                    matched = true;%0A                %7D%0A            %7D%0A            if (matched) %7B%0A                acceptedCnt++;%0A            %7D%0A            srcs = nextSrcs;%0A        %7D%0A        return (!m_oneOrMore %7C%7C (0 %3C acceptedCnt));
 %0A   

add details