Mercurial > hg > Members > kono > PLparser
comparison src/plparser/PLCharScannerImpl.java @ 11:79d492bce828
clean up
author | one |
---|---|
date | Thu, 02 Sep 2010 11:55:56 +0900 |
parents | |
children | da8c1569b0c1 |
comparison
equal
deleted
inserted
replaced
10:0d74081c1309 | 11:79d492bce828 |
---|---|
1 package plparser; | |
2 | |
3 import java.io.FileNotFoundException; | |
4 import java.io.FileReader; | |
5 import java.io.IOException; | |
6 import java.io.InputStreamReader; | |
7 import java.nio.CharBuffer; | |
8 | |
9 public abstract class PLCharScannerImpl<Node> extends PLScannerImpl<Node> { | |
10 | |
11 public CharBuffer cb; | |
12 | |
13 | |
14 public PLCharScannerImpl(PLScanner<Node> plScannerImpl, Dictionary<Node> dict, | |
15 Token<Node> nullToken) { | |
16 super(plScannerImpl, dict, nullToken); | |
17 } | |
18 | |
19 public PLCharScannerImpl() { | |
20 super(); | |
21 } | |
22 | |
23 public Token<Node> lookupDict(CharBuffer w, TokenID id) { | |
24 Token<Node> t; | |
25 String s = w.flip().toString(); | |
26 if ((t = dict.get(s))==null) { | |
27 dict.put(s, t = new Token<Node>(s,id)); | |
28 } | |
29 return nextToken = t; | |
30 } | |
31 | |
32 public char nextChar() { | |
33 if (!cb.hasRemaining()) extendInput(); | |
34 char ch = cb.get(); | |
35 if (ch=='\n') lineno++; | |
36 return ch; | |
37 } | |
38 | |
39 @Override | |
40 public boolean hasRemaining() { | |
41 return cb.hasRemaining()||extendInput(); | |
42 } | |
43 | |
44 protected boolean extendInput() { | |
45 if (file!=null && cb.position()!=0) { | |
46 // move remaining data to the top, set position for next read | |
47 cb.compact(); | |
48 try { | |
49 if (prompt!=null) System.out.print(prompt); | |
50 if (file.read(cb)>0) { | |
51 cb.flip(); // prepare for get | |
52 return true; | |
53 } else { | |
54 throw new IOException(); | |
55 } | |
56 } catch (IOException e) { | |
57 file = null ; | |
58 cb.flip(); | |
59 } | |
60 } | |
61 return false; | |
62 } | |
63 | |
64 @Override | |
65 public PLScanner<Node> set(String exp) { | |
66 cb = CharBuffer.wrap(exp); | |
67 filename = null; file = null; | |
68 nextToken = nullToken; | |
69 return this; | |
70 } | |
71 | |
72 @Override | |
73 public PLScanner<Node> setFile(String file) throws FileNotFoundException { | |
74 this.filename = file; | |
75 nextToken = nullToken; | |
76 set(new FileReader(file)); | |
77 return this; | |
78 } | |
79 | |
80 @Override | |
81 public PLScanner<Node> set(InputStreamReader file) { | |
82 this.file = file; | |
83 cb = CharBuffer.allocate(BufferSize); | |
84 try { | |
85 if (prompt!=null) System.out.print(prompt); | |
86 if (file.read(cb) <= 0) { | |
87 throw new IOException(); | |
88 } | |
89 } catch (IOException e) { | |
90 file = null; cb = null; | |
91 set(""); | |
92 return this; | |
93 } finally { | |
94 cb.flip(); | |
95 } | |
96 lineno = 0; | |
97 return this; | |
98 } | |
99 | |
100 } |