regular lexer bkp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Lexer {
private class Indexer {
private int[] indexes;
public Indexer() {
indexes = new int[programText.size()];
int index = 0;
int sum = 0;
for (String item : programText) {
indexes[index] = sum;
sum += item.length() + 1;
index++;
}
}
public int getMegaLinePosition(int position) {
for (int index : indexes) {
if (index >= position) return index;
}
return -1;
}
public int getMegaLineNumber(int position) {
for (int i = 0; i < indexes.length; i++) {
if (indexes[i] >= position) return i;
}
return -1;
}
public int getLineNumber(int position, String currentString) {
for (int i = 0; i < indexes.length; i++) {
if (indexes[i] >= (position + (megaString.length() - currentString.length()))) {
return i;
}
}
return -1;
}
public int getLinePosition(int position, String currentString) {
return (position + (megaString.length() - currentString.length())) - indexes[getLineNumber(position, currentString)];
}
}
private ArrayList<String> programText;
private String megaString;
//private String keywords = "(^ifif|^dodo|^do)";
private String ident = "([a-zA-Z]+)(\\2)";
private String space = "[ \t\n]*";
private String comment = "(^\\{|^\\(\\*)(\\*(?!\\))|[^}*]|[\n])*(\\}|\\*\\))";
private int line = 1;
private int col = 1;
public Lexer() {
megaString = new String("");
}
public void incrementPos(String str, int pos) {
for (int i = 0; i < pos; i++) {
if (str.charAt(i) == '\n') {
line++;
col = 1;
} else {
col++;
}
}
}
public void setProgramText(ArrayList<String> programText) {
programText = new ArrayList<String>(programText);
}
public void analyze() {
if (programText == null) {
System.out.println("There is no text to analyze.");
return;
}
for (String item : programText) {
megaString = megaString.concat(item + "\n");
}
//System.out.println(megaString);
String currentString = new String(megaString);
Indexer indexer = new Indexer();
//String pattern = "("+keywords+space+")|(^"+ident+space+")|("+comment+space+")";
String keywordsPattern = "("+keywords+space+")";
String identPattern = "(^"+ident+space+")";
String commentPattern = "("+comment+space+")";
Pattern kp = Pattern.compile(keywordsPattern);
Pattern ip = Pattern.compile(identPattern);
Pattern cp = Pattern.compile(commentPattern);
//Pattern p = Pattern.compile(pattern);
int errorFlag = 0;
while (currentString.length() > 0) {
Matcher km = kp.matcher(currentString);
Matcher im = ip.matcher(currentString);
Matcher cm = cp.matcher(currentString);
//Matcher m = p.matcher(currentString);
if (km.find()) {
errorFlag = 0;
if (km.group(1) != null) {
System.out.println("KEYWORD" + "(" + line + "," + col + ") : " + currentString.substring(0, km.end()));
} else if (km.group(2) != null) {
System.out.println("km group 2 : " + currentString.substring(0, km.end()));
} else if (km.group(3) != null) {
System.out.println("km group 3 : " + currentString.substring(0, km.end()));
}
incrementPos(currentString, km.end());
currentString = currentString.substring(km.end(), currentString.length());
continue;
} else if (im.find()) {
errorFlag = 0;
if (im.group(1) != null) {
System.out.println("IDENT : " + "(" + line + "," + col + ") : " + currentString.substring(0, im.end()));
} else if (im.group(2) != null) {
System.out.println("im group 2 : " + currentString.substring(0, im.end()));
} else if (im.group(3) != null) {
System.out.println("im group 3 : " + currentString.substring(0, im.end()));
}
incrementPos(currentString, im.end());
currentString = currentString.substring(im.end(), currentString.length());
continue;
} else if (cm.find()) {
errorFlag = 0;
if (cm.group(1) != null) {
System.out.println("COMMENT : " + "(" + line + "," + col + ") : " + currentString.substring(0, cm.end()));
} else if (cm.group(2) != null) {
System.out.println("cm group 2 : " + currentString.substring(0, cm.end()));
} else if (cm.group(3) != null) {
System.out.println("cm group 3 : " + currentString.substring(0, cm.end()));
}
incrementPos(currentString, cm.end());
currentString = currentString.substring(cm.end(), currentString.length());
continue;
} else {
if (errorFlag == 0) {
errorFlag = 1;
System.out.println("Error at line " + line + ", col " + col + ".");
}
incrementPos(currentString, 1);
}
//if (m.find()) {
//if (m.group(1))
//System.out.println(m.end() + ", cs: " + currentString);
// System.out.println(currentString.substring(0, m.end()));
// currentString = currentString.substring(m.end(), currentString.length());
//}
}
}
}