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

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1521
71f35e4b93a5
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

duke@1 1 /*
jjg@1521 2 * Copyright (c) 1999, 2013, 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;
jjg@1521 62
duke@1 63 /**
duke@1 64 * Create a scanner from the input array. This method might
duke@1 65 * modify the array. To avoid copying the input array, ensure
duke@1 66 * that {@code inputLength < input.length} or
duke@1 67 * {@code input[input.length -1]} is a white space character.
duke@1 68 *
duke@1 69 * @param fac the factory which created this Scanner
jjg@1358 70 * @param buf the input, might be modified
duke@1 71 * Must be positive and less than or equal to input.length.
duke@1 72 */
mcimadamore@1113 73 protected Scanner(ScannerFactory fac, CharBuffer buf) {
mcimadamore@1113 74 this(fac, new JavaTokenizer(fac, buf));
duke@1 75 }
duke@1 76
mcimadamore@1113 77 protected Scanner(ScannerFactory fac, char[] buf, int inputLength) {
mcimadamore@1113 78 this(fac, new JavaTokenizer(fac, buf, inputLength));
duke@1 79 }
duke@1 80
mcimadamore@1113 81 protected Scanner(ScannerFactory fac, JavaTokenizer tokenizer) {
mcimadamore@1113 82 this.tokenizer = tokenizer;
mcimadamore@1113 83 tokens = fac.tokens;
mcimadamore@1113 84 token = prevToken = DUMMY;
duke@1 85 }
duke@1 86
duke@1 87 public Token token() {
mcimadamore@1144 88 return token(0);
duke@1 89 }
duke@1 90
mcimadamore@1144 91 public Token token(int lookahead) {
mcimadamore@1144 92 if (lookahead == 0) {
mcimadamore@1144 93 return token;
mcimadamore@1144 94 } else {
mcimadamore@1144 95 ensureLookahead(lookahead);
mcimadamore@1144 96 return savedTokens.get(lookahead - 1);
mcimadamore@1144 97 }
mcimadamore@1144 98 }
mcimadamore@1144 99 //where
mcimadamore@1144 100 private void ensureLookahead(int lookahead) {
mcimadamore@1144 101 for (int i = savedTokens.size() ; i < lookahead ; i ++) {
mcimadamore@1144 102 savedTokens.add(tokenizer.readToken());
mcimadamore@1144 103 }
mcimadamore@1144 104 }
mcimadamore@1144 105
mcimadamore@1113 106 public Token prevToken() {
mcimadamore@1113 107 return prevToken;
duke@1 108 }
duke@1 109
mcimadamore@1113 110 public void nextToken() {
mcimadamore@1113 111 prevToken = token;
mcimadamore@1144 112 if (!savedTokens.isEmpty()) {
mcimadamore@1144 113 token = savedTokens.remove(0);
mcimadamore@1144 114 } else {
mcimadamore@1144 115 token = tokenizer.readToken();
mcimadamore@1144 116 }
duke@1 117 }
duke@1 118
mcimadamore@1113 119 public Token split() {
mcimadamore@1113 120 Token[] splitTokens = token.split(tokens);
mcimadamore@1113 121 prevToken = splitTokens[0];
mcimadamore@1113 122 token = splitTokens[1];
mcimadamore@1113 123 return token;
duke@1 124 }
duke@1 125
mcimadamore@1113 126 public LineMap getLineMap() {
mcimadamore@1113 127 return tokenizer.getLineMap();
duke@1 128 }
duke@1 129
duke@1 130 public int errPos() {
mcimadamore@1113 131 return tokenizer.errPos();
duke@1 132 }
duke@1 133
duke@1 134 public void errPos(int pos) {
mcimadamore@1113 135 tokenizer.errPos(pos);
duke@1 136 }
duke@1 137 }

mercurial