📄 gtkscanner.java
字号:
/* * @(#)GTKScanner.java 1.39 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.java.swing.plaf.gtk;import java.io.*;import java.util.HashMap;/** * @author Shannon Hickey * @version 1.39 01/23/03 */class GTKScanner { public static final String CHARS_a_2_z = "abcdefghijklmnopqrstuvwxyz"; public static final String CHARS_A_2_Z = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static final String CHARS_DIGITS = "0123456789"; public static final int TOKEN_EOF = -1; public static final int TOKEN_LEFT_PAREN = '('; public static final int TOKEN_RIGHT_PAREN = ')'; public static final int TOKEN_LEFT_CURLY = '{'; public static final int TOKEN_RIGHT_CURLY = '}'; public static final int TOKEN_LEFT_BRACE = '['; public static final int TOKEN_RIGHT_BRACE = ']'; public static final int TOKEN_EQUAL_SIGN = '='; public static final int TOKEN_COMMA = ','; public static final int TOKEN_NONE = 256; public static final int TOKEN_ERROR = TOKEN_NONE + 1; public static final int TOKEN_CHAR = TOKEN_ERROR + 1; public static final int TOKEN_BINARY = TOKEN_CHAR + 1; public static final int TOKEN_OCTAL = TOKEN_BINARY + 1; public static final int TOKEN_INT = TOKEN_OCTAL + 1; public static final int TOKEN_HEX = TOKEN_INT + 1; public static final int TOKEN_FLOAT = TOKEN_HEX + 1; public static final int TOKEN_STRING = TOKEN_FLOAT + 1; public static final int TOKEN_SYMBOL = TOKEN_STRING + 1; public static final int TOKEN_IDENTIFIER = TOKEN_SYMBOL + 1; public static final int TOKEN_IDENTIFIER_NULL = TOKEN_IDENTIFIER + 1; public static final int TOKEN_LAST = TOKEN_IDENTIFIER_NULL + 1; public static final int ERR_UNKNOWN = 0; public static final int ERR_UNEXP_EOF = ERR_UNKNOWN + 1; public static final int ERR_UNEXP_EOF_IN_STRING = ERR_UNEXP_EOF + 1; public static final int ERR_UNEXP_EOF_IN_COMMENT = ERR_UNEXP_EOF_IN_STRING + 1; public static final int ERR_NON_DIGIT_IN_CONST = ERR_UNEXP_EOF_IN_COMMENT + 1; public static final int ERR_DIGIT_RADIX = ERR_NON_DIGIT_IN_CONST + 1; public static final int ERR_FLOAT_RADIX = ERR_DIGIT_RADIX + 1; public static final int ERR_FLOAT_MALFORMED = ERR_FLOAT_RADIX + 1; String whiteSpaceChars = " \t\r\n"; String identifierFirst = CHARS_a_2_z + CHARS_A_2_Z + "_"; String identifierNth = CHARS_a_2_z + CHARS_A_2_Z + "_-" + CHARS_DIGITS; String commentSingle = "#\n"; boolean caseSensitive = false; boolean scanCommentMulti = true; boolean scanIdentifier = true; boolean scanIdentifier1Char = false; boolean scanIdentifierNULL = false; boolean scanSymbols = true; boolean scanBinary = false; boolean scanOctal = true; boolean scanFloat = true; boolean scanHex = true; boolean scanHexDollar = false; boolean scanStringSq = true; boolean scanStringDq = true; boolean numbers2Int = true; boolean int2Float = false; boolean identifier2String = false; boolean char2Token = true; boolean symbol2Token = false; private static class ScannerKey { private int scope; private String symbol; public int value = -1; ScannerKey(int scope, String symbol) { this.scope = scope; this.symbol = symbol; } public boolean equals(Object o) { if (o instanceof ScannerKey) { ScannerKey comp = (ScannerKey)o; return scope == comp.scope && symbol.equals(comp.symbol); } return false; } public int hashCode() { int result = 17; result = 37 * result + scope; result = 37 * result + symbol.hashCode(); return result; } } static class TokenValue { long longVal; double doubleVal; char charVal; String stringVal; TokenValue() { clear(); } void copyFrom(TokenValue other) { longVal = other.longVal; doubleVal = other.doubleVal; charVal = other.charVal; stringVal = other.stringVal; } void clear() { longVal = 0L; doubleVal = 0.0D; charVal = (char)0; stringVal = null; } } private String inputName; private HashMap symbolTable = new HashMap(); private Reader reader; int currToken; TokenValue currValue = new TokenValue(); int currLine; int currPosition; int nextToken; TokenValue nextValue = new TokenValue(); int nextLine; int nextPosition; int currScope = 0; private static int nextUniqueScope = 1; private static final int CHAR_EOF = -1; private static final int CHAR_NONE = -2; private int peekedChar = CHAR_NONE; private ScannerKey lookupKey = new ScannerKey(0, null); public GTKScanner() { clearScanner(); } public void clearScanner() { if (reader != null) { try { reader.close(); } catch (IOException ioe) { } reader = null; } inputName = null; currToken = TOKEN_NONE; currValue.clear(); currLine = 1; currPosition = 0; nextToken = TOKEN_NONE; nextValue.clear(); nextLine = 1; nextPosition = 0; currScope = 0; peekedChar = CHAR_NONE; } public void scanReader(Reader r, String inputName) { if (r == null) { return; } if (reader != null) { clearScanner(); } reader = r; this.inputName = inputName; } public static int getUniqueScopeID() { return nextUniqueScope++; } public int setScope(int scope) { int oldScope = currScope; currScope = scope; return oldScope; } public void addSymbol(String symbol, int value) { if (symbol == null) { return; } ScannerKey key = lookupSymbol(symbol); if (key == null) { key = new ScannerKey(currScope, caseSensitive ? symbol : symbol.toLowerCase()); symbolTable.put(key, key); } key.value = value; } public boolean containsSymbol(String symbol) { return lookupSymbol(symbol) != null; } private ScannerKey lookupSymbol(String symbol) { lookupKey.scope = currScope; lookupKey.symbol = (caseSensitive ? symbol : symbol.toLowerCase()); return (ScannerKey)symbolTable.get(lookupKey); } public void clearSymbolTable() { symbolTable.clear(); } public int peekNextToken() throws IOException { if (nextToken == TOKEN_NONE) { readAToken(); switch(nextToken) { case TOKEN_SYMBOL: if (symbol2Token) { nextToken = (int)nextValue.longVal; } break; case TOKEN_IDENTIFIER: if (identifier2String) { nextToken = TOKEN_STRING; } break; case TOKEN_HEX: case TOKEN_OCTAL: case TOKEN_BINARY: if (numbers2Int) { nextToken = TOKEN_INT; } break; } if (nextToken == TOKEN_INT && int2Float) { nextToken = TOKEN_FLOAT; nextValue.doubleVal = nextValue.longVal; } } return nextToken; } public int getToken() throws IOException { currToken = peekNextToken(); currValue.copyFrom(nextValue); currLine = nextLine; currPosition = nextPosition; if (currToken != TOKEN_EOF) { nextToken = TOKEN_NONE; } return currToken; } private int peekNextChar() throws IOException { if (peekedChar == CHAR_NONE) { peekedChar = reader.read(); } return peekedChar; } private int getChar() throws IOException { int ch = peekNextChar(); if (ch != CHAR_EOF) { peekedChar = CHAR_NONE; if (ch == '\n') { nextPosition = 0; nextLine++; } else { nextPosition++; } } return ch; } // ----- scanning methods and variables ----- // private StringBuffer sb; private TokenValue value = new TokenValue(); private int token; private int ch; private boolean skipSpaceAndComments() throws IOException { while(ch != CHAR_EOF) { if (whiteSpaceChars.indexOf(ch) != -1) { // continue } else if (scanCommentMulti && ch == '/' && peekNextChar() == '*') { getChar(); while((ch = getChar()) != CHAR_EOF) { if (ch == '*' && peekNextChar() == '/') { getChar(); break; } } if (ch == CHAR_EOF) { return false; } } else if (commentSingle.length() == 2 && ch == commentSingle.charAt(0)) { while((ch = getChar()) != CHAR_EOF) { if (ch == commentSingle.charAt(1)) { break; } } if (ch == CHAR_EOF) { return false; } } else { break; } ch = getChar(); } return true; } private void readAToken() throws IOException { boolean inString = false; nextValue.clear(); sb = null; do { value.clear(); token = TOKEN_NONE; ch = getChar(); if (!skipSpaceAndComments()) { token = TOKEN_ERROR; value.longVal = ERR_UNEXP_EOF_IN_COMMENT; } else if (scanIdentifier && ch != CHAR_EOF && identifierFirst.indexOf(ch) != -1) { checkForIdentifier(); handleOrdinaryChar(); } else { switch(ch) { case CHAR_EOF: token = TOKEN_EOF; break; case '"': if (!scanStringDq) { handleOrdinaryChar(); } else { token = TOKEN_STRING; inString = true; sb = new StringBuffer();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -