📄 tokenizer.java
字号:
* @return */ int getPosition() { return iIndex; } /** * mark the current position to be used for future getLastPart() calls * * @return */ String getPart(int begin, int end) { return sCommand.substring(begin, end); } /** * mark the current position to be used for future getLastPart() calls * * @return */ int getPartMarker() { return beginIndex; } /** * mark the current position to be used for future getLastPart() calls * * @return */ void setPartMarker() { beginIndex = iIndex; } /** * mark the position to be used for future getLastPart() calls * * @return */ void setPartMarker(int position) { beginIndex = position; } /** * return part of the command string from the last marked position * * @return */ String getLastPart() { return sCommand.substring(beginIndex, iIndex); }// fredt@users 20020910 - patch 1.7.1 by Nitin Chauhan - rewrite as switch /** * Method declaration * * * @throws SQLException */ private void getToken() throws SQLException { if (bWait) { bWait = false; iIndex = nextTokenIndex; return; } while (iIndex < iLength && Character.isWhitespace(sCommand.charAt(iIndex))) { iIndex++; } sToken = ""; tokenIndex = iIndex; if (iIndex >= iLength) { iType = 0; return; } char c = sCommand.charAt(iIndex); boolean point = false, digit = false, exp = false, afterexp = false; boolean end = false; char cfirst = 0; if (Character.isJavaIdentifierStart(c)) { iType = NAME; } else if (Character.isDigit(c)) { iType = NUMBER; digit = true; } else { switch (c) { case '(' : case ')' : case ',' : case '*' : case '=' : case ';' : case '+' : case '%' : iType = SPECIAL; iIndex++; sToken = String.valueOf(c); return; case '\"' : iType = QUOTED_IDENTIFIER; iIndex++; sToken = getString('"'); if (iIndex == sCommand.length()) { return; } c = sCommand.charAt(iIndex); if (c == '.') { sLongNameFirst = sToken; iIndex++;// fredt - todo - avoid recursion - this has problems when there is whitespace// after the dot - the same with NAME getToken(); sLongNameLast = sToken; iType = LONG_NAME; StringBuffer sb = new StringBuffer(sLongNameFirst.length() + 1 + sLongNameLast.length()); sb.append(sLongNameFirst); sb.append('.'); sb.append(sLongNameLast); sToken = sb.toString(); } return; case '\'' : iType = STRING; iIndex++; sToken = getString('\''); return; case '!' : case '<' : case '>' : case '|' : case '/' : case '-' : cfirst = c; iType = SPECIAL; break; case '.' : iType = DECIMAL; point = true; break; default : throw Trace.error(Trace.UNEXPECTED_TOKEN, String.valueOf(c)); } } int start = iIndex++; while (true) { if (iIndex >= iLength) { c = ' '; end = true; Trace.check(iType != STRING && iType != QUOTED_IDENTIFIER, Trace.UNEXPECTED_END_OF_COMMAND); } else { c = sCommand.charAt(iIndex); } switch (iType) { case NAME : if (Character.isJavaIdentifierPart(c)) { break; } // fredt - new char[] will back sToken sToken = sCommand.substring(start, iIndex).toUpperCase(); if (c == '.') { sLongNameFirst = sToken; iIndex++; getToken(); // todo: eliminate recursion sLongNameLast = sToken; iType = LONG_NAME; sToken = sLongNameFirst + "." + sLongNameLast; } return; case QUOTED_IDENTIFIER : case STRING : // shouldn't get here break; case REMARK : if (end) { // unfinished remark // maybe print error here iType = 0; return; } else if (c == '*') { iIndex++; if (iIndex < iLength && sCommand.charAt(iIndex) == '/') { // using recursion here iIndex++; getToken(); return; } } break; case REMARK_LINE : if (end) { iType = 0; return; } else if (c == '\r' || c == '\n') { // using recursion here getToken(); return; } break; case SPECIAL : if (c == '/' && cfirst == '/') { iType = REMARK_LINE; break; } else if (c == '-' && cfirst == '-') { iType = REMARK_LINE; break; } else if (c == '*' && cfirst == '/') { iType = REMARK; break; } else if (c == '>' || c == '=' || c == '|') { break; } sToken = sCommand.substring(start, iIndex); return; case NUMBER : case FLOAT : case DECIMAL : if (Character.isDigit(c)) { digit = true; } else if (c == '.') { iType = DECIMAL; if (point) { throw Trace.error(Trace.UNEXPECTED_TOKEN, "."); } point = true; } else if (c == 'E' || c == 'e') { if (exp) { throw Trace.error(Trace.UNEXPECTED_TOKEN, "E"); } // HJB-2001-08-2001 - now we are sure it's a float iType = FLOAT; // first character after exp may be + or - afterexp = true; point = true; exp = true; } else if (c == '-' && afterexp) { afterexp = false; } else if (c == '+' && afterexp) { afterexp = false; } else { afterexp = false; if (!digit) { if (point && start == iIndex - 1) { sToken = "."; iType = SPECIAL; return; } throw Trace.error(Trace.UNEXPECTED_TOKEN, String.valueOf(c)); } sToken = sCommand.substring(start, iIndex); return; } } iIndex++; } }// fredt - strings are constructed from new char[] objects to avoid slack// because these strings might end up as part of internal data structures// or table elements.// we may consider using pools to avoid recreating the strings private String getString(char quoteChar) throws SQLException { try { int nextIndex = iIndex; boolean quoteInside = false; for (;;) { nextIndex = sCommand.indexOf(quoteChar, nextIndex); if (nextIndex < 0) { throw Trace.error(Trace.UNEXPECTED_END_OF_COMMAND); } if (nextIndex < iLength - 1 && sCommand.charAt(nextIndex + 1) == quoteChar) { quoteInside = true; nextIndex += 2; continue; } break; } char[] chBuffer = new char[nextIndex - iIndex]; sCommand.getChars(iIndex, nextIndex, chBuffer, 0); int j = chBuffer.length; if (quoteInside) { j = 0; // fredt - loop assumes all occurences of quoteChar are paired for (int i = 0; i < chBuffer.length; i++, j++) { if (chBuffer[i] == quoteChar) { i++; } chBuffer[j] = chBuffer[i]; } } iIndex = ++nextIndex; return new String(chBuffer, 0, j); } catch (SQLException e) { throw e; } catch (Exception e) { e.getMessage(); } return null; }// fredt@users 20020420 - patch523880 by leptipre@users - VIEW support /** * Method declaration * * * @param s */ void setString(String s, int pos) { sCommand = s; iLength = s.length(); bWait = false; iIndex = pos; } /** * Method declaration * * * @return */ int getLength() { return iLength; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -