KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > piaget > analyze > Scanner


1 /*
2  * Scanner.java
3  *
4  * Created on April 12, 2005, 8:05 PM
5  */

6
7 package org.netbeans.modules.piaget.analyze;
8
9 import java.io.InputStreamReader JavaDoc;
10 import java.io.IOException JavaDoc;
11
12
13 /**
14  *
15  * @author loicsegapelli
16  *
17  * Opens a file and gets content as tokens.
18  */

19 class Scanner {
20
21     public static final int EOF = -1;
22     public static final int WORD = 1;
23     public static final int LONG = 2;
24     public int lastReturned;
25     
26     /** This character represents the end of the input.
27      */

28     private static final char EOF_CH = (char)-1;
29
30     private boolean pushed;
31
32     //protected int start;
33

34     public String JavaDoc sval;
35     public long lval;
36
37     private StringBuffer JavaDoc buf;
38
39     /** The current character.
40      */

41     private char ch;
42
43     private int line = 1;
44
45
46     
47     /** The input stream providing the input characters.
48      */

49     private final InputStreamReader JavaDoc in;
50
51     public Scanner(InputStreamReader JavaDoc in) {
52         this.in = in;
53         pushed = false;
54         nextCh();
55     }
56
57     /** close the file input stream
58      */

59     public void close() throws IOException JavaDoc {
60         in.close();
61     }
62     
63     public void pushBack(){
64         pushed = true;
65     }
66
67
68     public int nextToken() {
69         if(pushed){
70             pushed = false;
71             return lastReturned;
72         }
73         
74         buf = new StringBuffer JavaDoc();
75         while (!interestingChar()) nextCh();
76         if(ch == EOF_CH) return EOF;
77         while(interestingChar()){
78             buf.append(ch);
79             nextCh();
80         }
81         
82         if(buf.length() == 0){
83             nextToken();
84         }
85         
86         
87         String JavaDoc result = buf.toString();
88         
89         try{
90             long l = Long.parseLong(result);
91             lval = l;
92             lastReturned = LONG;
93             return lastReturned;
94         }catch (NumberFormatException JavaDoc e){
95             sval = result;
96             lastReturned = WORD;
97             return lastReturned;
98         }
99         
100     }
101     
102     private boolean interestingChar(){
103         return ch != '\n' && ch != '\r' && ch !='\t' && !Character.isWhitespace(ch);
104     }
105
106     public int lineno(){
107         return line;
108     }
109     
110     /** Puts the next character into 'ch' and updates the current position.
111      */

112     private void nextCh() {
113         switch (ch) {
114             case EOF_CH:
115                 return;
116             case '\n':
117                 line++;
118                 break;
119             default:
120                 break;
121         }
122         try {
123             ch = (char)in.read();
124             oldch = ((oldch == '\r') && (ch == '\n')) ? (char)in.read() : ch;
125             ch = (oldch == '\r') ? '\n' : oldch;
126         } catch (IOException JavaDoc e) {
127             e.printStackTrace(System.out);
128         }
129     }
130
131     private char oldch;
132 }
133
Popular Tags