package metalexer.jflex;

import java.util.HashMap;
import java.util.Map;

public class SymbolAssigner {
    private int counter;
    
    private final Map<String, Integer> symbolMap;

    public SymbolAssigner(int firstSymbol) {
        this.counter = firstSymbol;
        this.symbolMap = new HashMap<String, Integer>();
    }

    public int assignSymbol(String name) {
        if(symbolMap.containsKey(name)) {
            return symbolMap.get(name);
        }
        if(counter == Integer.MAX_VALUE) {
            throw new IllegalStateException("No more symbols available.");
        }
        int sym = counter++;
        symbolMap.put(name, sym);
        return sym;
    }
    
    public int getNumSymbols() {
        return counter;
    }
}
