changeset 11:79d492bce828

clean up
author one
date Thu, 02 Sep 2010 11:55:56 +0900
parents 0d74081c1309
children fbc80bc6204c
files src/plparser/DictProperty.java src/plparser/PLCharScannerImpl.java src/plparser/PLScanner.java src/plparser/PLScannerImpl.java src/plparser/PropertyListCharTokenizer.java src/plparser/PropertyListParser.java src/plparser/PropertyListScanner.java src/plparser/PropertyListStreamScanner.java src/plparser/PropertyListStreamTokenizer.java src/plparser/TestScanner.java
diffstat 10 files changed, 152 insertions(+), 137 deletions(-) [+]
line wrap: on
line diff
--- a/src/plparser/DictProperty.java	Thu Sep 02 10:52:44 2010 +0900
+++ b/src/plparser/DictProperty.java	Thu Sep 02 11:55:56 2010 +0900
@@ -3,7 +3,6 @@
 import java.io.PrintStream;
 import java.util.HashMap;
 import java.util.LinkedList;
-import java.util.Set;
 
 public class DictProperty extends Property {
 	HashMap<Property, Property> map;
--- /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
--- a/src/plparser/PLScanner.java	Thu Sep 02 10:52:44 2010 +0900
+++ b/src/plparser/PLScanner.java	Thu Sep 02 11:55:56 2010 +0900
@@ -35,5 +35,6 @@
 	public Iterable<Token<Node>> scanToken(String exp);
 
 	public Iterable<Token<Node>> scanToken(FileReader file);
+	public Dictionary<Node> dictionary();
 
 }
\ No newline at end of file
--- a/src/plparser/PLScannerImpl.java	Thu Sep 02 10:52:44 2010 +0900
+++ b/src/plparser/PLScannerImpl.java	Thu Sep 02 11:55:56 2010 +0900
@@ -18,14 +18,24 @@
 	public String prompt;
 	public static final int BufferSize = 4096;
 
-	public PLScannerImpl() {
-		super();
+	/**
+	 * Construtor for push Scanner
+	 * @param p previous scanner
+	 * @param dict dictionary
+	 * @param nullToken shared null token
+	 */
+	public PLScannerImpl(PLScanner<Node> p,
+			Dictionary<Node> dict, Token<Node> nullToken) {
+		this.prev = p;
+		this.dict = dict; 
+		this.nullToken = nullToken;
 	}
 
-	public PLScannerImpl(PLScannerImpl<Node> plScannerImpl,
-			Dictionary<Node> dict, Token<Node> nullToken) {
-		this.dict = dict; 
-		this.nullToken = nullToken;
+	
+	public PLScannerImpl() {
+		this.dict = new Dictionary<Node>();
+		this.nullToken = new Token<Node>("",TokenID.NULL);
+		this.prev = null;
 	}
 
 
@@ -101,4 +111,8 @@
 		return prev;
 	}
 
+	public Dictionary<Node> dictionary() {
+		return dict;
+	}
+
 }
\ No newline at end of file
--- a/src/plparser/PropertyListCharTokenizer.java	Thu Sep 02 10:52:44 2010 +0900
+++ b/src/plparser/PropertyListCharTokenizer.java	Thu Sep 02 11:55:56 2010 +0900
@@ -4,19 +4,16 @@
 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 class PropertyListCharTokenizer<T> extends PLCharScannerImpl<T> implements PLScanner<T> {
 
 	public PropertyListCharTokenizer(
 			PLScanner<T>s,
 			Dictionary<T> dict, Token<T> nullToken) {
-		super(dict);
-		this.prev = s;
-		this.nullToken = nullToken;
+		super(s,dict,nullToken);
+	}
+
+	public PropertyListCharTokenizer() {
+		super();
 	}
 
 	public char ch;
@@ -37,7 +34,7 @@
 				while(hasRemaining()&&Character.isJavaIdentifierPart((ch=nextChar()))) {
 					w.put(ch);
 				}
-				return lookupDict(w);
+				return lookupDict(w,TokenID.VARIABLE);
 			} else if (Character.isDigit(ch)) { // should handle more complex case
 				w.put(ch);
 				while(hasRemaining()&&Character.isDigit((ch=nextChar()))) {
@@ -69,40 +66,24 @@
 				while(hasRemaining() && (ch=nextChar())!=d) w.put(ch);
 				if (!hasRemaining())return nullToken; // non terminate string
 				ch = nextChar();
-				Token<T> t = lookupDict(w);
+				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);
+				nextToken = lookupDict(w,TokenID.Any);
 				if (!hasRemaining())return nextToken; 
 				ch = nextChar();
 				return nextToken;
 			default:
-				ch = nextChar();
+				ch = nextChar(); // unrecognized char
 				continue;
 			}
 		}
 	}
 
-	private Token<T> lookupDict(CharBuffer w) {
-		Token<T> t;
-		String s = w.flip().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();
-		if (ch=='\n') lineno++;
-		return ch;
-	}
-
 
 	@Override
 	public PLScanner<T> pushScannerFile(InputStream newfile, String prompt) {
--- a/src/plparser/PropertyListParser.java	Thu Sep 02 10:52:44 2010 +0900
+++ b/src/plparser/PropertyListParser.java	Thu Sep 02 11:55:56 2010 +0900
@@ -26,7 +26,7 @@
 	}
 	
 
-	public void initReservedWord() {
+	private void initReservedWord(Dictionary<Node> dict) {
 		dict.reserve("=",TokenID.Assign);
 		dict.reserve(",",TokenID.Comma);
 		dict.reserve(";",TokenID.Semicolon);
@@ -41,11 +41,11 @@
 	}
 
 	public void initialize() {
-		dict = new Dictionary<Node>();
-	//	scope = new PropertyListScope<Node>(null,dict);
-		initReservedWord();
-		// scanner = new PropertyListScanner<Node>(dict);
-		scanner = new PropertyListCharTokenizer<Node>(dict);
+		scanner = new PropertyListScanner<Node>();
+		// scanner = new PropertyListCharTokenizer<Node>();
+		dict = scanner.dictionary();
+		initReservedWord(dict);
+		//	scope = new PropertyListScope<Node>(null,dict);
 	}
 
 	public Node parse() {
--- a/src/plparser/PropertyListScanner.java	Thu Sep 02 10:52:44 2010 +0900
+++ b/src/plparser/PropertyListScanner.java	Thu Sep 02 11:55:56 2010 +0900
@@ -9,7 +9,7 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-public class PropertyListScanner<Node> extends PLScannerImpl<Node> implements PLScanner<Node> {
+public class PropertyListScanner<Node> extends PLCharScannerImpl<Node> implements PLScanner<Node> {
 
 	//  We cannot make Generic Singleton pattern
 	//	static PropertyListScanner scanner = new PropertyListScanner();
@@ -31,24 +31,18 @@
 	 */
 
 	public Matcher scan;
-	public CharBuffer cb;
-	public PropertyListScanner(Dictionary<Node> dict) {
-		this.dict = dict;
-		nullToken = new Token<Node>("",TokenID.NULL);
-	}
-
 	/*
 	 * Scanner Container for Stack
 	 */
 	public PropertyListScanner(PropertyListScanner<Node> prev, Dictionary<Node> dict, Token<Node> nullToken) {
-		this.prev = prev;
-		this.dict = dict;
-		this.nullToken = nullToken;
+		super(prev,dict,nullToken);
 	}
 
+	public PropertyListScanner() {
+	}
 
 	// Pattern must contain exact 1 group
-	private static Pattern tokenPat = Pattern.compile(
+	static Pattern tokenPat = Pattern.compile(
 			"([={}(),;])"
 	);
 	private static Pattern namePat  = Pattern.compile("([_a-zA-Z][\\@\\w]*)");
@@ -177,76 +171,19 @@
 	}
 
 	@Override
-	public boolean hasRemaining() {
-		return cb.hasRemaining()||extendInput();
-	}
-
-	/*
-	 *    Extend Input data
-	 */
-	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 (but we don't...) 
-					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);
+		super.set(exp);
 		scan = tokenPat.matcher(cb);
-		filename = null; file = null;
-		nextToken = nullToken;
-		return this;
-	}
-
-	/*
-	 * Read From File
-	 *    We cannot read symbol bigger than Buffersize
-	 */
-	@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();
-		}
+		super.set(file);
 		scan = tokenPat.matcher(cb);
-		lineno = 0;
 		return this;
 	}
-
-
+	
 	@Override
 	public PLScanner<Node> pushScannerFile(InputStream newfile, String prompt) {
 		return new PropertyListScanner<Node>(this,dict,nullToken).setFile(newfile,prompt);
--- a/src/plparser/PropertyListStreamScanner.java	Thu Sep 02 10:52:44 2010 +0900
+++ b/src/plparser/PropertyListStreamScanner.java	Thu Sep 02 11:55:56 2010 +0900
@@ -21,17 +21,9 @@
 	public PropertyListStreamScanner(
 			PLScanner<T> s,
 			Dictionary<T> dict, Token<T> nullToken) {
-		this.dict = dict;
-		this.nullToken = nullToken;
+		super(s,dict,nullToken);
 	}
 
-
-	public PropertyListStreamScanner(Dictionary<T> dict) {
-		this.dict = dict;
-		nullToken = new Token<T>("",TokenID.NULL);
-	}
-
-
 	public void init() {
 		String pattern = ".";
 		scan.useDelimiter(pattern);
--- a/src/plparser/PropertyListStreamTokenizer.java	Thu Sep 02 10:52:44 2010 +0900
+++ b/src/plparser/PropertyListStreamTokenizer.java	Thu Sep 02 11:55:56 2010 +0900
@@ -25,19 +25,11 @@
 	public final static char DOUBLE_QUOTE = '"';
 
 	public PropertyListStreamTokenizer(
-			PropertyListStreamTokenizer<T> propertyListStreamTokenizer,
+			PLScanner<T> s,
 			Dictionary<T> dict, Token<T> nullToken) {
-		this.dict = dict;
-		this.nullToken = nullToken;
+		super(s,dict,nullToken);
 	}
 
-
-	public PropertyListStreamTokenizer(Dictionary<T> dict) {
-		this.dict = dict;
-		nullToken = new Token<T>("",TokenID.NULL);
-	}
-
-
 	public void init() {
 		tokenizer.resetSyntax();
 		tokenizer.wordChars('0', '9');
--- a/src/plparser/TestScanner.java	Thu Sep 02 10:52:44 2010 +0900
+++ b/src/plparser/TestScanner.java	Thu Sep 02 11:55:56 2010 +0900
@@ -24,11 +24,10 @@
 	}
 	
 	public static void initScanner() {
-		Dictionary<Property> dict = new Dictionary<Property>();
-		 // scan = new PropertyListScanner<Property>(dict);
-		// scan = new PropertyListStreamTokenizer<Property>(dict);
-		// scan = new PropertyListStreamScanner<Property>(dict);
-		scan = new PropertyListCharTokenizer<Property>(dict);
+		 // scan = new PropertyListScanner<Property>();
+		// scan = new PropertyListStreamTokenizer<Property>();
+		// scan = new PropertyListStreamScanner<Property>();
+		scan = new PropertyListCharTokenizer<Property>();
 	}