src/share/classes/com/sun/tools/javac/parser/Scanner.java

Tue, 09 Oct 2012 19:31:58 -0700

author
jjg
date
Tue, 09 Oct 2012 19:31:58 -0700
changeset 1358
fc123bdeddb8
parent 1144
9448fe783fd2
child 1521
71f35e4b93a5
permissions
-rw-r--r--

8000208: fix langtools javadoc comment issues
Reviewed-by: bpatel, mcimadamore

duke@1 1 /*
jjg@1358 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.parser;
duke@1 27
duke@1 28 import java.nio.*;
mcimadamore@1144 29 import java.util.List;
mcimadamore@1144 30 import java.util.ArrayList;
duke@1 31
mcimadamore@1113 32 import com.sun.tools.javac.util.Position.LineMap;
mcimadamore@1113 33 import com.sun.tools.javac.parser.JavaTokenizer.*;
duke@1 34
mcimadamore@1113 35 import static com.sun.tools.javac.parser.Tokens.*;
duke@1 36
duke@1 37 /** The lexical analyzer maps an input stream consisting of
duke@1 38 * ASCII characters and Unicode escapes into a token sequence.
duke@1 39 *
jjg@581 40 * <p><b>This is NOT part of any supported API.
jjg@581 41 * If you write code that depends on this, you do so at your own risk.
duke@1 42 * This code and its internal interfaces are subject to change or
duke@1 43 * deletion without notice.</b>
duke@1 44 */
duke@1 45 public class Scanner implements Lexer {
duke@1 46
mcimadamore@1113 47 private Tokens tokens;
duke@1 48
duke@1 49 /** The token, set by nextToken().
duke@1 50 */
duke@1 51 private Token token;
duke@1 52
mcimadamore@1113 53 /** The previous token, set by nextToken().
duke@1 54 */
mcimadamore@1113 55 private Token prevToken;
duke@1 56
mcimadamore@1144 57 /** Buffer of saved tokens (used during lookahead)
mcimadamore@1144 58 */
mcimadamore@1144 59 private List<Token> savedTokens = new ArrayList<Token>();
mcimadamore@1144 60
mcimadamore@1113 61 private JavaTokenizer tokenizer;
duke@1 62 /**
duke@1 63 * Create a scanner from the input array. This method might
duke@1 64 * modify the array. To avoid copying the input array, ensure
duke@1 65 * that {@code inputLength < input.length} or
duke@1 66 * {@code input[input.length -1]} is a white space character.
duke@1 67 *
duke@1 68 * @param fac the factory which created this Scanner
jjg@1358 69 * @param buf the input, might be modified
duke@1 70 * Must be positive and less than or equal to input.length.
duke@1 71 */
mcimadamore@1113 72 protected Scanner(ScannerFactory fac, CharBuffer buf) {
mcimadamore@1113 73 this(fac, new JavaTokenizer(fac, buf));
duke@1 74 }
duke@1 75
mcimadamore@1113 76 protected Scanner(ScannerFactory fac, char[] buf, int inputLength) {
mcimadamore@1113 77 this(fac, new JavaTokenizer(fac, buf, inputLength));
duke@1 78 }
duke@1 79
mcimadamore@1113 80 protected Scanner(ScannerFactory fac, JavaTokenizer tokenizer) {
mcimadamore@1113 81 this.tokenizer = tokenizer;
mcimadamore@1113 82 tokens = fac.tokens;
mcimadamore@1113 83 token = prevToken = DUMMY;
duke@1 84 }
duke@1 85
duke@1 86 public Token token() {
mcimadamore@1144 87 return token(0);
duke@1 88 }
duke@1 89
mcimadamore@1144 90 public Token token(int lookahead) {
mcimadamore@1144 91 if (lookahead == 0) {
mcimadamore@1144 92 return token;
mcimadamore@1144 93 } else {
mcimadamore@1144 94 ensureLookahead(lookahead);
mcimadamore@1144 95 return savedTokens.get(lookahead - 1);
mcimadamore@1144 96 }
mcimadamore@1144 97 }
mcimadamore@1144 98 //where
mcimadamore@1144 99 private void ensureLookahead(int lookahead) {
mcimadamore@1144 100 for (int i = savedTokens.size() ; i < lookahead ; i ++) {
mcimadamore@1144 101 savedTokens.add(tokenizer.readToken());
mcimadamore@1144 102 }
mcimadamore@1144 103 }
mcimadamore@1144 104
mcimadamore@1113 105 public Token prevToken() {
mcimadamore@1113 106 return prevToken;
duke@1 107 }
duke@1 108
mcimadamore@1113 109 public void nextToken() {
mcimadamore@1113 110 prevToken = token;
mcimadamore@1144 111 if (!savedTokens.isEmpty()) {
mcimadamore@1144 112 token = savedTokens.remove(0);
mcimadamore@1144 113 } else {
mcimadamore@1144 114 token = tokenizer.readToken();
mcimadamore@1144 115 }
duke@1 116 }
duke@1 117
mcimadamore@1113 118 public Token split() {
mcimadamore@1113 119 Token[] splitTokens = token.split(tokens);
mcimadamore@1113 120 prevToken = splitTokens[0];
mcimadamore@1113 121 token = splitTokens[1];
mcimadamore@1113 122 return token;
duke@1 123 }
duke@1 124
mcimadamore@1113 125 public LineMap getLineMap() {
mcimadamore@1113 126 return tokenizer.getLineMap();
duke@1 127 }
duke@1 128
duke@1 129 public int errPos() {
mcimadamore@1113 130 return tokenizer.errPos();
duke@1 131 }
duke@1 132
duke@1 133 public void errPos(int pos) {
mcimadamore@1113 134 tokenizer.errPos(pos);
duke@1 135 }
duke@1 136 }

mercurial