package idv.wuyou.code.to.image;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author na.shi.wu.you
*/
abstract class CodeTokenizer {
static List<String> tokenize(String code) {
List<String> result = new ArrayList<String>();
tokenize(code, result);
return result;
}
static void tokenize(String code, List<String> buffer) {
Ver02.instance.tokenizeUtil(code, buffer);
}
private CodeTokenizer() {}
abstract void tokenizeUtil(String code, List<String> buffer);
/*^***************************************************************************
* Version 01
****************************************************************************/
private static class Ver01 extends CodeTokenizer {
private static final Ver01 instance = new Ver01();
private static final String DILIMS = " =;,(){}[].\n";
@Override
void tokenizeUtil(String code, List<String> buffer) {
boolean isDilim = false;
StringBuilder builder = new StringBuilder();
for(int i = 0; i < code.length(); i++) {
char c = code.charAt(i);
boolean isCurrentDilim = DILIMS.indexOf(c) >= 0;
if( isCurrentDilim != isDilim ) {
isDilim = !isDilim;
if( builder.length() > 0 )
buffer.add(builder.toString());
builder.delete(0, builder.length());
}
builder.append(c);
}
if( builder.length() > 0 )
buffer.add(builder.toString());
}
}
/*^***************************************************************************
* Version 02
****************************************************************************/
private static class Ver02 extends CodeTokenizer {
private static enum Mode {
MultiComment,
SingleComment,
String,
Annotate,
Delim,
Default,
;
}
private static final Ver02 instance = new Ver02();
private static final String DILIMS = " =;,(){}[].\'\n";
@Override
void tokenizeUtil(String code, List<String> buffer) {
StringBuilder builder = new StringBuilder();
int len = code.length();
Mode mode = Mode.Default;
for(int i=0; i<len; i++) {
switch( mode ) {
case MultiComment :
mode = inMultiComment(code, i, builder, buffer);
break;
case SingleComment :
mode = inSingleComment(code, i, builder, buffer);
break;
case String :
mode = inString(code, i, builder, buffer);
break;
case Annotate :
mode = inAnnotate(code, i, builder, buffer);
break;
case Delim :
mode = inDelim(code, i, builder, buffer);
break;
default :
mode = inDefault(code, i, builder, buffer);
break;
}
}
clear(builder, buffer);
}
private Mode inMultiComment(String code, int index,
StringBuilder builder, List<String> buffer) {
char curr = code.charAt(index);
char prev = index == 0 ? 0 : code.charAt(index-1);
builder.append(curr);
if( curr == '/' && prev == '*' ) {
clear(builder, buffer);
return Mode.Default;
}
else {
return Mode.MultiComment;
}
}
private Mode inSingleComment(String code, int index,
StringBuilder builder, List<String> buffer) {
char curr = code.charAt(index);
builder.append(curr);
if( curr == '\n' ) {
clear(builder, buffer);
return Mode.Default;
}
else {
return Mode.SingleComment;
}
}
private Mode inString(String code, int index,
StringBuilder builder, List<String> buffer) {
char curr = code.charAt(index);
char prev = index == 0 ? 0 : code.charAt(index-1);
builder.append(curr);
if( curr == '"' && prev != '\\' ) {
clear(builder, buffer);
return Mode.Default;
}
else {
return Mode.String;
}
}
private Mode inAnnotate(String code, int index,
StringBuilder builder, List<String> buffer) {
char curr = code.charAt(index);
if( curr == '(' || curr == ' ' || curr == '\n' ) {
clear(builder, buffer);
builder.append(curr);
return Mode.Delim;
}
else {
builder.append(curr);
return Mode.Annotate;
}
}
private Mode inDelim(String code, int index,
StringBuilder builder, List<String> buffer) {
char curr = code.charAt(index);
char prev = index == 0 ? 0 : code.charAt(index-1);
char next = index == code.length()-1 ? 0 : code.charAt(index+1);
if( curr == '/' && next == '/' ) {
clear(builder, buffer);
builder.append(curr);
return Mode.SingleComment;
}
else if( curr == '/' && next == '*' ) {
clear(builder, buffer);
builder.append(curr);
return Mode.MultiComment;
}
else if( curr == '"' && prev != '\\' && prev != '\'' ) {
clear(builder, buffer);
builder.append(curr);
return Mode.String;
}
else if( curr == '@' ) {
clear(builder, buffer);
builder.append(curr);
return Mode.Annotate;
}
else if( DILIMS.indexOf(curr) >= 0 ) {
builder.append(curr);
return Mode.Delim;
}
else {
clear(builder, buffer);
builder.append(curr);
return Mode.Default;
}
}
private Mode inDefault(String code, int index,
StringBuilder builder, List<String> buffer) {
char curr = code.charAt(index);
char prev = index == 0 ? 0 : code.charAt(index - 1);
char next = index == code.length() - 1 ? 0 : code.charAt(index + 1);
if( curr == '/' && next == '/' ) {
clear(builder, buffer);
builder.append(curr);
return Mode.SingleComment;
}
else if( curr == '/' && next == '*' ) {
clear(builder, buffer);
builder.append(curr);
return Mode.MultiComment;
}
else if( curr == '"' && prev != '\\' && prev != '\'' ) {
clear(builder, buffer);
builder.append(curr);
return Mode.String;
}
else if( curr == '@' ) {
clear(builder, buffer);
builder.append(curr);
return Mode.Annotate;
}
else if( DILIMS.indexOf(curr) < 0 ) {
builder.append(curr);
return Mode.Default;
}
else {
clear(builder, buffer);
builder.append(curr);
return Mode.Delim;
}
}
private void clear(StringBuilder builder, List<String> buffer) {
if( builder.length() > 0 ) {
buffer.add(builder.toString());
builder.delete(0, builder.length());
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页