Mercurial > hg > Members > kono > PLparser
view src/plparser/PropertyListCharTokenizer.java @ 9:29e309b2f624
Try several Tokenizer
author | one |
---|---|
date | Thu, 02 Sep 2010 10:00:23 +0900 |
parents | |
children | 0d74081c1309 |
line wrap: on
line source
package plparser; import java.io.FileNotFoundException; import java.io.InputStream; import java.nio.CharBuffer; public class PropertyListCharTokenizer<T> extends PropertyListScanner<T> implements PLScanner<T> { public PropertyListCharTokenizer(Dictionary<T> dict) { super(dict); } public PropertyListCharTokenizer( PLScanner<T>s, Dictionary<T> dict, Token<T> nullToken) { super(dict); this.nullToken = nullToken; this.prev = null; } public char ch; @Override public Token<T> nextToken() { nextToken = nullToken; if (cb==null) return nextToken; if (!hasRemaining()) return nextToken; while(Character.isSpaceChar(ch)) { if (!hasRemaining()) return nextToken; ch = nextChar(); } CharBuffer w = CharBuffer.allocate(BufferSize); if (Character.isJavaIdentifierStart(ch)) { w.put(ch); while(hasRemaining()&&Character.isJavaIdentifierPart((ch=nextChar()))) { w.put(ch); } return lookupDict(w); } else if (Character.isDigit(ch)||ch=='-'||ch=='+') { w.put(ch); while(hasRemaining()&&Character.isDigit((ch=nextChar()))) { w.put(ch); } return nextToken = new Token<T>(w.toString(),TokenID.NUMBER); } else if (ch=='/') { w.put(ch); if (!hasRemaining()) return new Token<T>(w.toString(),TokenID.Any); ch = nextChar(); if (ch=='/') { while(hasRemaining() && (ch=nextChar())!='\n'); if (!hasRemaining())return nullToken; ch = nextChar(); return nextToken(); } if (ch=='*') { while(hasRemaining() && !((ch=nextChar())=='*'&&(ch=nextChar())=='/')); if (!hasRemaining())return nullToken; ch = nextChar(); return nextToken(); } return new Token<T>(w.toString(),TokenID.Any); } else if (ch=='\'') { while(hasRemaining() && (ch=nextChar())!='\'') w.put(ch); if (!hasRemaining())return nullToken; // non terminate string ch = nextChar(); return lookupDict(w); } else if (ch=='"') { while(hasRemaining() && (ch=nextChar())!='"') w.put(ch); if (!hasRemaining())return nullToken; // non terminate string ch = nextChar(); return lookupDict(w); } else { nextToken = lookupDict(w); if (!hasRemaining())return nextToken; ch = nextChar(); return nextToken; } } private Token<T> lookupDict(CharBuffer w) { Token<T> t; String s = w.toString(); if ((t = dict.get(s))==null) { dict.put(s, t = new Token<T>(s,TokenID.Any)); } return nextToken = t; } private char nextChar() { if (!cb.hasRemaining()) extendInput(); char ch = cb.get(); return ch; } @Override public PLScanner<T> pushScannerFile(InputStream newfile, String prompt) { return new PropertyListCharTokenizer<T>(this,dict,nullToken).setFile(newfile,prompt); } @Override public PLScanner<T> pushScanner(String exp) { return new PropertyListCharTokenizer<T>(this,dict,nullToken).set(exp); } @Override public PLScanner<T> pushScannerFile(String newfile) throws FileNotFoundException { return new PropertyListCharTokenizer<T>(this,dict,nullToken).setFile(newfile); } }