aboutsummaryrefslogtreecommitdiff
path: root/javaparser-core-generators/src/main/java/com/github/javaparser/generator/core/other/GrammarLetterGenerator.java
blob: 9d50bc6769c8f2acd85773a359de2b87546d2bd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.github.javaparser.generator.core.other;

import java.util.function.Function;

/**
 * Prints the LETTER and PART_LETTER tokens. They should be inserted into the grammar manually.
 */
public class GrammarLetterGenerator {
    public static void main(String[] args) {
        generate("LETTER", c -> Character.isJavaIdentifierStart(c) || Character.isHighSurrogate((char) (int) c) || Character.isLowSurrogate((char) (int) c));
        generate("PART_LETTER", c -> Character.isJavaIdentifierPart(c) || Character.isHighSurrogate((char) (int) c) || Character.isLowSurrogate((char) (int) c));
    }

    private static void generate(String tokenName, Function<Integer, Boolean> f) {
        final String indent = "         ";
        System.out.println("  < #" + tokenName + ": [");
        System.out.print(indent);
        int nltime = 0;
        int i = 0;
        while (i < 0x10000) {
            while (!f.apply(i) && i < 0x10000) {
                i++;
            }
            String start = format(i);
            while (f.apply(i) && i < 0x10000) {
                i++;
            }
            String end = format(i - 1);
            if (i >= 0x10000) {
                break;
            }
            if (start.equals(end)) {
                nltime++;
                System.out.print(start + ",  ");
            } else {
                nltime += 2;
                System.out.print(start + "-" + end + ",  ");
            }
            if (nltime >= 10) {
                nltime = 0;
                System.out.println();
                System.out.print(indent);
            }
        }
        // Too lazy to remove the final illegal comma.
        System.out.println("]");
        System.out.println("  >");
    }

    private static String format(int i) {
        return String.format("\"\\u%04x\"", i);
    }
}