Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5ed5c9d

Browse files
author
code4wt
committed
构建语法解析器框架
1 parent 3e18bc4 commit 5ed5c9d

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.code4wt.jsonparser.exception;
2+
3+
/**
4+
* Created by code4wt on 17/5/11.
5+
*/
6+
public class JsonParseException extends RuntimeException {
7+
8+
public JsonParseException(String message) {
9+
super(message);
10+
}
11+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.code4wt.jsonparser.tokenizer;
2+
3+
import java.io.IOException;
4+
import java.io.Reader;
5+
6+
/**
7+
* Created by code4wt on 17/5/11.
8+
*/
9+
public class CharReader {
10+
11+
private static final int BUFFER_SIZE = 1024;
12+
13+
private Reader reader;
14+
15+
private char[] buffer;
16+
17+
private int pos;
18+
19+
private int size;
20+
21+
public CharReader(Reader reader) {
22+
this.reader = reader;
23+
buffer = new char[BUFFER_SIZE];
24+
}
25+
26+
public char next() throws IOException {
27+
if (!hasMore()) {
28+
return (char) -1;
29+
}
30+
31+
return buffer[pos++];
32+
}
33+
34+
public boolean hasMore() throws IOException {
35+
if (pos < size) {
36+
return true;
37+
}
38+
39+
fillBuffer();
40+
return pos < size;
41+
}
42+
43+
void fillBuffer() throws IOException {
44+
int n = reader.read(buffer);
45+
if (n == -1) {
46+
return;
47+
}
48+
49+
pos = 0;
50+
size = n;
51+
}
52+
}

‎src/main/java/com/code4wt/jsonparser/tokenizer/TokenType.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ public enum TokenType {
88
BEGIN_ARRAY, END_ARRAY,
99
NULL, NUMBER, STRING, BOOLEAN,
1010
SEP_COLON, SEP_COMMA,
11-
END_DOCOCUMENT
11+
END_DOCUMENT
1212
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,92 @@
11
package com.code4wt.jsonparser.tokenizer;
22

3+
import com.code4wt.jsonparser.exception.JsonParseException;
4+
5+
import java.io.IOException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import static com.code4wt.jsonparser.tokenizer.TokenType.*;
10+
311
/**
412
* Created by code4wt on 17/5/10.
513
*/
614
public class Tokenizer {
15+
16+
private CharReader charReader;
17+
18+
private List<Token> tokens = new ArrayList<Token>();
19+
20+
public void tokenize() throws IOException {
21+
// 使用do-while处理空文件
22+
Token token;
23+
do {
24+
token = parse();
25+
tokens.add(token);
26+
} while (token.getTokenType() != END_DOCUMENT);
27+
}
28+
29+
private Token parse() throws IOException {
30+
char ch;
31+
for(;;) {
32+
if (!charReader.hasMore()) {
33+
return new Token(END_DOCUMENT, null);
34+
}
35+
36+
ch = charReader.next();
37+
if (!isWhiteSpace(ch)) {
38+
break;
39+
}
40+
}
41+
42+
switch (ch) {
43+
case '{':
44+
return new Token(BEGIN_OBJECT, String.valueOf(ch));
45+
case '}':
46+
return new Token(END_OBJECT, String.valueOf(ch));
47+
case '[':
48+
return new Token(BEGIN_ARRAY, String.valueOf(ch));
49+
case ']':
50+
return new Token(END_ARRAY, String.valueOf(ch));
51+
case ',':
52+
return new Token(SEP_COMMA, String.valueOf(ch));
53+
case ':':
54+
return new Token(SEP_COLON, String.valueOf(ch));
55+
case 'n':
56+
return readNull();
57+
case 't':
58+
case 'f':
59+
return readBoolean();
60+
case '"':
61+
return readString();
62+
case '-':
63+
return readNumber();
64+
}
65+
66+
if (ch >= '0' && ch <= '9') {
67+
return readNumber();
68+
}
69+
70+
throw new JsonParseException("Illegal character");
71+
}
72+
73+
private boolean isWhiteSpace(char ch) {
74+
return (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n');
75+
}
76+
77+
private Token readString() {
78+
return null;
79+
}
80+
81+
private Token readNumber() {
82+
return null;
83+
}
84+
85+
private Token readBoolean() {
86+
return null;
87+
}
88+
89+
private Token readNull() {
90+
return null;
91+
}
792
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /