1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-22 07:20:09 -08:00

Switch Lexer to use a map for patterns, to allow for removal.

This commit is contained in:
Danila Fedorin 2017-07-27 10:47:11 -07:00
parent 79e85832ce
commit 077a34c618

View File

@ -7,6 +7,7 @@ import org.nwapw.abacus.lexing.pattern.PatternNode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
/**
@ -19,13 +20,13 @@ public class Lexer<T> {
/**
* The registered patterns.
*/
private ArrayList<Pattern<T>> patterns;
private HashMap<String, Pattern<T>> patterns;
/**
* Creates a new lexer with no registered patterns.
*/
public Lexer(){
patterns = new ArrayList<>();
patterns = new HashMap<>();
}
/**
@ -35,7 +36,7 @@ public class Lexer<T> {
*/
public void register(String pattern, T id){
Pattern<T> compiledPattern = new Pattern<>(pattern, id);
if(compiledPattern.getHead() != null) patterns.add(compiledPattern);
if(compiledPattern.getHead() != null) patterns.put(pattern, compiledPattern);
}
/**
@ -50,7 +51,7 @@ public class Lexer<T> {
HashSet<PatternNode<T>> currentSet = new HashSet<>();
HashSet<PatternNode<T>> futureSet = new HashSet<>();
int index = startAt;
for(Pattern<T> pattern : patterns){
for(Pattern<T> pattern : patterns.values()){
pattern.getHead().addInto(currentSet);
}
while(!currentSet.isEmpty()){