package org.nwapw.abacus.lexing;

import org.nwapw.abacus.lexing.pattern.EndNode;
import org.nwapw.abacus.lexing.pattern.Match;
import org.nwapw.abacus.lexing.pattern.Pattern;
import org.nwapw.abacus.lexing.pattern.PatternNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;

public class Lexer<T> {

    private ArrayList<Pattern<T>> patterns;

    public Lexer(){
        patterns = new ArrayList<>();
    }

    public void register(String pattern, T id){
        Pattern<T> compiledPattern = new Pattern<>(pattern, id);
        if(compiledPattern.getHead() != null) patterns.add(compiledPattern);
    }

    public Match<T> lexOne(String from, int startAt, Comparator<T> compare){
        ArrayList<Match<T>> matches = new ArrayList<>();
        HashSet<PatternNode<T>> currentSet = new HashSet<>();
        HashSet<PatternNode<T>> futureSet = new HashSet<>();
        int index = startAt;
        for(Pattern<T> pattern : patterns){
            pattern.getHead().addInto(currentSet);
        }
        while(!currentSet.isEmpty() && index < from.length()){
            char currentChar = from.charAt(index);
            for(PatternNode<T> node : currentSet){
                if(node.matches(currentChar)) {
                    node.addOutputsInto(futureSet);
                } else if(node instanceof EndNode){
                    matches.add(new Match<>(startAt, index, ((EndNode<T>) node).getPatternId()));
                }
            }

            HashSet<PatternNode<T>> tmp = currentSet;
            currentSet = futureSet;
            futureSet = tmp;
            futureSet.clear();

            index++;
        }
        matches.sort((a, b) -> compare.compare(a.getType(), b.getType()));
        if(compare != null) {
            matches.sort(Comparator.comparingInt(a -> a.getTo() - a.getFrom()));
        }
        return matches.isEmpty() ? null : matches.get(matches.size() - 1);
    }

    public ArrayList<Match<T>> lexAll(String from, int startAt, Comparator<T> compare){
        int index = startAt;
        ArrayList<Match<T>> matches = new ArrayList<>();
        Match<T> lastMatch = null;
        while((lastMatch = lexOne(from, index, compare)) != null && index < from.length()){
            if(lastMatch.getTo() == lastMatch.getFrom()) return null;
            matches.add(lastMatch);
            index += lastMatch.getTo() - lastMatch.getFrom();
        }
        return matches;
    }

}

@@ -254,38 +254,8 @@
 st;%0A
-import java.util.Collections;%0A
 impo
@@ -1068,86 +1068,10 @@
 ty()
- && index %3C from.length())%7B%0A            char currentChar = from.charAt(index);
+)%7B
 %0A   
@@ -1141,32 +1141,64 @@
  if(
-node.matches(currentChar
+index %3C from.length() && node.matches(from.charAt(index)
 )) %7B

Fix bug causing the last character not to be matched.