1 21 22 package net.percederberg.grammatica.parser.re; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 27 import net.percederberg.grammatica.parser.LookAheadReader; 28 29 36 class AlternativeElement extends Element { 37 38 41 private Element elem1; 42 43 46 private Element elem2; 47 48 54 public AlternativeElement(Element first, Element second) { 55 elem1 = first; 56 elem2 = second; 57 } 58 59 67 public Object clone() { 68 return new AlternativeElement(elem1, elem2); 69 } 70 71 88 public int match(Matcher m, LookAheadReader input, int start, int skip) 89 throws IOException { 90 91 int length = 0; 92 int length1 = -1; 93 int length2 = -1; 94 int skip1 = 0; 95 int skip2 = 0; 96 97 while (length >= 0 && skip1 + skip2 <= skip) { 98 length1 = elem1.match(m, input, start, skip1); 99 length2 = elem2.match(m, input, start, skip2); 100 if (length1 >= length2) { 101 length = length1; 102 skip1++; 103 } else { 104 length = length2; 105 skip2++; 106 } 107 } 108 return length; 109 } 110 111 117 public void printTo(PrintWriter output, String indent) { 118 output.println(indent + "Alternative 1"); 119 elem1.printTo(output, indent + " "); 120 output.println(indent + "Alternative 2"); 121 elem2.printTo(output, indent + " "); 122 } 123 124 } 125 | Popular Tags |