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

Wed, 10 Oct 2012 18:44:21 -0700

author
jjg
date
Wed, 10 Oct 2012 18:44:21 -0700
changeset 1362
c46e0c9940d6
parent 1358
fc123bdeddb8
child 1521
71f35e4b93a5
permissions
-rw-r--r--

8000310: Clean up use of StringBuffer in langtools
Reviewed-by: bpatel

     1 /*
     2  * Copyright (c) 1999, 2012, 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  */
    26 package com.sun.tools.javac.parser;
    28 import java.nio.*;
    29 import java.util.List;
    30 import java.util.ArrayList;
    32 import com.sun.tools.javac.util.Position.LineMap;
    33 import com.sun.tools.javac.parser.JavaTokenizer.*;
    35 import static com.sun.tools.javac.parser.Tokens.*;
    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  */
    45 public class Scanner implements Lexer {
    47     private Tokens tokens;
    49     /** The token, set by nextToken().
    50      */
    51     private Token token;
    53     /** The previous token, set by nextToken().
    54      */
    55     private Token prevToken;
    57     /** Buffer of saved tokens (used during lookahead)
    58      */
    59     private List<Token> savedTokens = new ArrayList<Token>();
    61     private JavaTokenizer tokenizer;
    62     /**
    63      * Create a scanner from the input array.  This method might
    64      * modify the array.  To avoid copying the input array, ensure
    65      * that {@code inputLength < input.length} or
    66      * {@code input[input.length -1]} is a white space character.
    67      *
    68      * @param fac the factory which created this Scanner
    69      * @param buf the input, might be modified
    70      * Must be positive and less than or equal to input.length.
    71      */
    72     protected Scanner(ScannerFactory fac, CharBuffer buf) {
    73         this(fac, new JavaTokenizer(fac, buf));
    74     }
    76     protected Scanner(ScannerFactory fac, char[] buf, int inputLength) {
    77         this(fac, new JavaTokenizer(fac, buf, inputLength));
    78     }
    80     protected Scanner(ScannerFactory fac, JavaTokenizer tokenizer) {
    81         this.tokenizer = tokenizer;
    82         tokens = fac.tokens;
    83         token = prevToken = DUMMY;
    84     }
    86     public Token token() {
    87         return token(0);
    88     }
    90     public Token token(int lookahead) {
    91         if (lookahead == 0) {
    92             return token;
    93         } else {
    94             ensureLookahead(lookahead);
    95             return savedTokens.get(lookahead - 1);
    96         }
    97     }
    98     //where
    99         private void ensureLookahead(int lookahead) {
   100             for (int i = savedTokens.size() ; i < lookahead ; i ++) {
   101                 savedTokens.add(tokenizer.readToken());
   102             }
   103         }
   105     public Token prevToken() {
   106         return prevToken;
   107     }
   109     public void nextToken() {
   110         prevToken = token;
   111         if (!savedTokens.isEmpty()) {
   112             token = savedTokens.remove(0);
   113         } else {
   114             token = tokenizer.readToken();
   115         }
   116     }
   118     public Token split() {
   119         Token[] splitTokens = token.split(tokens);
   120         prevToken = splitTokens[0];
   121         token = splitTokens[1];
   122         return token;
   123     }
   125     public LineMap getLineMap() {
   126         return tokenizer.getLineMap();
   127     }
   129     public int errPos() {
   130         return tokenizer.errPos();
   131     }
   133     public void errPos(int pos) {
   134         tokenizer.errPos(pos);
   135     }
   136 }

mercurial