src/java.base/share/classes/java/util/regex/PrintPattern.java
author igerasim
Tue, 04 Jun 2019 18:55:53 -0700
changeset 55214 7a026580fed5
parent 47216 71c04702a3d5
permissions -rw-r--r--
8225198: Optimize regex tree for greedy quantifiers of type {N,} Reviewed-by: redestad, bchristi
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
     1
/*
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
     4
 *
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    10
 *
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    15
 * accompanied this code).
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    16
 *
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    20
 *
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    23
 * questions.
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    24
 */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    25
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    26
package java.util.regex;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    27
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    28
import java.util.HashMap;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    29
import java.util.regex.Pattern.CharPredicate;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    30
import static java.util.regex.ASCII.*;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    31
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    32
/**
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    33
 * A utility class to print out the pattern node tree.
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    34
 */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    35
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    36
class PrintPattern {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    37
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    38
    private static HashMap<Pattern.Node, Integer> ids = new HashMap<>();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    39
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    40
    private static void print(Pattern.Node node, String text, int depth) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    41
        if (!ids.containsKey(node))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    42
            ids.put(node, ids.size());
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    43
        print("%6d:%" + (depth==0? "": depth<<1) + "s<%s>", ids.get(node), "", text);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    44
        if (ids.containsKey(node.next))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    45
            print(" (=>%d)", ids.get(node.next));
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    46
        print("%n");
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    47
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    48
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    49
    private static void print(String s, int depth) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    50
        print("       %" + (depth==0?"":depth<<1) + "s<%s>%n", "", s);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    51
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    52
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    53
    private static void print(String fmt, Object ... args) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    54
        System.err.printf(fmt, args);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    55
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    56
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    57
    private static String toStringCPS(int[] cps) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    58
        StringBuilder sb = new StringBuilder(cps.length);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    59
        for (int cp : cps)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    60
            sb.append(toStringCP(cp));
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    61
        return sb.toString();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    62
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    63
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    64
    private static String toStringCP(int cp) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    65
        return (isPrint(cp) ? "" + (char)cp
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    66
                            : "\\u" + Integer.toString(cp, 16));
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    67
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    68
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    69
    private static String toStringRange(int min, int max) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    70
       if (max == Pattern.MAX_REPS) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    71
           if (min == 0)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    72
               return " * ";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    73
           else if (min == 1)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    74
               return " + ";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    75
           return "{" + min + ", max}";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    76
       }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    77
       return "{" + min + ", " +  max + "}";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    78
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    79
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    80
    private static String toStringCtype(int type) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    81
        switch(type) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    82
        case UPPER:  return "ASCII.UPPER";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    83
        case LOWER:  return "ASCII.LOWER";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    84
        case DIGIT:  return "ASCII.DIGIT";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    85
        case SPACE:  return "ASCII.SPACE";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    86
        case PUNCT:  return "ASCII.PUNCT";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    87
        case CNTRL:  return "ASCII.CNTRL";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    88
        case BLANK:  return "ASCII.BLANK";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    89
        case UNDER:  return "ASCII.UNDER";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    90
        case ASCII:  return "ASCII.ASCII";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    91
        case ALPHA:  return "ASCII.ALPHA";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    92
        case ALNUM:  return "ASCII.ALNUM";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    93
        case GRAPH:  return "ASCII.GRAPH";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    94
        case WORD:   return "ASCII.WORD";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    95
        case XDIGIT: return "ASCII.XDIGIT";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    96
        default: return "ASCII ?";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    97
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    98
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
    99
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   100
    private static String toString(Pattern.Node node) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   101
        String name = node.getClass().getName();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   102
        return name.substring(name.lastIndexOf('$') + 1);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   103
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   104
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   105
    static HashMap<CharPredicate, String> pmap;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   106
    static {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   107
        pmap = new HashMap<>();
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 37882
diff changeset
   108
        pmap.put(Pattern.ALL(), "All");
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 37882
diff changeset
   109
        pmap.put(Pattern.DOT(), "Dot");
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 37882
diff changeset
   110
        pmap.put(Pattern.UNIXDOT(), "UnixDot");
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 37882
diff changeset
   111
        pmap.put(Pattern.VertWS(), "VertWS");
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 37882
diff changeset
   112
        pmap.put(Pattern.HorizWS(), "HorizWS");
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   113
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 37882
diff changeset
   114
        pmap.put(CharPredicates.ASCII_DIGIT(), "ASCII.DIGIT");
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 37882
diff changeset
   115
        pmap.put(CharPredicates.ASCII_WORD(),  "ASCII.WORD");
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 37882
diff changeset
   116
        pmap.put(CharPredicates.ASCII_SPACE(), "ASCII.SPACE");
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   117
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   118
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   119
    static void walk(Pattern.Node node, int depth) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   120
        depth++;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   121
        while(node != null) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   122
            String name = toString(node);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   123
            String str;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   124
            if (node instanceof Pattern.Prolog) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   125
                print(node, name, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   126
                // print the loop here
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   127
                Pattern.Loop loop = ((Pattern.Prolog)node).loop;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   128
                name = toString(loop);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   129
                str = name + " " + toStringRange(loop.cmin, loop.cmax);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   130
                print(loop, str, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   131
                walk(loop.body, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   132
                print("/" + name, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   133
                node = loop;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   134
            } else if (node instanceof Pattern.Loop) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   135
                return;  // stop here, body.next -> loop
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   136
            } else if (node instanceof Pattern.Curly) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   137
                Pattern.Curly c = (Pattern.Curly)node;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   138
                str = "Curly " + c.type + " " + toStringRange(c.cmin, c.cmax);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   139
                print(node, str, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   140
                walk(c.atom, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   141
                print("/Curly", depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   142
            } else if (node instanceof Pattern.GroupCurly) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   143
                Pattern.GroupCurly gc = (Pattern.GroupCurly)node;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   144
                str = "GroupCurly " + gc.groupIndex / 2 +
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   145
                      ", " + gc.type + " " + toStringRange(gc.cmin, gc.cmax);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   146
                print(node, str, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   147
                walk(gc.atom, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   148
                print("/GroupCurly", depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   149
            } else if (node instanceof Pattern.GroupHead) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   150
                Pattern.GroupHead head = (Pattern.GroupHead)node;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   151
                Pattern.GroupTail tail = head.tail;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   152
                print(head, "Group.head " + (tail.groupIndex / 2), depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   153
                walk(head.next, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   154
                print(tail, "/Group.tail " + (tail.groupIndex / 2), depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   155
                node = tail;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   156
            } else if (node instanceof Pattern.GroupTail) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   157
                return;  // stopper
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   158
            } else if (node instanceof Pattern.Ques) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   159
                print(node, "Ques " + ((Pattern.Ques)node).type, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   160
                walk(((Pattern.Ques)node).atom, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   161
                print("/Ques", depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   162
            } else if (node instanceof Pattern.Branch) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   163
                Pattern.Branch b = (Pattern.Branch)node;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   164
                print(b, name, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   165
                int i = 0;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   166
                while (true) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   167
                    if (b.atoms[i] != null) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   168
                        walk(b.atoms[i], depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   169
                    } else {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   170
                        print("  (accepted)", depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   171
                    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   172
                    if (++i == b.size)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   173
                        break;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   174
                    print("-branch.separator-", depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   175
                }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   176
                node = b.conn;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   177
                print(node, "/Branch", depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   178
            } else if (node instanceof Pattern.BranchConn) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   179
                return;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   180
            } else if (node instanceof Pattern.CharProperty) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   181
                str = pmap.get(((Pattern.CharProperty)node).predicate);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   182
                if (str == null)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   183
                    str = toString(node);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   184
                else
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   185
                    str = "Single \"" + str + "\"";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   186
                print(node, str, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   187
            } else if (node instanceof Pattern.SliceNode) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   188
                str = name + "  \"" +
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   189
                      toStringCPS(((Pattern.SliceNode)node).buffer) + "\"";
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   190
                print(node, str, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   191
            } else if (node instanceof Pattern.CharPropertyGreedy) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   192
                Pattern.CharPropertyGreedy gcp = (Pattern.CharPropertyGreedy)node;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   193
                String pstr = pmap.get(gcp.predicate);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   194
                if (pstr == null)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   195
                    pstr = gcp.predicate.toString();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   196
                else
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   197
                    pstr = "Single \"" + pstr + "\"";
55214
7a026580fed5 8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents: 47216
diff changeset
   198
                str = name + " " + pstr;
7a026580fed5 8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents: 47216
diff changeset
   199
                if (gcp.cmin == 0)
7a026580fed5 8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents: 47216
diff changeset
   200
                    str += "*";
7a026580fed5 8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents: 47216
diff changeset
   201
                else if (gcp.cmin == 1)
7a026580fed5 8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents: 47216
diff changeset
   202
                    str += "+";
7a026580fed5 8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents: 47216
diff changeset
   203
                else
7a026580fed5 8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents: 47216
diff changeset
   204
                    str += "{" + gcp.cmin + ",}";
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   205
                print(node, str, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   206
            } else if (node instanceof Pattern.BackRef) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   207
                str = "GroupBackRef " + ((Pattern.BackRef)node).groupIndex / 2;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   208
                print(node, str, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   209
            } else if (node instanceof Pattern.LastNode) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   210
                print(node, "END", depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   211
            } else if (node == Pattern.accept) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   212
                return;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   213
            } else {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   214
                print(node, name, depth);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   215
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   216
            node = node.next;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   217
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   218
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   219
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   220
    public static void main(String[] args) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   221
        Pattern p = Pattern.compile(args[0]);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   222
        System.out.println("   Pattern: " + p);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   223
        walk(p.root, 0);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   224
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
diff changeset
   225
}