2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
34 import gnu.regexp.REException;
35 import gnu.regexp.REMatch;
37 import multex.Failure;
40 * a class to do some basic, regexp based parsing of character data
43 * <p>Description: </p>
44 * <p>Copyright: Copyright (c) 2003</p>
46 * @author not attributable
50 public class SimpleParser {
59 public SimpleParser(String aData) {
66 * @param aRegularExpression
68 * @throws SimpleParserExc
71 public boolean parses(RE aRegularExpression) throws SimpleParserExc {
72 REMatch match = aRegularExpression.getMatch(data, position);
74 return (match!=null && match.getStartIndex()==position) ;
79 * @param aRegularExpression
82 * @throws SimpleParserExc
85 public String parse(RE aRegularExpression, String aMessage) throws SimpleParserExc {
86 REMatch match = aRegularExpression.getMatch(data, position);
88 if (match==null || match.getStartIndex()!=position)
89 throw new SimpleParserExc(aMessage+" at position "+position+" in '"+data+"'");
91 position=match.getEndIndex();
93 return match.toString();
98 * @param aRegularExpression
100 * @throws SimpleParserExc
103 public String parse(RE aRegularExpression) throws SimpleParserExc {
104 return parse( aRegularExpression, "No match found for '"+aRegularExpression.toString()+"'");
109 * @param aRegularExpression
110 * @throws SimpleParserExc
113 public void skip(RE aRegularExpression) throws SimpleParserExc {
114 REMatch match = aRegularExpression.getMatch(data, position);
116 if (match!=null && match.getStartIndex()==position)
117 position=match.getEndIndex();
122 * @param anExpression
124 * @throws SimpleParserExc
127 public boolean parses(String anExpression) throws SimpleParserExc {
129 return parses(new RE(anExpression));
131 catch (SimpleParserExc e) {
134 catch (REException e) {
135 throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
137 catch (Throwable t) {
138 throw new SimpleParserFailure( t );
144 * @param anExpression
146 * @throws SimpleParserExc
147 * @throws SimpleParserFailure
150 public String parse(String anExpression) throws SimpleParserExc, SimpleParserFailure {
152 return parse(new RE(anExpression));
154 catch (SimpleParserExc e) {
157 catch (REException e) {
158 throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
160 catch (Throwable t) {
161 throw new SimpleParserFailure( t );
167 * @param anExpression
170 * @throws SimpleParserExc
171 * @throws SimpleParserFailure
174 public String parse(String anExpression, String aMessage) throws SimpleParserExc, SimpleParserFailure {
176 return parse(new RE(anExpression), aMessage);
178 catch (SimpleParserExc e) {
181 catch (REException e) {
182 throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
184 catch (Throwable t) {
185 throw new SimpleParserFailure( t );
191 * @param anExpression
192 * @throws SimpleParserExc
193 * @throws SimpleParserFailure
196 public void skip(String anExpression) throws SimpleParserExc, SimpleParserFailure {
198 skip(new RE(anExpression));
200 catch (SimpleParserExc e) {
203 catch (REException e) {
204 throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
206 catch (Throwable t) {
207 throw new SimpleParserFailure( t );
213 * @return true if the parser is at the end of the data
216 public boolean isAtEnd() {
217 return position>=data.length();
224 public String remainingData() {
225 return data.substring(position);
231 * <p>Description: </p>
232 * <p>Copyright: Copyright (c) 2003</p>
234 * @author not attributable
238 public static class SimpleParserFailure extends Failure {
239 public SimpleParserFailure(Throwable aThrowable) {
240 super(aThrowable.getMessage(), aThrowable);
243 public SimpleParserFailure(String aMessage, Throwable aThrowable) {
244 super(aMessage, aThrowable);
248 public static class SimpleParserExc extends Exc {
249 public SimpleParserExc(String aMessage) {