1 21 22 package net.percederberg.grammatica.parser; 23 24 import java.util.ArrayList ; 25 26 40 class LookAheadSet { 41 42 47 private ArrayList elements = new ArrayList (); 48 49 52 private int maxLength; 53 54 60 public LookAheadSet(int maxLength) { 61 this.maxLength = maxLength; 62 } 63 64 71 public LookAheadSet(int maxLength, LookAheadSet set) { 72 this(maxLength); 73 addAll(set); 74 } 75 76 84 public boolean isEmpty() { 85 return elements.size() == 0; 86 } 87 88 94 public int getMinLength() { 95 Sequence seq; 96 int min = -1; 97 98 for (int i = 0; i < elements.size(); i++) { 99 seq = (Sequence) elements.get(i); 100 if (min < 0 || seq.length() < min) { 101 min = seq.length(); 102 } 103 } 104 return (min < 0) ? 0 : min; 105 } 106 107 113 public int getMaxLength() { 114 Sequence seq; 115 int max = 0; 116 117 for (int i = 0; i < elements.size(); i++) { 118 seq = (Sequence) elements.get(i); 119 if (seq.length() > max) { 120 max = seq.length(); 121 } 122 } 123 return max; 124 } 125 126 132 public int[] getInitialTokens() { 133 ArrayList list = new ArrayList (); 134 int[] result; 135 Integer token; 136 int i; 137 138 for (i = 0; i < elements.size(); i++) { 139 token = ((Sequence) elements.get(i)).getToken(0); 140 if (token != null && !list.contains(token)) { 141 list.add(token); 142 } 143 } 144 result = new int[list.size()]; 145 for (i = 0; i < list.size(); i++) { 146 result[i] = ((Integer ) list.get(i)).intValue(); 147 } 148 return result; 149 } 150 151 158 public boolean isRepetitive() { 159 Sequence seq; 160 161 for (int i = 0; i < elements.size(); i++) { 162 seq = (Sequence) elements.get(i); 163 if (seq.isRepetitive()) { 164 return true; 165 } 166 } 167 return false; 168 } 169 170 179 public boolean isNext(Parser parser) { 180 Sequence seq; 181 182 for (int i = 0; i < elements.size(); i++) { 183 seq = (Sequence) elements.get(i); 184 if (seq.isNext(parser)) { 185 return true; 186 } 187 } 188 return false; 189 } 190 191 201 public boolean isNext(Parser parser, int length) { 202 Sequence seq; 203 204 for (int i = 0; i < elements.size(); i++) { 205 seq = (Sequence) elements.get(i); 206 if (seq.isNext(parser, length)) { 207 return true; 208 } 209 } 210 return false; 211 } 212 213 226 public boolean hasOverlap(LookAheadSet set) { 227 for (int i = 0; i < elements.size(); i++) { 228 if (set.hasOverlap((Sequence) elements.get(i))) { 229 return true; 230 } 231 } 232 return false; 233 } 234 235 248 private boolean hasOverlap(Sequence seq) { 249 Sequence elem; 250 251 for (int i = 0; i < elements.size(); i++) { 252 elem = (Sequence) elements.get(i); 253 if (seq.startsWith(elem) || elem.startsWith(seq)) { 254 return true; 255 } 256 } 257 return false; 258 } 259 260 271 public boolean hasIntersection(LookAheadSet set) { 272 for (int i = 0; i < elements.size(); i++) { 273 if (set.contains((Sequence) elements.get(i))) { 274 return true; 275 } 276 } 277 return false; 278 } 279 280 289 private boolean contains(Sequence elem) { 290 return findSequence(elem) != null; 291 } 292 293 301 private Sequence findSequence(Sequence elem) { 302 for (int i = 0; i < elements.size(); i++) { 303 if (elements.get(i).equals(elem)) { 304 return (Sequence) elements.get(i); 305 } 306 } 307 return null; 308 } 309 310 318 private void add(Sequence seq) { 319 if (seq.length() > maxLength) { 320 seq = new Sequence(maxLength, seq); 321 } 322 if (!contains(seq)) { 323 elements.add(seq); 324 } 325 } 326 327 333 public void add(int token) { 334 add(new Sequence(false, token)); 335 } 336 337 343 public void addAll(LookAheadSet set) { 344 for (int i = 0; i < set.elements.size(); i++) { 345 add((Sequence) set.elements.get(i)); 346 } 347 } 348 349 353 public void addEmpty() { 354 add(new Sequence()); 355 } 356 357 362 private void remove(Sequence seq) { 363 elements.remove(seq); 364 } 365 366 372 public void removeAll(LookAheadSet set) { 373 for (int i = 0; i < set.elements.size(); i++) { 374 remove((Sequence) set.elements.get(i)); 375 } 376 } 377 378 388 public LookAheadSet createNextSet(int token) { 389 LookAheadSet result = new LookAheadSet(maxLength - 1); 390 Sequence seq; 391 Integer value; 392 393 for (int i = 0; i < elements.size(); i++) { 394 seq = (Sequence) elements.get(i); 395 value = seq.getToken(0); 396 if (value != null && value.intValue() == token) { 397 result.add(seq.subsequence(1)); 398 } 399 } 400 return result; 401 } 402 403 413 public LookAheadSet createIntersection(LookAheadSet set) { 414 LookAheadSet result = new LookAheadSet(maxLength); 415 Sequence seq1; 416 Sequence seq2; 417 418 for (int i = 0; i < elements.size(); i++) { 419 seq1 = (Sequence) elements.get(i); 420 seq2 = set.findSequence(seq1); 421 if (seq2 != null && seq1.isRepetitive()) { 422 result.add(seq2); 423 } else if (seq2 != null) { 424 result.add(seq1); 425 } 426 } 427 return result; 428 } 429 430 441 public LookAheadSet createCombination(LookAheadSet set) { 442 LookAheadSet result = new LookAheadSet(maxLength); 443 Sequence first; 444 Sequence second; 445 446 if (this.isEmpty()) { 448 return set; 449 } else if (set.isEmpty()) { 450 return this; 451 } 452 453 for (int i = 0; i < elements.size(); i++) { 455 first = (Sequence) elements.get(i); 456 if (first.length() >= maxLength) { 457 result.add(first); 458 } else if (first.length() <= 0) { 459 result.addAll(set); 460 } else { 461 for (int j = 0; j < set.elements.size(); j++) { 462 second = (Sequence) set.elements.get(j); 463 result.add(first.concat(maxLength, second)); 464 } 465 } 466 } 467 return result; 468 } 469 470 479 public LookAheadSet createOverlaps(LookAheadSet set) { 480 LookAheadSet result = new LookAheadSet(maxLength); 481 Sequence seq; 482 483 for (int i = 0; i < elements.size(); i++) { 484 seq = (Sequence) elements.get(i); 485 if (set.hasOverlap(seq)) { 486 result.add(seq); 487 } 488 } 489 return result; 490 } 491 492 501 public LookAheadSet createFilter(LookAheadSet set) { 502 LookAheadSet result = new LookAheadSet(maxLength); 503 Sequence first; 504 Sequence second; 505 506 if (this.isEmpty() || set.isEmpty()) { 508 return this; 509 } 510 511 for (int i = 0; i < elements.size(); i++) { 513 first = (Sequence) elements.get(i); 514 for (int j = 0; j < set.elements.size(); j++) { 515 second = (Sequence) set.elements.get(j); 516 if (first.startsWith(second)) { 517 result.add(first.subsequence(second.length())); 518 } 519 } 520 } 521 return result; 522 } 523 524 530 public LookAheadSet createRepetitive() { 531 LookAheadSet result = new LookAheadSet(maxLength); 532 Sequence seq; 533 534 for (int i = 0; i < elements.size(); i++) { 535 seq = (Sequence) elements.get(i); 536 if (seq.isRepetitive()) { 537 result.add(seq); 538 } else { 539 result.add(new Sequence(true, seq)); 540 } 541 } 542 return result; 543 } 544 545 550 public String toString() { 551 return toString(null); 552 } 553 554 561 public String toString(Tokenizer tokenizer) { 562 StringBuffer buffer = new StringBuffer (); 563 Sequence seq; 564 565 buffer.append("{"); 566 for (int i = 0; i < elements.size(); i++) { 567 seq = (Sequence) elements.get(i); 568 buffer.append("\n "); 569 buffer.append(seq.toString(tokenizer)); 570 } 571 buffer.append("\n}"); 572 return buffer.toString(); 573 } 574 575 576 584 private class Sequence { 585 586 590 private boolean repeat = false; 591 592 595 private ArrayList tokens = null; 596 597 601 public Sequence() { 602 this.repeat = false; 603 this.tokens = new ArrayList (0); 604 } 605 606 612 public Sequence(boolean repeat, int token) { 613 this.repeat = false; 614 this.tokens = new ArrayList (1); 615 this.tokens.add(new Integer (token)); 616 } 617 618 627 public Sequence(int length, Sequence seq) { 628 this.repeat = seq.repeat; 629 this.tokens = new ArrayList (length); 630 if (seq.length() < length) { 631 length = seq.length(); 632 } 633 for (int i = 0; i < length; i++) { 634 tokens.add(seq.tokens.get(i)); 635 } 636 } 637 638 646 public Sequence(boolean repeat, Sequence seq) { 647 this.repeat = repeat; 648 this.tokens = seq.tokens; 649 } 650 651 656 public int length() { 657 return tokens.size(); 658 } 659 660 667 public Integer getToken(int pos) { 668 if (pos >= 0 && pos < tokens.size()) { 669 return (Integer ) tokens.get(pos); 670 } else { 671 return null; 672 } 673 } 674 675 685 public boolean equals(Object obj) { 686 if (obj instanceof Sequence) { 687 return tokens.equals(((Sequence) obj).tokens); 688 } else { 689 return false; 690 } 691 } 692 693 703 public boolean startsWith(Sequence seq) { 704 if (length() < seq.length()) { 705 return false; 706 } 707 for (int i = 0; i < seq.tokens.size(); i++) { 708 if (!tokens.get(i).equals(seq.tokens.get(i))) { 709 return false; 710 } 711 } 712 return true; 713 } 714 715 722 public boolean isRepetitive() { 723 return repeat; 724 } 725 726 735 public boolean isNext(Parser parser) { 736 Token token; 737 Integer id; 738 739 for (int i = 0; i < tokens.size(); i++) { 740 id = (Integer ) tokens.get(i); 741 token = parser.peekToken(i); 742 if (token == null || token.getId() != id.intValue()) { 743 return false; 744 } 745 } 746 return true; 747 } 748 749 759 public boolean isNext(Parser parser, int length) { 760 Token token; 761 Integer id; 762 763 if (length > tokens.size()) { 764 length = tokens.size(); 765 } 766 for (int i = 0; i < length; i++) { 767 id = (Integer ) tokens.get(i); 768 token = parser.peekToken(i); 769 if (token == null || token.getId() != id.intValue()) { 770 return false; 771 } 772 } 773 return true; 774 } 775 776 781 public String toString() { 782 return toString(null); 783 } 784 785 792 public String toString(Tokenizer tokenizer) { 793 StringBuffer buffer = new StringBuffer (); 794 String str; 795 Integer id; 796 797 if (tokenizer == null) { 798 buffer.append(tokens.toString()); 799 } else { 800 buffer.append("["); 801 for (int i = 0; i < tokens.size(); i++) { 802 id = (Integer ) tokens.get(i); 803 str = tokenizer.getPatternDescription(id.intValue()); 804 if (i > 0) { 805 buffer.append(" "); 806 } 807 buffer.append(str); 808 } 809 buffer.append("]"); 810 } 811 if (repeat) { 812 buffer.append(" *"); 813 } 814 return buffer.toString(); 815 } 816 817 827 public Sequence concat(int length, Sequence seq) { 828 Sequence res = new Sequence(length, this); 829 830 if (seq.repeat) { 831 res.repeat = true; 832 } 833 length -= this.length(); 834 if (length > seq.length()) { 835 res.tokens.addAll(seq.tokens); 836 } else { 837 for (int i = 0; i < length; i++) { 838 res.tokens.add(seq.tokens.get(i)); 839 } 840 } 841 return res; 842 } 843 844 852 public Sequence subsequence(int start) { 853 Sequence res = new Sequence(length(), this); 854 855 while (start > 0 && res.tokens.size() > 0) { 856 res.tokens.remove(0); 857 start--; 858 } 859 return res; 860 } 861 } 862 } 863 | Popular Tags |