KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > parser > re > AlternativeElement


1 /*
2  * AlternativeElement.java
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1
7  * of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307, USA.
18  *
19  * Copyright (c) 2003-2005 Per Cederberg. All rights reserved.
20  */

21
22 package net.percederberg.grammatica.parser.re;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26
27 import net.percederberg.grammatica.parser.LookAheadReader;
28
29 /**
30  * A regular expression alternative element. This element matches the
31  * longest alternative element.
32  *
33  * @author Per Cederberg, <per at percederberg dot net>
34  * @version 1.5
35  */

36 class AlternativeElement extends Element {
37
38     /**
39      * The first alternative element.
40      */

41     private Element elem1;
42
43     /**
44      * The second alternative element.
45      */

46     private Element elem2;
47
48     /**
49      * Creates a new alternative element.
50      *
51      * @param first the first alternative
52      * @param second the second alternative
53      */

54     public AlternativeElement(Element first, Element second) {
55         elem1 = first;
56         elem2 = second;
57     }
58
59     /**
60      * Creates a copy of this element. The copy will be an instance
61      * of the same class matching the same strings. Copies of elements
62      * are necessary to allow elements to cache intermediate results
63      * while matching strings without interfering with other threads.
64      *
65      * @return a copy of this element
66      */

67     public Object JavaDoc clone() {
68         return new AlternativeElement(elem1, elem2);
69     }
70
71     /**
72      * Returns the length of a matching string starting at the
73      * specified position. The number of matches to skip can also be
74      * specified, but numbers higher than zero (0) cause a failed
75      * match for any element that doesn't attempt to combine other
76      * elements.
77      *
78      * @param m the matcher being used
79      * @param input the input character stream to match
80      * @param start the starting position
81      * @param skip the number of matches to skip
82      *
83      * @return the length of the longest matching string, or
84      * -1 if no match was found
85      *
86      * @throws IOException if a I/O error occurred
87      */

88     public int match(Matcher m, LookAheadReader input, int start, int skip)
89         throws IOException JavaDoc {
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     /**
112      * Prints this element to the specified output stream.
113      *
114      * @param output the output stream to use
115      * @param indent the current indentation
116      */

117     public void printTo(PrintWriter JavaDoc output, String JavaDoc 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