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());
//}
}
}
}