1 package org.apache.oro.text.awk; 2 3 59 60 import java.io.Serializable ; 61 import java.util.*; 62 63 import org.apache.oro.text.regex.*; 64 65 66 final class DFAState { 67 int _stateNumber; 68 BitSet _state; 69 70 DFAState(BitSet s, int num){ 71 _state = s; 72 _stateNumber = num; 73 } 74 } 75 76 94 public final class AwkPattern implements Pattern, Serializable { 95 final static int _INVALID_STATE = -1, _START_STATE = 1; 96 97 int _numStates, _endPosition, _options; 98 String _expression; 99 Vector _Dtrans, _nodeList[], _stateList; 100 BitSet _U, _emptySet, _followSet[], _endStates; 101 Hashtable _stateMap; 102 boolean _matchesNullString, _fastMap[]; 103 boolean _hasBeginAnchor = false, _hasEndAnchor = false; 104 105 AwkPattern(String expression, SyntaxTree tree){ 106 int token, node, tstateArray[]; 107 DFAState dfaState; 108 109 _expression = expression; 110 111 _endPosition = tree._positions - 1; 113 _followSet = tree._followSet; 114 115 _Dtrans = new Vector(); 116 _stateList = new Vector(); 117 _endStates = new BitSet(); 118 119 _U = new BitSet(tree._positions); 120 _U.or(tree._root._firstPosition()); 121 122 tstateArray = new int[LeafNode._NUM_TOKENS]; 123 _Dtrans.addElement(tstateArray); _Dtrans.addElement(tstateArray); 126 127 _numStates = _START_STATE; 128 if(_U.get(_endPosition)) 129 _endStates.set(_numStates); 130 dfaState = new DFAState((BitSet)_U.clone(), _numStates); 131 _stateMap = new Hashtable(); 132 _stateMap.put(dfaState._state, dfaState); 133 _stateList.addElement(dfaState); _stateList.addElement(dfaState); 136 _numStates++; 137 138 _U.xor(_U); _emptySet = new BitSet(tree._positions); 140 141 _nodeList = new Vector[LeafNode._NUM_TOKENS]; 142 for(token = 0; token < LeafNode._NUM_TOKENS; token++){ 143 _nodeList[token] = new Vector(); 144 for(node=0; node < tree._positions; node++) 145 if(tree._nodes[node]._matches((char)token)) 146 _nodeList[token].addElement(tree._nodes[node]); 147 } 148 149 _fastMap = tree.createFastMap(); 150 _matchesNullString = _endStates.get(_START_STATE); 151 } 152 153 void _createNewState(int current, int token, int[] tstateArray) { 155 int node, pos; 156 DFAState T, dfaState; 157 158 T = (DFAState)_stateList.elementAt(current); 159 node = _nodeList[token].size(); 160 _U.xor(_U); while(node-- > 0){ 162 pos = ((LeafNode)_nodeList[token].elementAt(node))._position; 163 if(T._state.get(pos)) 164 _U.or(_followSet[pos]); 165 } 166 167 if(!_stateMap.containsKey(_U)){ 168 dfaState = new DFAState((BitSet)_U.clone(), _numStates++); 169 _stateList.addElement(dfaState); 170 _stateMap.put(dfaState._state, dfaState); 171 _Dtrans.addElement(new int[LeafNode._NUM_TOKENS]); 172 173 if(!_U.equals(_emptySet)){ 174 tstateArray[token] = _numStates - 1; 175 176 if(_U.get(_endPosition)) 177 _endStates.set(_numStates - 1); 178 } else 179 tstateArray[token] = _INVALID_STATE; 180 } else { 181 if(_U.equals(_emptySet)) 182 tstateArray[token] = _INVALID_STATE; 183 else 184 tstateArray[token] = ((DFAState)_stateMap.get(_U))._stateNumber; 185 } 186 } 187 188 int[] _getStateArray(int state) { return ((int[])_Dtrans.elementAt(state)); } 189 190 191 197 public String getPattern() { return _expression; } 198 199 200 206 public int getOptions() { return _options; } 207 } 208 209 | Popular Tags |