Skip to content

Commit

Permalink
支持中文标签
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamijoucen committed May 4, 2019
1 parent b789383 commit ef47cf9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/com/kamijoucen/xml/io/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ public class IOUtil {

public final static boolean[] firstIdentifierFlags = new boolean[256];
public final static boolean[] identifierFlags = new boolean[256];
public final static char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

static {

for (char c = 0; c < firstIdentifierFlags.length; ++c) {
if (c >= 'A' && c <= 'Z') {
firstIdentifierFlags[c] = true;
} else if (c >= 'a' && c <= 'z') {
firstIdentifierFlags[c] = true;
} else if (c == '_' || c == '$') {
} else if (c == '_' || c == ':') {
firstIdentifierFlags[c] = true;
}
}
Expand All @@ -22,13 +22,12 @@ public class IOUtil {
identifierFlags[c] = true;
} else if (c >= 'a' && c <= 'z') {
identifierFlags[c] = true;
} else if (c == '_') {
} else if (c == '_' || c == ':' || c == '.' || c == '-') {
identifierFlags[c] = true;
} else if (c >= '0' && c <= '9') {
identifierFlags[c] = true;
}
}

}

}
2 changes: 1 addition & 1 deletion src/com/kamijoucen/xml/io/SimplePeekBufferReader2.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public int peek(int i) throws IOException {
}
// 这时的index是当前应该读取的
int index = bufIndex + 1;
if (index + i >= bufferLength) {
if (index + i - 1 >= bufferLength) {
fill();
}
if (bufferLength == -1) {
Expand Down
13 changes: 9 additions & 4 deletions src/com/kamijoucen/xml/parser/impl/DefaultScanner.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kamijoucen.xml.parser.impl;

import com.kamijoucen.common.utils.StringUtils;
import com.kamijoucen.xml.io.IOUtil;
import com.kamijoucen.xml.io.SimplePeekBufferReader2;
import com.kamijoucen.xml.parser.Scanner;
import com.kamijoucen.xml.exception.FileAccessException;
Expand Down Expand Up @@ -104,10 +105,14 @@ public Token nextToken() {
} else if (currentChar == '\"' || currentChar == '\'') {
currentStringToken = currentChar;
state = State.STRING;
} else if (StringUtils.isAlpha(currentChar) || currentChar == '_' || currentChar == ':') {
state = State.IDENTIFIER;
// } else if (StringUtils.isAlpha(currentChar) || currentChar == '_' || currentChar == ':') {
} else {
throw new XmlSyntaxException(tokenLocation + "处未知的字符 '" + currentChar + "'");
final boolean[] firstIdent = IOUtil.firstIdentifierFlags;
boolean isFirst = currentChar >= firstIdent.length || firstIdent[currentChar];
if (!isFirst) {
throw new XmlSyntaxException("illegal identifier :" + currentChar);
}
state = State.IDENTIFIER;
}
}
}
Expand Down Expand Up @@ -394,7 +399,7 @@ private TokenLocation makeTokenLocation() {
}

private boolean isIdentifierChar(char ch) {
return StringUtils.isAlpha(ch) || Character.isDigit(ch) || ch == '-' || ch == '_' || ch == '.' || ch == ':';
return ch >= IOUtil.identifierFlags.length || IOUtil.identifierFlags[ch];
}

private boolean isKeyWords(char ch) {
Expand Down

0 comments on commit ef47cf9

Please sign in to comment.