langtools/test/tools/javac/lexer/JavaLexerTest.java
changeset 34997 8174a7d851fb
child 35359 f04501964016
equal deleted inserted replaced
34996:ed25a4c782c2 34997:8174a7d851fb
       
     1 /*
       
     2  * Copyright (c) 2016, 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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 8056897
       
    27  * @summary Proper lexing of integer literals.
       
    28  */
       
    29 
       
    30 import java.io.IOException;
       
    31 import java.net.URI;
       
    32 import java.util.Objects;
       
    33 
       
    34 import javax.tools.JavaFileObject;
       
    35 import javax.tools.SimpleJavaFileObject;
       
    36 
       
    37 import com.sun.tools.javac.parser.JavaTokenizer;
       
    38 import com.sun.tools.javac.parser.ScannerFactory;
       
    39 import com.sun.tools.javac.parser.Tokens.Token;
       
    40 import com.sun.tools.javac.parser.Tokens.TokenKind;
       
    41 import com.sun.tools.javac.util.Context;
       
    42 import com.sun.tools.javac.util.Log;
       
    43 
       
    44 public class JavaLexerTest {
       
    45     public static void main(String... args) throws Exception {
       
    46         new JavaLexerTest().run();
       
    47     }
       
    48 
       
    49     void run() throws Exception {
       
    50         Context ctx = new Context();
       
    51         Log log = Log.instance(ctx);
       
    52         String input = "0bL 0b20L 0xL ";
       
    53         log.useSource(new SimpleJavaFileObject(new URI("mem://Test.java"), JavaFileObject.Kind.SOURCE) {
       
    54             @Override
       
    55             public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
       
    56                 return input;
       
    57             }
       
    58         });
       
    59         char[] inputArr = input.toCharArray();
       
    60         JavaTokenizer tokenizer = new JavaTokenizer(ScannerFactory.instance(ctx), inputArr, inputArr.length) {
       
    61         };
       
    62 
       
    63         assertKind(input, tokenizer, TokenKind.LONGLITERAL, "0bL");
       
    64         assertKind(input, tokenizer, TokenKind.LONGLITERAL, "0b20L");
       
    65         assertKind(input, tokenizer, TokenKind.LONGLITERAL, "0xL");
       
    66     }
       
    67 
       
    68     void assertKind(String input, JavaTokenizer tokenizer, TokenKind kind, String expectedText) {
       
    69         Token token = tokenizer.readToken();
       
    70 
       
    71         if (token.kind != kind) {
       
    72             throw new AssertionError("Unexpected token kind: " + token.kind);
       
    73         }
       
    74 
       
    75         String actualText = input.substring(token.pos, token.endPos);
       
    76 
       
    77         if (!Objects.equals(actualText, expectedText)) {
       
    78             throw new AssertionError("Unexpected token text: " + actualText);
       
    79         }
       
    80     }
       
    81 }