langtools/test/tools/javac/lexer/JavaLexerTest.java
author ysuenaga
Wed, 30 Mar 2016 21:05:13 +0900
changeset 37218 c7241bc368bf
parent 35359 f04501964016
permissions -rw-r--r--
8151674: STW phases at Concurrent GC should count in PerfCounter Reviewed-by: jmasa, sla, tschatzl
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34997
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
     1
/*
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
     4
 *
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
     8
 *
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    14
 *
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    18
 *
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    21
 * questions.
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    22
 */
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    23
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    24
/**
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    25
 * @test
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    26
 * @bug 8056897
35359
f04501964016 8148147: Sync up @modules from jigsaw/jake
alanb
parents: 34997
diff changeset
    27
 * @modules jdk.compiler/com.sun.tools.javac.parser
f04501964016 8148147: Sync up @modules from jigsaw/jake
alanb
parents: 34997
diff changeset
    28
 *          jdk.compiler/com.sun.tools.javac.util
34997
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    29
 * @summary Proper lexing of integer literals.
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    30
 */
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    31
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    32
import java.io.IOException;
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    33
import java.net.URI;
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    34
import java.util.Objects;
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    35
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    36
import javax.tools.JavaFileObject;
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    37
import javax.tools.SimpleJavaFileObject;
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    38
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    39
import com.sun.tools.javac.parser.JavaTokenizer;
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    40
import com.sun.tools.javac.parser.ScannerFactory;
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    41
import com.sun.tools.javac.parser.Tokens.Token;
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    42
import com.sun.tools.javac.parser.Tokens.TokenKind;
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    43
import com.sun.tools.javac.util.Context;
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    44
import com.sun.tools.javac.util.Log;
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    45
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    46
public class JavaLexerTest {
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    47
    public static void main(String... args) throws Exception {
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    48
        new JavaLexerTest().run();
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    49
    }
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    50
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    51
    void run() throws Exception {
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    52
        Context ctx = new Context();
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    53
        Log log = Log.instance(ctx);
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    54
        String input = "0bL 0b20L 0xL ";
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    55
        log.useSource(new SimpleJavaFileObject(new URI("mem://Test.java"), JavaFileObject.Kind.SOURCE) {
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    56
            @Override
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    57
            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    58
                return input;
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    59
            }
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    60
        });
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    61
        char[] inputArr = input.toCharArray();
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    62
        JavaTokenizer tokenizer = new JavaTokenizer(ScannerFactory.instance(ctx), inputArr, inputArr.length) {
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    63
        };
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    64
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    65
        assertKind(input, tokenizer, TokenKind.LONGLITERAL, "0bL");
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    66
        assertKind(input, tokenizer, TokenKind.LONGLITERAL, "0b20L");
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    67
        assertKind(input, tokenizer, TokenKind.LONGLITERAL, "0xL");
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    68
    }
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    69
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    70
    void assertKind(String input, JavaTokenizer tokenizer, TokenKind kind, String expectedText) {
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    71
        Token token = tokenizer.readToken();
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    72
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    73
        if (token.kind != kind) {
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    74
            throw new AssertionError("Unexpected token kind: " + token.kind);
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    75
        }
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    76
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    77
        String actualText = input.substring(token.pos, token.endPos);
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    78
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    79
        if (!Objects.equals(actualText, expectedText)) {
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    80
            throw new AssertionError("Unexpected token text: " + actualText);
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    81
        }
8174a7d851fb 8056897: Improve error recovery for empty binary and hexadecimal literals.
jlahoda
parents:
diff changeset
    82
    }
35359
f04501964016 8148147: Sync up @modules from jigsaw/jake
alanb
parents: 34997
diff changeset
    83
}