KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > DefaultSyntaxIterator


1 /*
2  * DefaultSyntaxIterator.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: DefaultSyntaxIterator.java,v 1.1.1.1 2002/09/24 16:07:44 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 public class DefaultSyntaxIterator implements SyntaxIterator
25 {
26     private Line line;
27     private int offset;
28     private char[] cachedChars;
29     private Line cachedLine;
30
31     protected DefaultSyntaxIterator(Position pos)
32     {
33         if (pos != null) {
34             line = pos.getLine();
35             offset = pos.getOffset();
36         }
37     }
38
39     public final Position getPosition()
40     {
41         return new Position(line, offset);
42     }
43
44     public final Line getLine()
45     {
46         return line;
47     }
48
49     public final char nextChar()
50     {
51         int limit = line.length();
52         if (offset < limit - 1) {
53             if (cachedLine != line) {
54                 cachedChars = hideSyntacticWhitespace(line);
55                 cachedLine = line;
56             }
57             while (++offset < limit) {
58                 if (cachedChars[offset] > ' ')
59                     return cachedChars[offset];
60             }
61         }
62         while (true) {
63             if (line.next() == null)
64                 return DONE;
65             line = line.next();
66             offset = 0;
67             cachedChars = hideSyntacticWhitespace(line);
68             cachedLine = line;
69             limit = line.length();
70             while (offset < limit) {
71                 if (cachedChars[offset] > ' ')
72                     return cachedChars[offset];
73                 ++offset;
74             }
75         }
76     }
77
78     public final char prevChar()
79     {
80         if (offset > 0) {
81             if (cachedLine != line) {
82                 cachedChars = hideSyntacticWhitespace(line);
83                 cachedLine = line;
84             }
85             while (--offset >= 0) {
86                 if (cachedChars[offset] > ' ')
87                     return cachedChars[offset];
88             }
89         }
90         while (true) {
91             if (line.previous() == null) {
92                 offset = 0;
93                 return DONE;
94             }
95             line = line.previous();
96             cachedChars = hideSyntacticWhitespace(line);
97             cachedLine = line;
98             offset = line.length();
99             while (--offset >= 0) {
100                 if (cachedChars[offset] > ' ')
101                     return cachedChars[offset];
102             }
103         }
104     }
105
106     // Default implementation. Subclasses can override this method to examine
107
// the line flags.
108
public char[] hideSyntacticWhitespace(Line line)
109     {
110         return hideSyntacticWhitespace(line.getText());
111     }
112
113     public char[] hideSyntacticWhitespace(String JavaDoc s)
114     {
115         return s.toCharArray();
116     }
117 }
118
Popular Tags