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