📄 scanner.java
字号:
return this.ttype = T_ID; /* return 'identifier' */ case S_FRAC: /* --- number (digit & point read) */ s.append((char)c); /* buffer character */ if (ccl == C_DIGIT) /* if another digit follows, */ break; /* do nothing else */ if ((c == 'e') /* if an exponent indicator follows, */ || (c == 'E')) { /* (lower- or uppercase), */ state = S_EXPIND; break; } /* go to exponent state */ if ((ccl == C_LETTER) /* if a letter */ || (ccl == C_POINT) /* or a decimal point */ || (ccl == C_SIGN)){ /* or a sign follows, */ state = S_ID; break;/* go to 'identifier' state */ } /* otherwise */ this.ungetc(c); /* put back last character, */ this.value = s.toString().substring(0,s.length()-1); return this.ttype = T_NUM; /* and return 'number' */ case S_EXPIND: /* --- exponent (indicator read) */ s.append((char)c); /* buffer character */ if (ccl == C_SIGN) { /* if a sign follows, */ state = S_EXPSGN; break; } /* go to 2nd 'exponent' state */ if (ccl == C_DIGIT) { /* if a digit follows, */ state = S_EXPDIG; break; } /* go to 3rd 'exponent' state */ if ((ccl == C_LETTER) /* if a letter */ || (ccl == C_POINT)){/* or a decimal point follows */ state = S_ID; break;/* go to 'identifier' state */ } /* otherwise */ this.ungetc(c); /* put back last character, */ this.value = s.toString().substring(0,s.length()-1); return this.ttype = T_ID; /* return 'identifier' */ case S_EXPSGN: /* --- exponent (sign read) */ s.append((char)c); /* buffer character */ if (ccl == C_DIGIT) { /* if a digit follows, */ state = S_EXPDIG; break;} /* do nothing else */ if ((ccl == C_LETTER) /* if a letter */ || (ccl == C_POINT) /* or a decimal point */ || (ccl == C_SIGN)){ /* or a sign follows */ state = S_ID; break;/* go to 'identifier' state */ } /* otherwise */ this.ungetc(c); /* put back last character, */ this.value = s.toString().substring(0,s.length()-1); return this.ttype = T_ID; /* return 'identifier' */ case S_EXPDIG: /* --- exponent (digit read) */ s.append((char)c); /* buffer character */ if (ccl == C_DIGIT) /* if another digit follows, */ break; /* do nothing else */ if ((ccl == C_LETTER) /* if a letter */ || (ccl == C_POINT) /* or a decimal point */ || (ccl == C_SIGN)){ /* or a sign follows, */ state = S_ID; break;/* go to 'identifier' state */ } /* otherwise */ this.ungetc(c); /* put back last character, */ this.value = s.toString().substring(0,s.length()-1); return this.ttype = T_NUM; /* and return 'number' */ case S_SIGN: /* --- number (sign read) */ s.append((char)c); /* buffer character */ if (ccl == C_DIGIT) { /* if a digit follows, */ state = S_NUMDIG; break; } /* go to 'number' state */ if (ccl == C_POINT) { /* if a decimal point follows, */ state = S_NUMPT; break; } /* go to fraction state */ if ((ccl == C_LETTER) /* if a letter */ || (ccl == C_SIGN)) { /* or a sign follows, */ state = S_ID; break; } /* go to 'identifier' state */ if ((c == '>') /* if a '>' follows and previous */ && (s.charAt(0) == '-')) { this.value = s.toString(); return this.ttype = T_RGT;}/* char was a minus sign */ this.ungetc(c); /* put back last character, */ this.value = s.toString().substring(0,s.length()-1); return this.ttype = T_ID; /* return 'identifier' */ case S_CMPOP: /* --- comparison operator read */ if ((c == '-') /* if a minus sign follows and */ && (s.charAt(0) == '<')) { /* prev. char was a '<' */ s.append('-'); this.ttype = T_LFT; } else if (c == '=') { /* if an equal sign follows */ s.append('='); this.ttype = T_CMP; } else { /* if anything else follows */ this.ungetc(c); this.ttype = s.charAt(0); } this.value = s.toString(); return this.ttype; /* and return the token read */ case S_STRING: /* --- quoted string */ if ((c == '\n') || (c == EOF)) /* if end of line or file */ throw new IOException("unterminated string" +this.lno()); if (c != quote) { /* if not at end of string */ if (c == '\\') { /* if escaped character follows, */ state = S_ESC; break; } /* go to escaped char state */ s.append((char)c); break; /* otherwise buffer character */ } this.value = s.toString(); return this.ttype = T_ID; /* return 'identifier' */ case S_ESC: /* --- after '\' in quoted string */ if ((c >= '0') && (c <= '7')) { /* if octal digit, */ ec = c -'0'; state = S_OCT1; break; }/* evaluate digit */ if (c == 'x') { /* if hexadecimal character code, */ state = S_HEX1; break;} /* go to hexadecimal evaluation */ switch (c) { /* evaluate character after '\' */ case 'b': c = '\b'; break; case 'f': c = '\f'; break; case 'n': c = '\n'; break; case 'r': c = '\r'; break; case 't': c = '\t'; break; case '\n': c = -1; break; default : break; } /* get escaped character and */ if (c >= 0) s.append((char)c); /* store it, then */ state = S_STRING; /* return to quoted string state */ break; case S_OCT1: /* --- escaped octal number 1 */ if ((c >= '0') /* if an octal digit follows, */ && (c <= '7')) { /* evaluate it */ ec = ec *8 +c -'0'; state = S_OCT2; break; } this.ungetc(c); /* otherwise put back last character */ s.append((char)ec); /* store escaped character and */ state = S_STRING; /* return to quoted string state */ break; case S_OCT2: /* --- escaped octal number 2 */ if ((c >= '0') || (c <= '7')) ec = ec *8 +c -'0'; /* if octal digit, evaluate it */ else this.ungetc(c); /* otherwise put back last character */ s.append((char)ec); /* store escaped character and */ state = S_STRING; /* return to quoted string state */ break; case S_HEX1: /* --- escaped hexadecimal number 1 */ if (ccl == C_DIGIT) { /* if hexadecimal digit, evaluate it */ ec = c -'0'; state = S_HEX2; break; } if ((c >= 'a') && (c <= 'f')) { ec = c -'a' +10; state = S_HEX2; break; } if ((c >= 'A') && (c <= 'F')) { ec = c -'A' +10; state = S_HEX2; break; } this.ungetc(c); /* otherwise put back last character */ s.append('x'); /* store escaped character ('x') and */ state = S_STRING; /* return to quoted string state */ break; case S_HEX2: /* --- escaped hexadecimal number 2 */ if (ccl == C_DIGIT) /* if hexadecimal digit, evaluate it */ ec = ec*16 +c -'0'; else if ((c >= 'a') && (c <= 'f')) ec = ec*16 +c -'a' +10; else if ((c >= 'A') && (c <= 'F')) ec = ec*16 +c -'A' +10; else this.ungetc(c); /* otherwise put back last character */ s.append((char)ec); /* store escaped character and */ state = S_STRING; /* return to quoted string state */ break; case S_SLASH: /* --- slash '/' */ if (c == '/') { /* if C++ style comment, then */ state = S_CPPCOM; break; } /* skip to end of line */ if (c == '*') { /* if C style comment */ state = S_CCOM1; break; } /* return to 1st state */ this.ungetc(c); /* otherwise put back last character */ this.value = "/"; /* store character in buffer */ return this.ttype = '/'; /* return `character' */ case S_CPPCOM: /* --- C++ style comment */ if ((c == '\n') /* if at end of line */ || (c == EOF)) /* or at end of file */ state = S_SPACE; /* return to white space skipping */ break; /* (skip to end of line) */ case S_CCOM1: /* --- C style comment 1 */ if (c == EOF) /* if end of file, abort */ throw new IOException("unterminated comment" +this.lno()); if (c == '*') /* if possibly 'end of comment', */ state = S_CCOM2; /* go to 2nd 'comment' state */ else if (c == '/') /* if possibly 'start of comment', */ state = S_CCOM3; /* go to 3rd 'comment' state */ break; case S_CCOM2: /* --- C style comment 2 */ if (c == EOF) /* if end of file, abort */ throw new IOException("unterminated comment" +this.lno()); if (c == '/') { /* if end of comment found */ if (--level <= 0) state = S_SPACE; else state = S_CCOM1; } else if (c != '*') /* if end of comment impossible */ state = S_CCOM1; /* return to comment skipping */ break; /* (possible start of comment) */ case S_CCOM3: /* --- C style comment 3 */ if (c == EOF) /* if end of file, abort */ throw new IOException("unterminated comment" +this.lno()); if (c == '*') { /* if start of comment found */ level++; state = S_CCOM1; } else if (c != '/') /* if start of comment impossible */ state = S_CCOM1; /* return to comment skipping */ break; /* (possible end of comment) */ default: /* if state is illegal, abort */ throw new IOException("illegal scanner state" +this.lno()); } /* switch() */ } /* while(1) */ } /* nextToken() */ /*------------------------------------------------------------------*/ public void ungetToken () { this.back = true; } public void pushBack () { this.back = true; } /*------------------------------------------------------------------*/ public void getChar (char c) throws IOException { /* --- check for a specific character */ if (this.nextToken() != c) /* check the next token */ throw new IOException(((c != '\n') ? "'" +c +"'" : "'\\n'") +" expected instead of '" +this.value +"'" +this.lno()); } /* getChar() */ /*------------------------------------------------------------------*/ public void getID (String id) throws IOException { /* --- check for an identifier */ if ((this.nextToken() != T_ID) /* check the next token */ && (this.ttype != T_NUM)) throw new IOException("identifier expected instead of '" +this.value +"'" +this.lno()); if (id == null) return; /* if a specific id is given, */ if (!id.equals(this.value)) /* compare the token value to it */ throw new IOException("'" +id +"' expected instead of '" +this.value +"'" +this.lno()); } /* getID() */ public void getID () throws IOException { getID(null); } /*------------------------------------------------------------------*/ public void getNumber () throws IOException { /* --- check for a number */ if (this.nextToken() != T_NUM) /* check the next token */ throw new IOException("number expected instead of \"" +this.value +"\"" +this.lno()); } /* getNumber() */ /*------------------------------------------------------------------*/ public static void main (String args[]) { /* --- main function for testing */ Scanner scanner; /* scanner for testing */ int token; /* next token */ String s; /* string to scan */ try { /* test the scanner */ switch (args.length) { /* evaluate the argument list */ case 0 : scanner = new Scanner("This is a test"); break; case 1 : scanner = new Scanner(args[0]); break; default: scanner = new Scanner(new FileInputStream(args[0])); break; } while (true) { /* scan string until the end */ token = scanner.nextToken(); if ((token < 0) || (token == T_EOF)) break; System.out.println(token +" : " +scanner.value); } } /* print token and value */ catch (IOException ioe) { System.err.println(ioe.getMessage()); } } /* main() */} /* class Scanner */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -