KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > jrexx > regex > PScanner


1 /*
2 * 01/07/2003 - 15:19:32
3 *
4 * PScanner.java -
5 * Copyright (C) 2003 Buero fuer Softwarearchitektur GbR
6 * ralf.meyer@karneim.com
7 * http://jrexx.sf.net
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */

23 package com.tc.jrexx.regex;
24
25 import java.util.Vector JavaDoc;
26 import java.text.ParsePosition JavaDoc;
27
28 class PScanner {
29     public static final int UNLIMITED_MAX_LENGTH = Integer.MAX_VALUE;
30
31     private final Automaton_Pattern.TerminalFormat[] terminalFormats;
32     private final int[] terminalsMaxLength;
33     private final boolean terminalFormatsAreExclusive;
34
35     public PScanner(Automaton_Pattern.TerminalFormat[] terminalFormats) {
36         this(terminalFormats,false);
37     }
38
39     public PScanner(Automaton_Pattern.TerminalFormat[] terminalFormats, boolean terminalFormatsAreExclusive) {
40         this.terminalFormats = terminalFormats;
41         this.terminalFormatsAreExclusive = terminalFormatsAreExclusive;
42
43         final int n = this.terminalFormats.length;
44         if (!this.terminalFormatsAreExclusive) {
45             // reverse terminalFormats list
46
for (int i= (n-1)>>1; i>=0; --i) {
47                 Automaton_Pattern.TerminalFormat temp = this.terminalFormats[i];
48                 this.terminalFormats[i] = this.terminalFormats[n-i];
49                 this.terminalFormats[n-i] = temp;
50             }
51         }
52         this.terminalsMaxLength = new int[n];
53         for (int i=0; i<n; i++) {
54             this.terminalsMaxLength[i] = this.terminalFormats[i].maxLength();
55         }
56     }
57
58
59     public Vector JavaDoc scan(String JavaDoc source) {
60         return this.scan(source,0);
61     }
62
63
64     public Vector JavaDoc scan(String JavaDoc source, int startIndex) {
65         if (source==null) {
66             String JavaDoc message = "null source specified";
67             throw new IllegalArgumentException JavaDoc(message);
68         }
69
70         final char[] input = source.toCharArray();
71
72         int firstIndexOfTerminalFormats = -1;
73         int lastIndexOfTerminalFormats = -1;
74
75         for (int i=this.terminalFormats.length-1; i>=0; i--) {
76             if (this.terminalFormats[i]!=null) {
77                 lastIndexOfTerminalFormats = i;
78                 break;
79             }
80         }
81
82         if (lastIndexOfTerminalFormats==-1) {
83             String JavaDoc message = "no terminal formats added";
84             throw new NullPointerException JavaDoc(message);
85         }
86
87         for (int i=0; i<=lastIndexOfTerminalFormats; i++) {
88             if (this.terminalFormats[i]!=null) {
89                 firstIndexOfTerminalFormats = i;
90                 break;
91             }
92         }
93
94 // System.out.println("Scanner start on: "+new String(input,startIndex,input.length-startIndex));
95
final Vector JavaDoc tokenList = new Vector JavaDoc();
96         final int inputLength = input.length;
97         final ParsePosition JavaDoc pos = new ParsePosition JavaDoc(startIndex);
98         int index = startIndex;
99         while (index<inputLength) {
100             int longestMatch = -1;
101             Object JavaDoc lastToken = null, token;
102             for (int i=lastIndexOfTerminalFormats; i>=firstIndexOfTerminalFormats; i--) {
103                 if (this.terminalsMaxLength[i]>=longestMatch) {
104                     pos.setIndex(index);
105
106 //System.out.print(this.terminalFormats[i].getClass().getName().substring(this.terminalFormats[i].getClass().getName().indexOf(".")+1));
107
//System.out.print(".scan("+new String(input,pos.getIndex(),input.length-pos.getIndex())+") -> ");
108
token = this.terminalFormats[i].parseObject(input,pos);
109 //System.out.println(token+" -> "+new String(input,pos.getIndex(),input.length-pos.getIndex()));
110
final int matchLength = pos.getIndex()-index;
111                     if (token!=null) {
112                         if (this.terminalFormatsAreExclusive) {
113                             longestMatch = matchLength;
114                             lastToken = token;
115                             break;
116                         } else {
117                             if (matchLength>=longestMatch) {
118                                 longestMatch = matchLength;
119                                 lastToken = token;
120                             }
121                         }
122                     }
123                 }
124             }
125 //if (lastToken!=null) System.out.println("Token recognized: "+lastToken);
126
if (lastToken!=null) tokenList.addElement(lastToken);
127             else {
128                 String JavaDoc message = "can not scan input:"
129                             +"\n"+new String JavaDoc(input,startIndex,input.length-startIndex)
130                             +"\nerrorPosition: "+index
131                             +"\n"+new String JavaDoc(input,index,input.length-index);
132                 throw new ParseException(message);
133             }
134             index+= longestMatch;
135         }
136
137         return tokenList;
138     }
139
140     public String JavaDoc toString() {
141         StringBuffer JavaDoc answer = new StringBuffer JavaDoc();
142         answer.append("Scanner(");
143         if (this.terminalFormatsAreExclusive) answer.append("exclusive");
144         answer.append(")");
145         for (int i=0; i<this.terminalFormats.length; i++)
146             if (this.terminalFormats[i]!=null)
147                 answer.append('\n').append(this.terminalFormats[i]);
148         return answer.toString();
149     }
150
151 }
Popular Tags