Mercurial > hg > Members > kono > PLparser
diff src/plparser/PLCharScannerImpl.java @ 11:79d492bce828
clean up
author | one |
---|---|
date | Thu, 02 Sep 2010 11:55:56 +0900 |
parents | |
children | da8c1569b0c1 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/plparser/PLCharScannerImpl.java Thu Sep 02 11:55:56 2010 +0900 @@ -0,0 +1,100 @@ +package plparser; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.CharBuffer; + +public abstract class PLCharScannerImpl<Node> extends PLScannerImpl<Node> { + + public CharBuffer cb; + + + public PLCharScannerImpl(PLScanner<Node> plScannerImpl, Dictionary<Node> dict, + Token<Node> nullToken) { + super(plScannerImpl, dict, nullToken); + } + + public PLCharScannerImpl() { + super(); + } + + public Token<Node> lookupDict(CharBuffer w, TokenID id) { + Token<Node> t; + String s = w.flip().toString(); + if ((t = dict.get(s))==null) { + dict.put(s, t = new Token<Node>(s,id)); + } + return nextToken = t; + } + + public char nextChar() { + if (!cb.hasRemaining()) extendInput(); + char ch = cb.get(); + if (ch=='\n') lineno++; + return ch; + } + + @Override + public boolean hasRemaining() { + return cb.hasRemaining()||extendInput(); + } + + protected boolean extendInput() { + if (file!=null && cb.position()!=0) { + // move remaining data to the top, set position for next read + cb.compact(); + try { + if (prompt!=null) System.out.print(prompt); + if (file.read(cb)>0) { + cb.flip(); // prepare for get + return true; + } else { + throw new IOException(); + } + } catch (IOException e) { + file = null ; + cb.flip(); + } + } + return false; + } + + @Override + public PLScanner<Node> set(String exp) { + cb = CharBuffer.wrap(exp); + filename = null; file = null; + nextToken = nullToken; + return this; + } + + @Override + public PLScanner<Node> setFile(String file) throws FileNotFoundException { + this.filename = file; + nextToken = nullToken; + set(new FileReader(file)); + return this; + } + + @Override + public PLScanner<Node> set(InputStreamReader file) { + this.file = file; + cb = CharBuffer.allocate(BufferSize); + try { + if (prompt!=null) System.out.print(prompt); + if (file.read(cb) <= 0) { + throw new IOException(); + } + } catch (IOException e) { + file = null; cb = null; + set(""); + return this; + } finally { + cb.flip(); + } + lineno = 0; + return this; + } + +} \ No newline at end of file