1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 16:00: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 b5ff2c1c2b
commit 586cd90e32

View File

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