00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 package org.antlr.runtime;
00029
00041 public class DFA {
00042 protected short[] eot;
00043 protected short[] eof;
00044 protected char[] min;
00045 protected char[] max;
00046 protected short[] accept;
00047 protected short[] special;
00048 protected short[][] transition;
00049
00050 protected int decisionNumber;
00051
00053 protected BaseRecognizer recognizer;
00054
00055 public static final boolean debug = false;
00056
00062 public int predict(IntStream input)
00063 throws RecognitionException
00064 {
00065 if ( debug ) {
00066 System.err.println("Enter DFA.predict for decision "+decisionNumber);
00067 }
00068 int mark = input.mark();
00069 int s = 0;
00070 try {
00071 while ( true ) {
00072 if ( debug ) System.err.println("DFA "+decisionNumber+" state "+s+" LA(1)="+(char)input.LA(1)+"("+input.LA(1)+
00073 "), index="+input.index());
00074 int specialState = special[s];
00075 if ( specialState>=0 ) {
00076 if ( debug ) {
00077 System.err.println("DFA "+decisionNumber+
00078 " state "+s+" is special state "+specialState);
00079 }
00080 s = specialStateTransition(specialState,input);
00081 if ( debug ) {
00082 System.err.println("DFA "+decisionNumber+
00083 " returns from special state "+specialState+" to "+s);
00084 }
00085 if ( s==-1 ) {
00086 noViableAlt(s,input);
00087 return 0;
00088 }
00089 input.consume();
00090 continue;
00091 }
00092 if ( accept[s] >= 1 ) {
00093 if ( debug ) System.err.println("accept; predict "+accept[s]+" from state "+s);
00094 return accept[s];
00095 }
00096
00097 char c = (char)input.LA(1);
00098 if (c>=min[s] && c<=max[s]) {
00099 int snext = transition[s][c-min[s]];
00100 if ( snext < 0 ) {
00101
00102
00103
00104
00105 if ( eot[s]>=0 ) {
00106 if ( debug ) System.err.println("EOT transition");
00107 s = eot[s];
00108 input.consume();
00109
00110
00111
00112
00113
00114 continue;
00115 }
00116 noViableAlt(s,input);
00117 return 0;
00118 }
00119 s = snext;
00120 input.consume();
00121 continue;
00122 }
00123 if ( eot[s]>=0 ) {
00124 if ( debug ) System.err.println("EOT transition");
00125 s = eot[s];
00126 input.consume();
00127 continue;
00128 }
00129 if ( c==(char)Token.EOF && eof[s]>=0 ) {
00130 if ( debug ) System.err.println("accept via EOF; predict "+accept[eof[s]]+" from "+eof[s]);
00131 return accept[eof[s]];
00132 }
00133
00134 if ( debug ) {
00135 System.err.println("min["+s+"]="+min[s]);
00136 System.err.println("max["+s+"]="+max[s]);
00137 System.err.println("eot["+s+"]="+eot[s]);
00138 System.err.println("eof["+s+"]="+eof[s]);
00139 for (int p=0; p<transition[s].length; p++) {
00140 System.err.print(transition[s][p]+" ");
00141 }
00142 System.err.println();
00143 }
00144 noViableAlt(s,input);
00145 return 0;
00146 }
00147 }
00148 finally {
00149 input.rewind(mark);
00150 }
00151 }
00152
00153 protected void noViableAlt(int s, IntStream input) throws NoViableAltException {
00154 if (recognizer.state.backtracking>0) {
00155 recognizer.state.failed=true;
00156 return;
00157 }
00158 NoViableAltException nvae =
00159 new NoViableAltException(getDescription(),
00160 decisionNumber,
00161 s,
00162 input);
00163 error(nvae);
00164 throw nvae;
00165 }
00166
00168 protected void error(NoViableAltException nvae) { ; }
00169
00170 public int specialStateTransition(int s, IntStream input)
00171 throws NoViableAltException
00172 {
00173 return -1;
00174 }
00175
00176 public String getDescription() {
00177 return "n/a";
00178 }
00179
00185 public static short[] unpackEncodedString(String encodedString) {
00186
00187 int size = 0;
00188 for (int i=0; i<encodedString.length(); i+=2) {
00189 size += encodedString.charAt(i);
00190 }
00191 short[] data = new short[size];
00192 int di = 0;
00193 for (int i=0; i<encodedString.length(); i+=2) {
00194 char n = encodedString.charAt(i);
00195 char v = encodedString.charAt(i+1);
00196
00197 for (int j=1; j<=n; j++) {
00198 data[di++] = (short)v;
00199 }
00200 }
00201 return data;
00202 }
00203
00205 public static char[] unpackEncodedStringToUnsignedChars(String encodedString) {
00206
00207 int size = 0;
00208 for (int i=0; i<encodedString.length(); i+=2) {
00209 size += encodedString.charAt(i);
00210 }
00211 char[] data = new char[size];
00212 int di = 0;
00213 for (int i=0; i<encodedString.length(); i+=2) {
00214 char n = encodedString.charAt(i);
00215 char v = encodedString.charAt(i+1);
00216
00217 for (int j=1; j<=n; j++) {
00218 data[di++] = v;
00219 }
00220 }
00221 return data;
00222 }
00223
00224
00225
00226
00227
00228
00229 }