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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

     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  */
    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;
    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     }
    77     protected Scanner(ScannerFactory fac, char[] buf, int inputLength) {
    78         this(fac, new JavaTokenizer(fac, buf, inputLength));
    79     }
    81     protected Scanner(ScannerFactory fac, JavaTokenizer tokenizer) {
    82         this.tokenizer = tokenizer;
    83         tokens = fac.tokens;
    84         token = prevToken = DUMMY;
    85     }
    87     public Token token() {
    88         return token(0);
    89     }
    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         }
   106     public Token prevToken() {
   107         return prevToken;
   108     }
   110     public void nextToken() {
   111         prevToken = token;
   112         if (!savedTokens.isEmpty()) {
   113             token = savedTokens.remove(0);
   114         } else {
   115             token = tokenizer.readToken();
   116         }
   117     }
   119     public Token split() {
   120         Token[] splitTokens = token.split(tokens);
   121         prevToken = splitTokens[0];
   122         token = splitTokens[1];
   123         return token;
   124     }
   126     public LineMap getLineMap() {
   127         return tokenizer.getLineMap();
   128     }
   130     public int errPos() {
   131         return tokenizer.errPos();
   132     }
   134     public void errPos(int pos) {
   135         tokenizer.errPos(pos);
   136     }
   137 }

mercurial