00001 package edu.ksu.cis.bandera.specification.assertion.parser;
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
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 import edu.ksu.cis.bandera.specification.assertion.lexer.*;
00039 import edu.ksu.cis.bandera.specification.assertion.node.*;
00040 import edu.ksu.cis.bandera.specification.assertion.analysis.*;
00041 import java.util.*;
00042
00043 import java.io.DataInputStream;
00044 import java.io.BufferedInputStream;
00045 import java.io.IOException;
00046
00047 public class Parser
00048 {
00049 public final Analysis ignoredTokens = new AnalysisAdapter();
00050
00051 protected Node node;
00052
00053 private final Lexer lexer;
00054 private final ListIterator stack = new LinkedList().listIterator();
00055 private int last_shift;
00056 private int last_pos;
00057 private int last_line;
00058 private final TokenIndex converter = new TokenIndex();
00059 private final int[] action = new int[2];
00060
00061 private final static int SHIFT = 0;
00062 private final static int REDUCE = 1;
00063 private final static int ACCEPT = 2;
00064 private final static int ERROR = 3;
00065
00066 private static int[][][] actionTable;
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 private static int[][][] gotoTable;
00105
00106
00107
00108
00109
00110
00111
00112
00113 private static String[] errorMessages;
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 private static int[] errors;
00127
00128
00129
00130 public Parser(Lexer lexer)
00131 {
00132 this.lexer = lexer;
00133
00134 if(actionTable == null)
00135 {
00136 try
00137 {
00138 DataInputStream s = new DataInputStream(
00139 new BufferedInputStream(
00140 Parser.class.getResourceAsStream("parser.dat")));
00141
00142
00143 int length = s.readInt();
00144 actionTable = new int[length][][];
00145 for(int i = 0; i < actionTable.length; i++)
00146 {
00147 length = s.readInt();
00148 actionTable[i] = new int[length][3];
00149 for(int j = 0; j < actionTable[i].length; j++)
00150 {
00151 for(int k = 0; k < 3; k++)
00152 {
00153 actionTable[i][j][k] = s.readInt();
00154 }
00155 }
00156 }
00157
00158
00159 length = s.readInt();
00160 gotoTable = new int[length][][];
00161 for(int i = 0; i < gotoTable.length; i++)
00162 {
00163 length = s.readInt();
00164 gotoTable[i] = new int[length][2];
00165 for(int j = 0; j < gotoTable[i].length; j++)
00166 {
00167 for(int k = 0; k < 2; k++)
00168 {
00169 gotoTable[i][j][k] = s.readInt();
00170 }
00171 }
00172 }
00173
00174
00175 length = s.readInt();
00176 errorMessages = new String[length];
00177 for(int i = 0; i < errorMessages.length; i++)
00178 {
00179 length = s.readInt();
00180 StringBuffer buffer = new StringBuffer();
00181
00182 for(int j = 0; j < length; j++)
00183 {
00184 buffer.append(s.readChar());
00185 }
00186 errorMessages[i] = buffer.toString();
00187 }
00188
00189
00190 length = s.readInt();
00191 errors = new int[length];
00192 for(int i = 0; i < errors.length; i++)
00193 {
00194 errors[i] = s.readInt();
00195 }
00196
00197 s.close();
00198 }
00199 catch(Exception e)
00200 {
00201 throw new RuntimeException("Unable to read parser.dat.");
00202 }
00203 }
00204 }
00205 protected void filter() throws ParserException, LexerException, IOException
00206 {
00207 }
00208 private int goTo(int index)
00209 {
00210 int state = state();
00211 int low = 1;
00212 int high = gotoTable[index].length - 1;
00213 int value = gotoTable[index][0][1];
00214
00215 while(low <= high)
00216 {
00217 int middle = (low + high) / 2;
00218
00219 if(state < gotoTable[index][middle][0])
00220 {
00221 high = middle - 1;
00222 }
00223 else if(state > gotoTable[index][middle][0])
00224 {
00225 low = middle + 1;
00226 }
00227 else
00228 {
00229 value = gotoTable[index][middle][1];
00230 break;
00231 }
00232 }
00233
00234 return value;
00235 }
00236 private int index(Switchable token)
00237 {
00238 converter.index = -1;
00239 token.apply(converter);
00240 return converter.index;
00241 }
00242 Node new0()
00243 {
00244 XPAssertion node3 = null;
00245 XPComment node2 = null;
00246 PName node1 = null;
00247 AUnit node = new AUnit(node1, node2, node3);
00248 return node;
00249 }
00250 Node new1()
00251 {
00252 XPAssertion node3 = null;
00253 XPComment node2 = null;
00254 PName node1 = (PName) pop();
00255 AUnit node = new AUnit(node1, node2, node3);
00256 return node;
00257 }
00258 Node new10()
00259 {
00260 XPAssertion node3 = (XPAssertion) pop();
00261 XPComment node2 = (XPComment) pop();
00262 PName node1 = null;
00263 AUnit node = new AUnit(node1, node2, node3);
00264 return node;
00265 }
00266 Node new11()
00267 {
00268 XPAssertion node3 = (XPAssertion) pop();
00269 XPComment node2 = (XPComment) pop();
00270 PName node1 = (PName) pop();
00271 AUnit node = new AUnit(node1, node2, node3);
00272 return node;
00273 }
00274 Node new12()
00275 {
00276 TEndOfLineComment node1 = (TEndOfLineComment) pop();
00277 AEndOfLineComment node = new AEndOfLineComment(node1);
00278 return node;
00279 }
00280 Node new13()
00281 {
00282 XPComment node5 = null;
00283 TSemicolon node4 = (TSemicolon) pop();
00284 TColon node3 = (TColon) pop();
00285 TId node2 = (TId) pop();
00286 TPre node1 = (TPre) pop();
00287 APreAssertion node = new APreAssertion(node1, node2, node3, node4, node5);
00288 return node;
00289 }
00290 Node new14()
00291 {
00292 XPComment node5 = (XPComment) pop();
00293 TSemicolon node4 = (TSemicolon) pop();
00294 TColon node3 = (TColon) pop();
00295 TId node2 = (TId) pop();
00296 TPre node1 = (TPre) pop();
00297 APreAssertion node = new APreAssertion(node1, node2, node3, node4, node5);
00298 return node;
00299 }
00300 Node new15()
00301 {
00302 XPComment node5 = null;
00303 TSemicolon node4 = (TSemicolon) pop();
00304 TColon node3 = (TColon) pop();
00305 TId node2 = (TId) pop();
00306 TPost node1 = (TPost) pop();
00307 APostAssertion node = new APostAssertion(node1, node2, node3, node4, node5);
00308 return node;
00309 }
00310 Node new16()
00311 {
00312 XPComment node5 = (XPComment) pop();
00313 TSemicolon node4 = (TSemicolon) pop();
00314 TColon node3 = (TColon) pop();
00315 TId node2 = (TId) pop();
00316 TPost node1 = (TPost) pop();
00317 APostAssertion node = new APostAssertion(node1, node2, node3, node4, node5);
00318 return node;
00319 }
00320 Node new17()
00321 {
00322 XPComment node8 = null;
00323 TSemicolon node7 = (TSemicolon) pop();
00324 TColon node6 = (TColon) pop();
00325 TId node5 = (TId) pop();
00326 TRBracket node4 = (TRBracket) pop();
00327 TId node3 = (TId) pop();
00328 TLBracket node2 = (TLBracket) pop();
00329 TLocation node1 = (TLocation) pop();
00330 ALocationAssertion node = new ALocationAssertion(node1, node2, node3, node4, node5, node6, node7, node8);
00331 return node;
00332 }
00333 Node new18()
00334 {
00335 XPComment node8 = (XPComment) pop();
00336 TSemicolon node7 = (TSemicolon) pop();
00337 TColon node6 = (TColon) pop();
00338 TId node5 = (TId) pop();
00339 TRBracket node4 = (TRBracket) pop();
00340 TId node3 = (TId) pop();
00341 TLBracket node2 = (TLBracket) pop();
00342 TLocation node1 = (TLocation) pop();
00343 ALocationAssertion node = new ALocationAssertion(node1, node2, node3, node4, node5, node6, node7, node8);
00344 return node;
00345 }
00346 Node new19()
00347 {
00348 TId node1 = (TId) pop();
00349 ASimpleName node = new ASimpleName(node1);
00350 return node;
00351 }
00352 Node new2()
00353 {
00354 XPAssertion node3 = null;
00355 XPComment node2 = (XPComment) pop();
00356 PName node1 = null;
00357 AUnit node = new AUnit(node1, node2, node3);
00358 return node;
00359 }
00360 Node new20()
00361 {
00362 TId node3 = (TId) pop();
00363 TDot node2 = (TDot) pop();
00364 PName node1 = (PName) pop();
00365 AQualifiedName node = new AQualifiedName(node1, node2, node3);
00366 return node;
00367 }
00368 Node new3()
00369 {
00370 PComment node2 = (PComment) pop();
00371 XPComment node1 = (XPComment) pop();
00372 X1PComment node = new X1PComment(node1, node2);
00373 return node;
00374 }
00375 Node new4()
00376 {
00377 PComment node1 = (PComment) pop();
00378 X2PComment node = new X2PComment(node1);
00379 return node;
00380 }
00381 Node new5()
00382 {
00383 XPAssertion node3 = null;
00384 XPComment node2 = (XPComment) pop();
00385 PName node1 = (PName) pop();
00386 AUnit node = new AUnit(node1, node2, node3);
00387 return node;
00388 }
00389 Node new6()
00390 {
00391 XPAssertion node3 = (XPAssertion) pop();
00392 XPComment node2 = null;
00393 PName node1 = null;
00394 AUnit node = new AUnit(node1, node2, node3);
00395 return node;
00396 }
00397 Node new7()
00398 {
00399 PAssertion node2 = (PAssertion) pop();
00400 XPAssertion node1 = (XPAssertion) pop();
00401 X1PAssertion node = new X1PAssertion(node1, node2);
00402 return node;
00403 }
00404 Node new8()
00405 {
00406 PAssertion node1 = (PAssertion) pop();
00407 X2PAssertion node = new X2PAssertion(node1);
00408 return node;
00409 }
00410 Node new9()
00411 {
00412 XPAssertion node3 = (XPAssertion) pop();
00413 XPComment node2 = null;
00414 PName node1 = (PName) pop();
00415 AUnit node = new AUnit(node1, node2, node3);
00416 return node;
00417 }
00418 public Start parse() throws ParserException, LexerException, IOException
00419 {
00420 push(0, null, false);
00421
00422 List ign = null;
00423 while(true)
00424 {
00425 while(index(lexer.peek()) == -1)
00426 {
00427 if(ign == null)
00428 {
00429 ign = new TypedLinkedList(NodeCast.instance);
00430 }
00431
00432 ign.add(lexer.next());
00433 }
00434
00435 if(ign != null)
00436 {
00437 ignoredTokens.setIn(lexer.peek(), ign);
00438 ign = null;
00439 }
00440
00441 last_pos = lexer.peek().getPos();
00442 last_line = lexer.peek().getLine();
00443
00444 int index = index(lexer.peek());
00445 action[0] = actionTable[state()][0][1];
00446 action[1] = actionTable[state()][0][2];
00447
00448 int low = 1;
00449 int high = actionTable[state()].length - 1;
00450
00451 while(low <= high)
00452 {
00453 int middle = (low + high) / 2;
00454
00455 if(index < actionTable[state()][middle][0])
00456 {
00457 high = middle - 1;
00458 }
00459 else if(index > actionTable[state()][middle][0])
00460 {
00461 low = middle + 1;
00462 }
00463 else
00464 {
00465 action[0] = actionTable[state()][middle][1];
00466 action[1] = actionTable[state()][middle][2];
00467 break;
00468 }
00469 }
00470
00471 switch(action[0])
00472 {
00473 case SHIFT:
00474 push(action[1], lexer.next(), true);
00475 last_shift = action[1];
00476 break;
00477 case REDUCE:
00478 switch(action[1])
00479 {
00480 case 0: { Node node = new0(); push(goTo(0), node, true); } break;
00481 case 1: { Node node = new1(); push(goTo(0), node, true); } break;
00482 case 2: { Node node = new2(); push(goTo(0), node, true); } break;
00483 case 3: { Node node = new3(); push(goTo(4), node, false); } break;
00484 case 4: { Node node = new4(); push(goTo(4), node, false); } break;
00485 case 5: { Node node = new5(); push(goTo(0), node, true); } break;
00486 case 6: { Node node = new6(); push(goTo(0), node, true); } break;
00487 case 7: { Node node = new7(); push(goTo(5), node, false); } break;
00488 case 8: { Node node = new8(); push(goTo(5), node, false); } break;
00489 case 9: { Node node = new9(); push(goTo(0), node, true); } break;
00490 case 10: { Node node = new10(); push(goTo(0), node, true); } break;
00491 case 11: { Node node = new11(); push(goTo(0), node, true); } break;
00492 case 12: { Node node = new12(); push(goTo(1), node, true); } break;
00493 case 13: { Node node = new13(); push(goTo(2), node, true); } break;
00494 case 14: { Node node = new14(); push(goTo(2), node, true); } break;
00495 case 15: { Node node = new15(); push(goTo(2), node, true); } break;
00496 case 16: { Node node = new16(); push(goTo(2), node, true); } break;
00497 case 17: { Node node = new17(); push(goTo(2), node, true); } break;
00498 case 18: { Node node = new18(); push(goTo(2), node, true); } break;
00499 case 19: { Node node = new19(); push(goTo(3), node, true); } break;
00500 case 20: { Node node = new20(); push(goTo(3), node, true); } break;
00501 }
00502 break;
00503 case ACCEPT:
00504 {
00505 EOF node2 = (EOF) lexer.next();
00506 PUnit node1 = (PUnit) pop();
00507 Start node = new Start(node1, node2);
00508 return node;
00509 }
00510 case ERROR:
00511 throw new ParserException(
00512 "[" + last_line + "," + last_pos + "] " +
00513 errorMessages[errors[action[1]]]);
00514 }
00515 }
00516 }
00517 private Node pop()
00518 {
00519 return (Node) ((State) stack.previous()).node;
00520 }
00521 private void push(int state, Node node, boolean filter) throws ParserException, LexerException, IOException
00522 {
00523 this.node = node;
00524
00525 if(filter)
00526 {
00527 filter();
00528 }
00529
00530 if(!stack.hasNext())
00531 {
00532 stack.add(new State(state, this.node));
00533 return;
00534 }
00535
00536 State s = (State) stack.next();
00537 s.state = state;
00538 s.node = this.node;
00539 }
00540 private int state()
00541 {
00542 State s = (State) stack.previous();
00543 stack.next();
00544 return s.state;
00545 }
00546 }