# HG changeset patch # User one # Date 1283392364 -32400 # Node ID 0d74081c130922826c40b7ca0615c9f7a2da6dfc # Parent 29e309b2f624c2de4929f39ff35afb6df0a7b684 Char loop tokenizer worked. diff -r 29e309b2f624 -r 0d74081c1309 src/plparser/PropertyListCharTokenizer.java --- a/src/plparser/PropertyListCharTokenizer.java Thu Sep 02 10:00:23 2010 +0900 +++ b/src/plparser/PropertyListCharTokenizer.java Thu Sep 02 10:52:44 2010 +0900 @@ -5,7 +5,7 @@ import java.nio.CharBuffer; public class PropertyListCharTokenizer extends PropertyListScanner - implements PLScanner { +implements PLScanner { public PropertyListCharTokenizer(Dictionary dict) { super(dict); @@ -15,72 +15,81 @@ PLScanners, Dictionary dict, Token nullToken) { super(dict); + this.prev = s; this.nullToken = nullToken; - this.prev = null; } public char ch; - + @Override public Token nextToken() { nextToken = nullToken; if (cb==null) return nextToken; - if (!hasRemaining()) return nextToken; - while(Character.isSpaceChar(ch)) { + while(true) { 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); + while(Character.isSpaceChar(ch)) { + if (!hasRemaining()) return nextToken; + ch = nextChar(); } - return nextToken = new Token(w.toString(),TokenID.NUMBER); - } else if (ch=='/') { - w.put(ch); - if (!hasRemaining()) return new Token(w.toString(),TokenID.Any); - ch = nextChar(); - if (ch=='/') { - while(hasRemaining() && (ch=nextChar())!='\n'); - if (!hasRemaining())return nullToken; + 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)) { // should handle more complex case + w.put(ch); + while(hasRemaining()&&Character.isDigit((ch=nextChar()))) { + w.put(ch); + } + return nextToken = new Token(w.flip().toString(),TokenID.NUMBER); + } + switch(ch) { + case '/': + w.put(ch); + if (!hasRemaining()) return new Token(w.flip().toString(),TokenID.Any); ch = nextChar(); - return nextToken(); - } - if (ch=='*') { - while(hasRemaining() && !((ch=nextChar())=='*'&&(ch=nextChar())=='/')); - if (!hasRemaining())return nullToken; + 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(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(); - return nextToken(); + Token t = lookupDict(w); + 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); + if (!hasRemaining())return nextToken; + ch = nextChar(); + return nextToken; + default: + ch = nextChar(); + continue; } - return new Token(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 lookupDict(CharBuffer w) { Token t; - String s = w.toString(); + String s = w.flip().toString(); if ((t = dict.get(s))==null) { dict.put(s, t = new Token(s,TokenID.Any)); } @@ -90,6 +99,7 @@ private char nextChar() { if (!cb.hasRemaining()) extendInput(); char ch = cb.get(); + if (ch=='\n') lineno++; return ch; } @@ -106,7 +116,7 @@ @Override public PLScanner pushScannerFile(String newfile) - throws FileNotFoundException { + throws FileNotFoundException { return new PropertyListCharTokenizer(this,dict,nullToken).setFile(newfile); } } diff -r 29e309b2f624 -r 0d74081c1309 src/plparser/PropertyListParser.java --- a/src/plparser/PropertyListParser.java Thu Sep 02 10:00:23 2010 +0900 +++ b/src/plparser/PropertyListParser.java Thu Sep 02 10:52:44 2010 +0900 @@ -44,7 +44,8 @@ dict = new Dictionary(); // scope = new PropertyListScope(null,dict); initReservedWord(); - scanner = new PropertyListScanner(dict); + // scanner = new PropertyListScanner(dict); + scanner = new PropertyListCharTokenizer(dict); } public Node parse() { diff -r 29e309b2f624 -r 0d74081c1309 src/plparser/PropertyListScanner.java --- a/src/plparser/PropertyListScanner.java Thu Sep 02 10:00:23 2010 +0900 +++ b/src/plparser/PropertyListScanner.java Thu Sep 02 10:52:44 2010 +0900 @@ -105,6 +105,9 @@ if ((t = dict.get(s))==null) { dict.put(s, t = new Token(s,TokenID.VARIABLE)); } + if (t.type!=TokenID.VARIABLE) { + t = new Token(s,TokenID.VARIABLE); + } return nextToken = t; } else if ((s=next(stringPat))!=null||(s=next(stringPat1))!=null||(s=next(namePat))!=null) { Token t;