00001 package gov.nasa.arc.ase.util; 00002 00003 public class Lexer { 00004 public static final char STRING = 'S'; 00005 public static final char INTEGER = 'I'; 00006 public static final char END = 'E'; 00007 public static final char ID = 'i'; 00008 public static final char LOGICAND = 'a'; 00009 public static final char LOGICOR = 'o'; 00010 public static final char EQUAL = 'e'; 00011 public static final char LESSEQUAL = 'l'; 00012 public static final char GREATEREQUAL = 'g'; 00013 00014 private char[] data; 00015 private int length; 00016 private int offset; 00017 private StringBuffer string; 00018 00019 public Lexer(String s) { 00020 length = s.length(); 00021 data = new char[length]; 00022 for(int i = 0; i < length; i++) 00023 data[i] = s.charAt(i); 00024 offset = 0; 00025 } 00026 public int getIntValue() { 00027 return Integer.parseInt(string.toString()); 00028 } 00029 public String getStringValue() { 00030 return string.toString(); 00031 } 00032 public char lex() throws LexerException { 00033 try { 00034 while(true) { 00035 switch(data[offset]) { 00036 case '\'': offset++; return parseQuoted('\''); 00037 case '"': offset++; return parseQuoted('"'); 00038 case ',': offset++; return ','; 00039 case ';': offset++; return ';'; 00040 case ':': offset++; return ':'; 00041 case '.': offset++; return '.'; 00042 case '(': offset++; return '('; 00043 case ')': offset++; return ')'; 00044 case ' ': offset++; break; 00045 case '\t': offset++; break; 00046 00047 case '=': 00048 offset++; 00049 if(data[offset] == '=') { 00050 offset++; 00051 return Lexer.EQUAL; 00052 } 00053 throw new LexerException("Unexpected character '" + data[offset] + "'"); 00054 00055 case '<': 00056 offset++; 00057 if(data[offset] == '=') { 00058 offset++; 00059 return Lexer.LESSEQUAL; 00060 } 00061 return '<'; 00062 00063 case '>': 00064 offset++; 00065 if(data[offset] == '=') { 00066 offset++; 00067 return Lexer.GREATEREQUAL; 00068 } 00069 return '>'; 00070 00071 case '&': 00072 offset++; 00073 if(data[offset] == '&') { 00074 offset++; 00075 return Lexer.LOGICAND; 00076 } 00077 return '&'; 00078 00079 case '|': 00080 offset++; 00081 if(data[offset] == '|') { 00082 offset++; 00083 return Lexer.LOGICOR; 00084 } 00085 return '|'; 00086 00087 default: 00088 if(Character.isDigit(data[offset])) return parseInt(); 00089 if(Character.isJavaIdentifierStart(data[offset])) return parseId(); 00090 throw new LexerException("invalid character '" + data[offset] + "'"); 00091 } 00092 } 00093 } catch(ArrayIndexOutOfBoundsException e) { 00094 return END; 00095 } 00096 } 00097 private char parseId() { 00098 string = new StringBuffer(); 00099 00100 try { 00101 while(Character.isJavaIdentifierPart(data[offset])) 00102 string.append(data[offset++]); 00103 } catch(IndexOutOfBoundsException e) { 00104 } 00105 00106 return ID; 00107 } 00108 private char parseInt() { 00109 string = new StringBuffer(); 00110 00111 try { 00112 while(Character.isDigit(data[offset])) 00113 string.append(data[offset++]); 00114 } catch(IndexOutOfBoundsException e) { 00115 } 00116 00117 return INTEGER; 00118 } 00119 private char parseQuoted(char quote) { 00120 string = new StringBuffer(); 00121 00122 try { 00123 while(data[offset] != quote) 00124 string.append(data[offset++]); 00125 offset++; 00126 } catch(IndexOutOfBoundsException e) { 00127 } 00128 00129 return STRING; 00130 } 00131 }