Mercurial > hg > Members > kono > PLparser
view src/plparser/PropertyListCharTokenizer.java @ 11:79d492bce828
clean up
author | one |
---|---|
date | Thu, 02 Sep 2010 11:55:56 +0900 |
parents | 0d74081c1309 |
children |
line wrap: on
line source
package plparser; import java.io.FileNotFoundException; import java.io.InputStream; import java.nio.CharBuffer; public class PropertyListCharTokenizer<T> extends PLCharScannerImpl<T> implements PLScanner<T> { public PropertyListCharTokenizer( PLScanner<T>s, Dictionary<T> dict, Token<T> nullToken) { super(s,dict,nullToken); } public PropertyListCharTokenizer() { super(); } public char ch; @Override public Token<T> nextToken() { nextToken = nullToken; if (cb==null) return nextToken; while(true) { 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,TokenID.VARIABLE); } else if (Character.isDigit(ch)) { // should handle more complex case w.put(ch); while(hasRemaining()&&Character.isDigit((ch=nextChar()))) { w.put(ch); } return nextToken = new Token<T>(w.flip().toString(),TokenID.NUMBER); } switch(ch) { case '/': w.put(ch); if (!hasRemaining()) return new Token<T>(w.flip().toString(),TokenID.Any); ch = nextChar(); if (ch=='/') { while(hasRemaining() && (ch=nextChar())!='\n'); if (!hasRemaining())return nullToken; ch = nextChar(); continue; } if (ch=='*') { while(hasRemaining() && !((ch=nextChar())=='*'&&(ch=nextChar())=='/')); if (!hasRemaining())return nullToken; ch = nextChar(); continue; } return new Token<T>(w.flip().toString(),TokenID.Any); case '\'': // should handle '\'' case case '"': char d = ch; while(hasRemaining() && (ch=nextChar())!=d) w.put(ch); if (!hasRemaining())return nullToken; // non terminate string ch = nextChar(); Token<T> t = lookupDict(w,TokenID.VARIABLE); if (t.type!=TokenID.VARIABLE) { t = new Token<T>(t.name,TokenID.VARIABLE); } return nextToken = t; case '{': case '}': case '(': case ')': case '=': case ',': case ';': w.put(ch); nextToken = lookupDict(w,TokenID.Any); if (!hasRemaining())return nextToken; ch = nextChar(); return nextToken; default: ch = nextChar(); // unrecognized char continue; } } } @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); } }