📄 lex.java
字号:
/* * Javassist, a Java-bytecode translator toolkit. * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved. * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. Alternatively, the contents of this file may be used under * the terms of the GNU Lesser General Public License Version 2.1 or later. * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. */package javassist.compiler;class Token { public Token next = null; public int tokenId; public long longValue; public double doubleValue; public String textValue;}public class Lex implements TokenId { private int lastChar; private StringBuffer textBuffer; private Token currentToken; private Token lookAheadTokens; private String input; private int position, maxlen, lineNumber; /** * Constructs a lexical analyzer. */ public Lex(String s) { lastChar = -1; textBuffer = new StringBuffer(); currentToken = new Token(); lookAheadTokens = null; input = s; position = 0; maxlen = s.length(); lineNumber = 0; } public int get() { if (lookAheadTokens == null) return get(currentToken); else { Token t; currentToken = t = lookAheadTokens; lookAheadTokens = lookAheadTokens.next; return t.tokenId; } } /** * Looks at the next token. */ public int lookAhead() { return lookAhead(0); } public int lookAhead(int i) { Token tk = lookAheadTokens; if (tk == null) { lookAheadTokens = tk = currentToken; // reuse an object! tk.next = null; get(tk); } for (; i-- > 0; tk = tk.next) if (tk.next == null) { Token tk2; tk.next = tk2 = new Token(); get(tk2); } currentToken = tk; return tk.tokenId; } public String getString() { return currentToken.textValue; } public long getLong() { return currentToken.longValue; } public double getDouble() { return currentToken.doubleValue; } private int get(Token token) { int t; do { t = readLine(token); } while (t == '\n'); token.tokenId = t; return t; } private int readLine(Token token) { int c = getNextNonWhiteChar(); if(c < 0) return c; else if(c == '\n') { ++lineNumber; return '\n'; } else if (c == '\'') return readCharConst(token); else if (c == '"') return readStringL(token); else if ('0' <= c && c <= '9') return readNumber(c, token); else if(c == '.'){ c = getc(); if ('0' <= c && c <= '9') { StringBuffer tbuf = textBuffer; tbuf.setLength(0); tbuf.append('.'); return readDouble(tbuf, c, token); } else{ ungetc(c); return readSeparator('.'); } } else if (Character.isJavaIdentifierStart((char)c)) return readIdentifier(c, token); else return readSeparator(c); } private int getNextNonWhiteChar() { int c; do { c = getc(); if (c == '/') { c = getc(); if (c == '/') do { c = getc(); } while (c != '\n' && c != '\r' && c != -1); else if (c == '*') while (true) { c = getc(); if (c == -1) break; else if (c == '*') if ((c = getc()) == '/') { c = ' '; break; } else ungetc(c); } else { ungetc(c); c = '/'; } } } while(isBlank(c)); return c; } private int readCharConst(Token token) { int c; int value = 0; while ((c = getc()) != '\'') if (c == '\\') value = readEscapeChar(); else if (c < 0x20) { if (c == '\n') ++lineNumber; return BadToken; } else value = c; token.longValue = value; return CharConstant; } private int readEscapeChar() { int c = getc(); if (c == 'n') c = '\n'; else if (c == 't') c = '\t'; else if (c == 'r') c = '\r'; else if (c == 'f') c = '\f'; else if (c == '\n') ++lineNumber; return c; } private int readStringL(Token token) { int c; StringBuffer tbuf = textBuffer; tbuf.setLength(0); for (;;) { while ((c = getc()) != '"') { if (c == '\\') c = readEscapeChar(); else if (c == '\n' || c < 0) { ++lineNumber; return BadToken; } tbuf.append((char)c); } for (;;) { c = getc(); if (c == '\n') ++lineNumber; else if (!isBlank(c)) break; } if (c != '"') { ungetc(c); break; } } token.textValue = tbuf.toString(); return StringL; } private int readNumber(int c, Token token) { long value = 0; int c2 = getc(); if (c == '0') if (c2 == 'X' || c2 == 'x') for (;;) { c = getc(); if ('0' <= c && c <= '9') value = value * 16 + (long)(c - '0'); else if ('A' <= c && c <= 'F') value = value * 16 + (long)(c - 'A' + 10); else if ('a' <= c && c <= 'f') value = value * 16 + (long)(c - 'a' + 10); else { token.longValue = value; if (c == 'L' || c == 'l') return LongConstant; else { ungetc(c); return IntConstant; } } } else if ('0' <= c2 && c2 <= '7') { value = c2 - '0'; for (;;) { c = getc(); if ('0' <= c && c <= '7') value = value * 8 + (long)(c - '0'); else { token.longValue = value; if (c == 'L' || c == 'l') return LongConstant; else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -