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

Mon, 14 Nov 2011 15:11:10 -0800

author
ksrini
date
Mon, 14 Nov 2011 15:11:10 -0800
changeset 1138
7375d4979bd3
parent 1113
d346ab55031b
child 1144
9448fe783fd2
permissions
-rw-r--r--

7106166: (javac) re-factor EndPos parser
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 1999, 2011, 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.*;
    30 import com.sun.tools.javac.util.*;
    31 import com.sun.tools.javac.util.Position.LineMap;
    32 import com.sun.tools.javac.parser.JavaTokenizer.*;
    34 import static com.sun.tools.javac.parser.Tokens.*;
    36 /** The lexical analyzer maps an input stream consisting of
    37  *  ASCII characters and Unicode escapes into a token sequence.
    38  *
    39  *  <p><b>This is NOT part of any supported API.
    40  *  If you write code that depends on this, you do so at your own risk.
    41  *  This code and its internal interfaces are subject to change or
    42  *  deletion without notice.</b>
    43  */
    44 public class Scanner implements Lexer {
    46     private Tokens tokens;
    48     /** The token, set by nextToken().
    49      */
    50     private Token token;
    52     /** The previous token, set by nextToken().
    53      */
    54     private Token prevToken;
    56     private JavaTokenizer tokenizer;
    57     /**
    58      * Create a scanner from the input array.  This method might
    59      * modify the array.  To avoid copying the input array, ensure
    60      * that {@code inputLength < input.length} or
    61      * {@code input[input.length -1]} is a white space character.
    62      *
    63      * @param fac the factory which created this Scanner
    64      * @param input the input, might be modified
    65      * @param inputLength the size of the input.
    66      * Must be positive and less than or equal to input.length.
    67      */
    68     protected Scanner(ScannerFactory fac, CharBuffer buf) {
    69         this(fac, new JavaTokenizer(fac, buf));
    70     }
    72     protected Scanner(ScannerFactory fac, char[] buf, int inputLength) {
    73         this(fac, new JavaTokenizer(fac, buf, inputLength));
    74     }
    76     protected Scanner(ScannerFactory fac, JavaTokenizer tokenizer) {
    77         this.tokenizer = tokenizer;
    78         tokens = fac.tokens;
    79         token = prevToken = DUMMY;
    80     }
    82     public Token token() {
    83         return token;
    84     }
    86     public Token prevToken() {
    87         return prevToken;
    88     }
    90     public void nextToken() {
    91         prevToken = token;
    92         token = tokenizer.readToken();
    93     }
    95     public Token split() {
    96         Token[] splitTokens = token.split(tokens);
    97         prevToken = splitTokens[0];
    98         token = splitTokens[1];
    99         return token;
   100     }
   102     public LineMap getLineMap() {
   103         return tokenizer.getLineMap();
   104     }
   106     public int errPos() {
   107         return tokenizer.errPos();
   108     }
   110     public void errPos(int pos) {
   111         tokenizer.errPos(pos);
   112     }
   113 }

mercurial