# HG changeset patch # User one # Date 1283396156 -32400 # Node ID 79d492bce8289955f2c78a6b2e0df1e28b071acc # Parent 0d74081c130922826c40b7ca0615c9f7a2da6dfc clean up diff -r 0d74081c1309 -r 79d492bce828 src/plparser/DictProperty.java --- 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 map; diff -r 0d74081c1309 -r 79d492bce828 src/plparser/PLCharScannerImpl.java --- /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 extends PLScannerImpl { + + public CharBuffer cb; + + + public PLCharScannerImpl(PLScanner plScannerImpl, Dictionary dict, + Token nullToken) { + super(plScannerImpl, dict, nullToken); + } + + public PLCharScannerImpl() { + super(); + } + + public Token lookupDict(CharBuffer w, TokenID id) { + Token t; + String s = w.flip().toString(); + if ((t = dict.get(s))==null) { + dict.put(s, t = new Token(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 set(String exp) { + cb = CharBuffer.wrap(exp); + filename = null; file = null; + nextToken = nullToken; + return this; + } + + @Override + public PLScanner setFile(String file) throws FileNotFoundException { + this.filename = file; + nextToken = nullToken; + set(new FileReader(file)); + return this; + } + + @Override + public PLScanner 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 diff -r 0d74081c1309 -r 79d492bce828 src/plparser/PLScanner.java --- 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> scanToken(String exp); public Iterable> scanToken(FileReader file); + public Dictionary dictionary(); } \ No newline at end of file diff -r 0d74081c1309 -r 79d492bce828 src/plparser/PLScannerImpl.java --- 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 p, + Dictionary dict, Token nullToken) { + this.prev = p; + this.dict = dict; + this.nullToken = nullToken; } - public PLScannerImpl(PLScannerImpl plScannerImpl, - Dictionary dict, Token nullToken) { - this.dict = dict; - this.nullToken = nullToken; + + public PLScannerImpl() { + this.dict = new Dictionary(); + this.nullToken = new Token("",TokenID.NULL); + this.prev = null; } @@ -101,4 +111,8 @@ return prev; } + public Dictionary dictionary() { + return dict; + } + } \ No newline at end of file diff -r 0d74081c1309 -r 79d492bce828 src/plparser/PropertyListCharTokenizer.java --- 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 extends PropertyListScanner -implements PLScanner { - - public PropertyListCharTokenizer(Dictionary dict) { - super(dict); - } +public class PropertyListCharTokenizer extends PLCharScannerImpl implements PLScanner { public PropertyListCharTokenizer( PLScanners, Dictionary dict, Token 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 = lookupDict(w); + Token t = lookupDict(w,TokenID.VARIABLE); if (t.type!=TokenID.VARIABLE) { t = new Token(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 lookupDict(CharBuffer w) { - Token t; - String s = w.flip().toString(); - if ((t = dict.get(s))==null) { - dict.put(s, t = new Token(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 pushScannerFile(InputStream newfile, String prompt) { diff -r 0d74081c1309 -r 79d492bce828 src/plparser/PropertyListParser.java --- 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 dict) { dict.reserve("=",TokenID.Assign); dict.reserve(",",TokenID.Comma); dict.reserve(";",TokenID.Semicolon); @@ -41,11 +41,11 @@ } public void initialize() { - dict = new Dictionary(); - // scope = new PropertyListScope(null,dict); - initReservedWord(); - // scanner = new PropertyListScanner(dict); - scanner = new PropertyListCharTokenizer(dict); + scanner = new PropertyListScanner(); + // scanner = new PropertyListCharTokenizer(); + dict = scanner.dictionary(); + initReservedWord(dict); + // scope = new PropertyListScope(null,dict); } public Node parse() { diff -r 0d74081c1309 -r 79d492bce828 src/plparser/PropertyListScanner.java --- 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 extends PLScannerImpl implements PLScanner { +public class PropertyListScanner extends PLCharScannerImpl implements PLScanner { // We cannot make Generic Singleton pattern // static PropertyListScanner scanner = new PropertyListScanner(); @@ -31,24 +31,18 @@ */ public Matcher scan; - public CharBuffer cb; - public PropertyListScanner(Dictionary dict) { - this.dict = dict; - nullToken = new Token("",TokenID.NULL); - } - /* * Scanner Container for Stack */ public PropertyListScanner(PropertyListScanner prev, Dictionary dict, Token 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 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 setFile(String file) throws FileNotFoundException { - this.filename = file; - nextToken = nullToken; - set(new FileReader(file)); return this; } @Override public PLScanner 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 pushScannerFile(InputStream newfile, String prompt) { return new PropertyListScanner(this,dict,nullToken).setFile(newfile,prompt); diff -r 0d74081c1309 -r 79d492bce828 src/plparser/PropertyListStreamScanner.java --- 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 s, Dictionary dict, Token nullToken) { - this.dict = dict; - this.nullToken = nullToken; + super(s,dict,nullToken); } - - public PropertyListStreamScanner(Dictionary dict) { - this.dict = dict; - nullToken = new Token("",TokenID.NULL); - } - - public void init() { String pattern = "."; scan.useDelimiter(pattern); diff -r 0d74081c1309 -r 79d492bce828 src/plparser/PropertyListStreamTokenizer.java --- 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 propertyListStreamTokenizer, + PLScanner s, Dictionary dict, Token nullToken) { - this.dict = dict; - this.nullToken = nullToken; + super(s,dict,nullToken); } - - public PropertyListStreamTokenizer(Dictionary dict) { - this.dict = dict; - nullToken = new Token("",TokenID.NULL); - } - - public void init() { tokenizer.resetSyntax(); tokenizer.wordChars('0', '9'); diff -r 0d74081c1309 -r 79d492bce828 src/plparser/TestScanner.java --- 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 dict = new Dictionary(); - // scan = new PropertyListScanner(dict); - // scan = new PropertyListStreamTokenizer(dict); - // scan = new PropertyListStreamScanner(dict); - scan = new PropertyListCharTokenizer(dict); + // scan = new PropertyListScanner(); + // scan = new PropertyListStreamTokenizer(); + // scan = new PropertyListStreamScanner(); + scan = new PropertyListCharTokenizer(); }