1.0.2 get faster lexer

This commit is contained in:
fangyidong
2009-01-10 08:19:34 +00:00
parent d65cf1822b
commit e1c300a681
10 changed files with 89 additions and 61 deletions

View File

@@ -1 +1 @@
Fang Yidong <fangyidong@yahoo.com.cn>
Fang Yidong <fangyidong@gmail.com>

View File

@@ -1,5 +1,9 @@
ChangeLog
Version 1.02 (2009/01/10)
* Updated json.lex to improve performance of the lexer
* Removed Rope.java and related junit test
Version 1.01 (2008/08/26)
* License changed to a more commerce friendly and clear one, Apache License 2.0
* Use JFlex to generate a faster Yylex.java

View File

@@ -1 +1 @@
1.0.1
1.0.2

View File

@@ -1,5 +1,5 @@
<project name="json-simple" default="main" basedir=".">
<property name="current-version" value="1.0.1"/>
<property name="current-version" value="1.0.2"/>
<property name="jar-name" value="json_simple-${current-version}.jar"/>
<target name="main" depends="mkdir,compile,jar">
@@ -29,4 +29,4 @@
basedir="build/main"
includes="**/*.class"/>
</target>
</project>
</project>

View File

@@ -1,11 +1,9 @@
package org.json.simple.parser;
import org.json.simple.rope.Rope;
%%
%{
private Rope sb=new Rope();
private StringBuffer sb=new StringBuffer();
%}
@@ -17,8 +15,11 @@ HEX_D = [a-fA-F0-9]
INT = [-]?[0-9]+
DOUBLE = {INT}((\.[0-9]+)?([eE][-+]?[0-9]+)?)
WS = [ \t\r\n]
UNESCAPED_CH = [^\"\\]
%%
<STRING_BEGIN> \" { yybegin(YYINITIAL);return new Yytoken(Yytoken.TYPE_VALUE,sb.toString());}
<STRING_BEGIN> {UNESCAPED_CH}+ { sb.append(yytext());}
<STRING_BEGIN> \\\" {sb.append('"');}
<STRING_BEGIN> \\\\ {sb.append('\\');}
<STRING_BEGIN> \\\/ {sb.append('/');}
@@ -30,10 +31,8 @@ WS = [ \t\r\n]
<STRING_BEGIN> \\u{HEX_D}{HEX_D}{HEX_D}{HEX_D} { int ch=Integer.parseInt(yytext().substring(2),16);
sb.append((char)ch);
}
<STRING_BEGIN> \" { yybegin(YYINITIAL);return new Yytoken(Yytoken.TYPE_VALUE,sb.toString());}
<STRING_BEGIN> . { sb.append(yytext());}
<YYINITIAL> \" { sb.clear();yybegin(STRING_BEGIN);}
<YYINITIAL> \" { sb.delete(0, sb.length());yybegin(STRING_BEGIN);}
<YYINITIAL> {INT} { Long val=Long.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE,val);}
<YYINITIAL> {DOUBLE} { Double val=Double.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE,val);}
<YYINITIAL> "true"|"false" { Boolean val=Boolean.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE,val);}
@@ -44,4 +43,4 @@ WS = [ \t\r\n]
<YYINITIAL> "]" { return new Yytoken(Yytoken.TYPE_RIGHT_SQUARE,null);}
<YYINITIAL> "," { return new Yytoken(Yytoken.TYPE_COMMA,null);}
<YYINITIAL> ":" { return new Yytoken(Yytoken.TYPE_COLON,null);}
<YYINITIAL> {WS} {}
<YYINITIAL> {WS}+ {}

BIN
lib/json_simple-1.0.2.jar Normal file

Binary file not shown.

View File

@@ -22,6 +22,12 @@ public class JSONParser {
public static final int S_PASSED_PAIR_KEY=4;
public static final int S_IN_ERROR=-1;
private LinkedList statusStack = new LinkedList();
private LinkedList valueStack = new LinkedList();
private Yylex lexer = new Yylex((Reader)null);
private Yytoken token = null;
private int status = S_INIT;
private int peekStatus(LinkedList statusStack){
if(statusStack.size()==0)
return -1;
@@ -29,13 +35,17 @@ public class JSONParser {
return status.intValue();
}
private void reset(Reader in) throws Exception{
statusStack.clear();
valueStack.clear();
lexer.yyreset(in);
token = null;
status = S_INIT;
}
public Object parse(Reader in) throws Exception{
LinkedList statusStack=new LinkedList();
LinkedList valueStack=new LinkedList();
Yylex lexer=new Yylex(in);
Yytoken token=null;
int status=S_INIT;
reset(in);
try{
do{
token=lexer.yylex();

View File

@@ -1,9 +1,7 @@
/* The following code was generated by JFlex 1.4.2 on 2008-11-25 */
/* The following code was generated by JFlex 1.4.2 */
package org.json.simple.parser;
import org.json.simple.rope.Rope;
class Yylex {
/** This character denotes the end of file */
@@ -30,11 +28,11 @@ class Yylex {
* Translates characters to character classes
*/
private static final String ZZ_CMAP_PACKED =
"\11\0\1\7\1\21\2\0\1\7\22\0\1\7\1\0\1\11\10\0"+
"\1\6\1\32\1\2\1\4\1\12\12\3\1\33\6\0\4\1\1\5"+
"\1\1\24\0\1\30\1\10\1\31\3\0\1\23\1\13\2\1\1\22"+
"\1\14\5\0\1\24\1\0\1\15\3\0\1\16\1\25\1\17\1\20"+
"\5\0\1\26\1\0\1\27\uff82\0";
"\11\0\2\7\2\0\1\7\22\0\1\7\1\0\1\11\10\0\1\6"+
"\1\31\1\2\1\4\1\12\12\3\1\32\6\0\4\1\1\5\1\1"+
"\24\0\1\27\1\10\1\30\3\0\1\22\1\13\2\1\1\21\1\14"+
"\5\0\1\23\1\0\1\15\3\0\1\16\1\24\1\17\1\20\5\0"+
"\1\25\1\0\1\26\uff82\0";
/**
* Translates characters to character classes
@@ -48,9 +46,9 @@ class Yylex {
private static final String ZZ_ACTION_PACKED_0 =
"\3\0\1\1\1\2\1\3\3\0\1\4\1\5\1\6"+
"\1\7\1\10\1\11\2\12\1\13\5\0\1\14\1\15"+
"\1\16\1\17\1\20\1\21\1\22\1\23\1\0\1\24"+
"\1\0\1\24\4\0\1\25\1\26\2\0\1\27";
"\1\7\1\10\1\11\1\12\1\0\1\13\5\0\1\14"+
"\1\15\1\16\1\17\1\20\1\21\1\22\1\23\1\0"+
"\1\24\1\0\1\24\4\0\1\25\1\26\2\0\1\27";
private static int [] zzUnpackAction() {
int [] result = new int[44];
@@ -131,7 +129,7 @@ class Yylex {
private boolean zzAtEOF;
/* user code: */
private Rope sb=new Rope();
private StringBuffer sb=new StringBuffer();
@@ -165,7 +163,7 @@ private Rope sb=new Rope();
char [] map = new char[0x10000];
int i = 0; /* index in packed string */
int j = 0; /* index in unpacked array */
while (i < 90) {
while (i < 88) {
int count = packed.charAt(i++);
char value = packed.charAt(i++);
do map[j++] = value; while (--count > 0);
@@ -420,27 +418,25 @@ private Rope sb=new Rope();
switch (zzInput) {
case 2: zzState = 2; break zzForNext;
case 3: zzIsFinal = true; zzState = 3; break zzForNext;
case 7:
case 17: zzIsFinal = true; zzNoLookAhead = true; zzState = 4; break zzForNext;
case 7: zzIsFinal = true; zzState = 4; break zzForNext;
case 9: zzIsFinal = true; zzNoLookAhead = true; zzState = 5; break zzForNext;
case 12: zzState = 6; break zzForNext;
case 13: zzState = 7; break zzForNext;
case 15: zzState = 8; break zzForNext;
case 22: zzIsFinal = true; zzNoLookAhead = true; zzState = 9; break zzForNext;
case 23: zzIsFinal = true; zzNoLookAhead = true; zzState = 10; break zzForNext;
case 24: zzIsFinal = true; zzNoLookAhead = true; zzState = 11; break zzForNext;
case 25: zzIsFinal = true; zzNoLookAhead = true; zzState = 12; break zzForNext;
case 26: zzIsFinal = true; zzNoLookAhead = true; zzState = 13; break zzForNext;
case 27: zzIsFinal = true; zzNoLookAhead = true; zzState = 14; break zzForNext;
case 21: zzIsFinal = true; zzNoLookAhead = true; zzState = 9; break zzForNext;
case 22: zzIsFinal = true; zzNoLookAhead = true; zzState = 10; break zzForNext;
case 23: zzIsFinal = true; zzNoLookAhead = true; zzState = 11; break zzForNext;
case 24: zzIsFinal = true; zzNoLookAhead = true; zzState = 12; break zzForNext;
case 25: zzIsFinal = true; zzNoLookAhead = true; zzState = 13; break zzForNext;
case 26: zzIsFinal = true; zzNoLookAhead = true; zzState = 14; break zzForNext;
default: break zzForAction;
}
case 1:
switch (zzInput) {
case 8: zzIsFinal = true; zzState = 16; break zzForNext;
case 8: zzState = 16; break zzForNext;
case 9: zzIsFinal = true; zzNoLookAhead = true; zzState = 17; break zzForNext;
case 17: break zzForAction;
default: zzIsFinal = true; zzNoLookAhead = true; zzState = 15; break zzForNext;
default: zzIsFinal = true; zzState = 15; break zzForNext;
}
case 2:
@@ -454,13 +450,19 @@ private Rope sb=new Rope();
case 3: zzIsFinal = true; break zzForNext;
case 4: zzState = 18; break zzForNext;
case 5:
case 18: zzState = 19; break zzForNext;
case 17: zzState = 19; break zzForNext;
default: break zzForAction;
}
case 4:
switch (zzInput) {
case 7: zzIsFinal = true; break zzForNext;
default: break zzForAction;
}
case 6:
switch (zzInput) {
case 19: zzState = 20; break zzForNext;
case 18: zzState = 20; break zzForNext;
default: break zzForAction;
}
@@ -476,6 +478,13 @@ private Rope sb=new Rope();
default: break zzForAction;
}
case 15:
switch (zzInput) {
case 8:
case 9: break zzForAction;
default: zzIsFinal = true; break zzForNext;
}
case 16:
switch (zzInput) {
case 8: zzIsFinal = true; zzNoLookAhead = true; zzState = 23; break zzForNext;
@@ -506,13 +515,13 @@ private Rope sb=new Rope();
case 20:
switch (zzInput) {
case 20: zzState = 35; break zzForNext;
case 19: zzState = 35; break zzForNext;
default: break zzForAction;
}
case 21:
switch (zzInput) {
case 20: zzState = 36; break zzForNext;
case 19: zzState = 36; break zzForNext;
default: break zzForAction;
}
@@ -529,15 +538,15 @@ private Rope sb=new Rope();
case 5:
case 11:
case 12:
case 18:
case 19: zzState = 38; break zzForNext;
case 17:
case 18: zzState = 38; break zzForNext;
default: break zzForAction;
}
case 32:
switch (zzInput) {
case 5:
case 18: zzState = 19; break zzForNext;
case 17: zzState = 19; break zzForNext;
case 3: zzIsFinal = true; break zzForNext;
default: break zzForAction;
}
@@ -556,19 +565,19 @@ private Rope sb=new Rope();
case 35:
switch (zzInput) {
case 21: zzState = 37; break zzForNext;
case 20: zzState = 37; break zzForNext;
default: break zzForAction;
}
case 36:
switch (zzInput) {
case 20: zzIsFinal = true; zzNoLookAhead = true; zzState = 39; break zzForNext;
case 19: zzIsFinal = true; zzNoLookAhead = true; zzState = 39; break zzForNext;
default: break zzForAction;
}
case 37:
switch (zzInput) {
case 18: zzIsFinal = true; zzNoLookAhead = true; zzState = 40; break zzForNext;
case 17: zzIsFinal = true; zzNoLookAhead = true; zzState = 40; break zzForNext;
default: break zzForAction;
}
@@ -579,8 +588,8 @@ private Rope sb=new Rope();
case 5:
case 11:
case 12:
case 18:
case 19: zzState = 41; break zzForNext;
case 17:
case 18: zzState = 41; break zzForNext;
default: break zzForAction;
}
@@ -591,8 +600,8 @@ private Rope sb=new Rope();
case 5:
case 11:
case 12:
case 18:
case 19: zzState = 42; break zzForNext;
case 17:
case 18: zzState = 42; break zzForNext;
default: break zzForAction;
}
@@ -603,8 +612,8 @@ private Rope sb=new Rope();
case 5:
case 11:
case 12:
case 18:
case 19: zzIsFinal = true; zzNoLookAhead = true; zzState = 43; break zzForNext;
case 17:
case 18: zzIsFinal = true; zzNoLookAhead = true; zzState = 43; break zzForNext;
default: break zzForAction;
}
@@ -632,7 +641,7 @@ private Rope sb=new Rope();
}
case 24: break;
case 3:
{ sb.clear();yybegin(STRING_BEGIN);
{ sb.delete(0, sb.length());yybegin(STRING_BEGIN);
}
case 25: break;
case 15:

View File

@@ -43,7 +43,6 @@
<formatter type="plain"/>
<test name="org.json.simple.Test"/>
<test name="org.json.simple.parser.YylexTest"/>
<test name="org.json.simple.rope.RopeTest"/>
</junit>
</target>
</project>
</project>

View File

@@ -4,6 +4,8 @@
*/
package org.json.simple;
import java.util.List;
import junit.framework.TestCase;
@@ -39,6 +41,11 @@ public class Test extends TestCase{
s="[5,,2]";
obj=JSONValue.parse(s);
assertEquals("[5,2]",obj.toString());
s="[\"hello\\bworld\\\"abc\\tdef\\\\ghi\\rjkl\\n123\\u4e2d\"]";
obj=JSONValue.parse(s);
assertEquals("hello\bworld\"abc\tdef\\ghi\rjkl\n123中",((List)obj).get(0).toString());
}
public void testEncode() throws Exception{