1 18 19 package org.sablecc.sablecc.automaton; 20 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 import java.util.SortedSet ; 25 26 import org.sablecc.sablecc.exception.InternalException; 27 28 33 class StateMatcher<T extends Comparable <? super T>> { 34 35 36 private final Dfa<T> dfa; 37 38 39 private final Nfa<T> nfa; 40 41 45 private final Map <DfaState<T>, SortedSet <NfaState<T>>> dfaToNfaSetMap = new HashMap <DfaState<T>, SortedSet <NfaState<T>>>(); 46 47 51 private final Map <SortedSet <NfaState<T>>, DfaState<T>> nfaSetToDfaMap = new HashMap <SortedSet <NfaState<T>>, DfaState<T>>(); 52 53 65 StateMatcher( 66 final Dfa<T> dfa, 67 final Nfa<T> nfa) { 68 69 if (dfa == null) { 70 throw new InternalException("dfa may not be null"); 71 } 72 73 if (nfa == null) { 74 throw new InternalException("nfa may not be null"); 75 } 76 77 this.dfa = dfa; 78 this.nfa = nfa; 79 } 80 81 93 DfaState<T> getDfaState( 94 SortedSet <NfaState<T>> nfaStates) { 95 96 if (nfaStates == null) { 97 throw new InternalException("nfaStates may not be null"); 98 } 99 100 DfaState<T> dfaState = this.nfaSetToDfaMap.get(nfaStates); 101 102 if (dfaState == null) { 103 104 for (NfaState<T> state : nfaStates) { 105 if (!this.nfa.getStates().contains(state)) { 106 throw new InternalException( 107 "invalid nfa state in nfaStates"); 108 } 109 } 110 111 dfaState = new DfaState<T>(this.dfa); 112 113 SortedSet <NfaState<T>> unmodifiableNfaStates = Collections 114 .unmodifiableSortedSet(nfaStates); 115 this.nfaSetToDfaMap.put(unmodifiableNfaStates, dfaState); 116 this.dfaToNfaSetMap.put(dfaState, unmodifiableNfaStates); 117 } 118 119 return dfaState; 120 } 121 122 135 SortedSet <NfaState<T>> getNfaStates( 136 DfaState<T> dfaState) { 137 138 if (dfaState == null) { 139 throw new InternalException("dfaState may not be null"); 140 } 141 142 if (!this.dfa.getUnstableStates().contains(dfaState)) { 143 throw new InternalException("invalid dfaState"); 144 } 145 146 SortedSet <NfaState<T>> nfaStates = this.dfaToNfaSetMap.get(dfaState); 147 if (nfaStates == null) { 148 throw new InternalException( 149 "corrupted internal data structures detected"); 150 } 151 152 return nfaStates; 153 } 154 155 175 boolean match( 176 DfaState<T> dfaState, 177 NfaState<T> nfaState) { 178 179 if (dfaState == null) { 180 throw new InternalException("dfaState may not be null"); 181 } 182 183 if (nfaState == null) { 184 throw new InternalException("nfaState may not be null"); 185 } 186 187 if (!this.dfa.getUnstableStates().contains(dfaState)) { 188 throw new InternalException("invalid dfaState"); 189 } 190 191 if (!this.nfa.getStates().contains(nfaState)) { 192 throw new InternalException("invalid nfaState"); 193 } 194 195 SortedSet <NfaState<T>> nfaStates = this.dfaToNfaSetMap.get(dfaState); 196 if (nfaStates == null) { 197 throw new InternalException( 198 "corrupted internal data structures detected"); 199 } 200 201 return nfaStates.contains(nfaState); 202 } 203 } 204 | Popular Tags |