1 19 20 package org.netbeans.modules.db.sql.editor; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.StringTokenizer ; 27 import org.netbeans.editor.Syntax; 28 import org.netbeans.editor.TokenID; 29 import org.netbeans.modules.db.api.sql.SQLKeywords; 30 import org.openide.util.NbBundle; 31 32 37 public class SQLSyntax extends Syntax { 38 39 private static final int ISI_WHITESPACE = 2; private static final int ISI_LINE_COMMENT = 4; private static final int ISI_BLOCK_COMMENT = 5; private static final int ISI_STRING = 6; private static final int ISI_STRING_A_QUOTE = 7; private static final int ISI_IDENTIFIER = 10; private static final int ISA_SLASH = 11; private static final int ISA_OPERATOR = 12; private static final int ISA_MINUS = 13; 48 private static final int ISA_STAR = 20; private static final int ISA_STAR_I_BLOCK_COMMENT_END = 21; private static final int ISA_EXCLAMATION = 26; private static final int ISA_ZERO = 27; private static final int ISI_INT = 28; private static final int ISI_DOUBLE = 30; private static final int ISA_DOT = 33; private static final int ISA_COMMA = 34; private static final int ISA_SEMICOLON = 35; private static final int ISA_LPAREN = 36; private static final int ISA_RPAREN = 37; 60 63 public SQLSyntax() { 64 tokenContextPath = SQLTokenContext.contextPath; 65 } 66 67 70 protected TokenID parseToken() { 71 char actChar; 73 while(offset < stopOffset) { 75 actChar = buffer[offset]; 76 77 switch (state) { 79 case INIT: 81 switch (actChar) { 82 case '\'': state = ISI_STRING; 84 break; 85 case '/': 86 state = ISA_SLASH; 87 break; 88 case '=': 89 case '>': 90 case '<': 91 case '+': 92 case ',': 93 case ')': 94 case '(': 95 case ';': 96 case '*': 97 case '!': 98 offset++; 99 state = INIT; 100 return SQLTokenContext.OPERATOR; 101 case '-': 102 state = ISA_MINUS; 103 break; 104 case '0': 105 state = ISA_ZERO; 106 break; 107 case '.': 108 state = ISA_DOT; 109 break; 110 default: 111 if (Character.isWhitespace(actChar)) { 113 state = ISI_WHITESPACE; 114 break; 115 } 116 117 if (Character.isDigit(actChar)) { 119 state = ISI_INT; 120 break; 121 } 122 123 state = ISI_IDENTIFIER; 125 break; 126 } 127 break; 128 case ISI_WHITESPACE: if (!Character.isWhitespace(actChar)) { 131 state = INIT; 132 return SQLTokenContext.WHITESPACE; 133 } 134 break; 135 136 case ISI_LINE_COMMENT: 138 if (actChar == '\n') { 139 state = INIT; 140 return SQLTokenContext.LINE_COMMENT; 141 } 142 break; 143 144 case ISI_BLOCK_COMMENT: 146 if(actChar =='*') { 147 state = ISA_STAR_I_BLOCK_COMMENT_END; 148 } 149 break; 150 151 case ISI_STRING: 153 switch (actChar) { 154 case '\n': 155 state = INIT; 156 return SQLTokenContext.INCOMPLETE_STRING; 157 case '\'': offset++; 159 state = INIT; 160 return SQLTokenContext.STRING; 161 } 162 break; 163 164 case ISI_IDENTIFIER: 166 if (!Character.isLetterOrDigit(actChar) && actChar != '_') { 167 state = INIT; 168 TokenID tid = matchKeyword(buffer, tokenOffset, offset - tokenOffset); 169 if (tid != null) { 170 return tid; 171 } else { 172 return SQLTokenContext.IDENTIFIER; 173 } 174 } 175 break; 176 177 case ISA_SLASH: 179 switch (actChar) { 180 case '*': 181 state = ISI_BLOCK_COMMENT; 182 break; 183 default: 184 if(Character.isWhitespace(actChar) || 185 actChar == '(') { 186 state = INIT; 187 return SQLTokenContext.OPERATOR; 188 } 189 } 190 break; 191 192 case ISA_MINUS: 194 switch (actChar) { 195 case '-': 196 state = ISI_LINE_COMMENT; 197 break; 198 default: 199 state = INIT; 200 return SQLTokenContext.OPERATOR; 201 } 202 break; 203 204 case ISA_STAR_I_BLOCK_COMMENT_END: 206 switch (actChar) { 207 case '/': 208 offset++; 209 state = INIT; 210 return SQLTokenContext.BLOCK_COMMENT; 211 default: 212 offset--; 213 state = ISI_BLOCK_COMMENT; 214 break; 215 } 216 break; 217 218 case ISA_ZERO: 220 switch (actChar) { 221 case '.': 222 state = ISI_DOUBLE; 223 break; 224 default: 225 if (Character.isDigit(actChar)) { 226 state = ISI_INT; 227 break; 228 } else { 229 state = INIT; 230 return SQLTokenContext.INT_LITERAL; 231 } 232 } 233 break; 234 235 case ISI_INT: 237 switch (actChar) { 238 case '.': 239 state = ISI_DOUBLE; 240 break; 241 default: 242 if (Character.isDigit(actChar)) { 243 state = ISI_INT; 244 break; 245 } else { 246 state = INIT; 247 return SQLTokenContext.INT_LITERAL; 248 } 249 } 250 break; 251 252 case ISI_DOUBLE: 255 if (actChar >= '0' && actChar <= '9') { 256 state = ISI_DOUBLE; 257 break; 258 } else { 259 state = INIT; 260 return SQLTokenContext.DOUBLE_LITERAL; 261 } 262 263 case ISA_DOT: 265 if (Character.isDigit(actChar)) { 266 state = ISI_DOUBLE; 267 } else { state = INIT; 269 return SQLTokenContext.DOT; 270 } 271 break; 272 273 } 275 offset++; 276 } 278 283 if (lastBuffer) { 284 switch(state) { 285 case ISI_WHITESPACE: 286 state = INIT; 287 return SQLTokenContext.WHITESPACE; 288 case ISI_IDENTIFIER: 289 state = INIT; 290 TokenID tid = 291 matchKeyword(buffer, tokenOffset, offset - tokenOffset); 292 if(tid != null) { 293 return tid; 294 } 295 else { 296 return SQLTokenContext.IDENTIFIER; 297 } 298 case ISI_LINE_COMMENT: 299 return SQLTokenContext.LINE_COMMENT; 301 case ISI_BLOCK_COMMENT: 302 case ISA_STAR_I_BLOCK_COMMENT_END: 303 return SQLTokenContext.BLOCK_COMMENT; 305 case ISI_STRING: 306 return SQLTokenContext.STRING; case ISA_ZERO: 308 case ISI_INT: 309 state = INIT; 310 return SQLTokenContext.INT_LITERAL; 311 case ISI_DOUBLE: 312 state = INIT; 313 return SQLTokenContext.DOUBLE_LITERAL; 314 case ISA_DOT: 315 state = INIT; 316 return SQLTokenContext.DOT; 317 case ISA_SLASH: 318 state = INIT; 319 return SQLTokenContext.OPERATOR; 320 } 321 } 322 323 331 332 switch (state) { 333 case ISI_WHITESPACE: 334 return SQLTokenContext.WHITESPACE; 335 } 336 337 return null; } 339 340 343 public String getStateName(int stateNumber) { 344 switch(stateNumber) { 345 case ISI_WHITESPACE: 346 return "ISI_WHITESPACE"; case ISI_LINE_COMMENT: 348 return "ISI_LINE_COMMENT"; case ISI_BLOCK_COMMENT: 350 return "ISI_BLOCK_COMMENT"; case ISI_STRING: 352 return "ISI_STRING"; case ISI_STRING_A_QUOTE: 354 return "ISI_STRING_A_QUOTE"; case ISI_IDENTIFIER: 356 return "ISI_IDENTIFIER"; case ISA_SLASH: 358 return "ISA_SLASH"; case ISA_OPERATOR: 360 return "ISA_OPERATOR"; case ISA_MINUS: 362 return "ISA_MINUS"; case ISA_STAR: 364 return "ISA_STAR"; case ISA_STAR_I_BLOCK_COMMENT_END: 366 return "ISA_STAR_I_BLOCK_COMMENT_END"; case ISA_ZERO: 368 return "ISA_ZERO"; case ISI_INT: 370 return "ISI_INT"; case ISI_DOUBLE: 372 return "ISI_DOUBLE"; case ISA_DOT: 374 return "ISA_DOT"; case ISA_COMMA: 376 return "ISA_COMMA"; 378 default: 379 return super.getStateName(stateNumber); 380 } 381 } 382 383 389 public TokenID matchKeyword(char[] buffer, int offset, int len) { 390 String keywordCandidate = new String (buffer, offset, len); 391 392 if (SQLKeywords.isSQL99Keyword(keywordCandidate)) { 393 return SQLTokenContext.KEYWORD; 394 } 395 396 return null; 397 } 398 } 399 | Popular Tags |