KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CombineElement.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 combination element. This element matches two
31  * consecutive elements.
32  *
33  * @author Per Cederberg, <per at percederberg dot net>
34  * @version 1.5
35  */

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

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

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

54     public CombineElement(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 CombineElement(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 length1 = -1;
92         int length2 = 0;
93         int skip1 = 0;
94         int skip2 = 0;
95
96         while (skip >= 0) {
97             length1 = elem1.match(m, input, start, skip1);
98             if (length1 < 0) {
99                 return -1;
100             }
101             length2 = elem2.match(m, input, start + length1, skip2);
102             if (length2 < 0) {
103                 skip1++;
104                 skip2 = 0;
105             } else {
106                 skip2++;
107                 skip--;
108             }
109         }
110
111         return length1 + length2;
112     }
113
114     /**
115      * Prints this element to the specified output stream.
116      *
117      * @param output the output stream to use
118      * @param indent the current indentation
119      */

120     public void printTo(PrintWriter JavaDoc output, String JavaDoc indent) {
121         elem1.printTo(output, indent);
122         elem2.printTo(output, indent);
123     }
124
125 }
126
Popular Tags