A parser of regular expression patterns. The only public entry point is #parse(String pattern, int flags).
| 23 | * The only public entry point is {@link #parse(String pattern, int flags)}. |
| 24 | */ |
| 25 | class Parser { |
| 26 | |
| 27 | // Unexpected error |
| 28 | private static final String ERR_INTERNAL_ERROR = "regexp/syntax: internal error"; |
| 29 | |
| 30 | // Parse errors |
| 31 | private static final String ERR_INVALID_CHAR_RANGE = "invalid character class range"; |
| 32 | private static final String ERR_INVALID_ESCAPE = "invalid escape sequence"; |
| 33 | private static final String ERR_INVALID_NAMED_CAPTURE = "invalid named capture"; |
| 34 | private static final String ERR_INVALID_PERL_OP = "invalid or unsupported Perl syntax"; |
| 35 | private static final String ERR_INVALID_REPEAT_OP = "invalid nested repetition operator"; |
| 36 | private static final String ERR_INVALID_REPEAT_SIZE = "invalid repeat count"; |
| 37 | private static final String ERR_MISSING_BRACKET = "missing closing ]"; |
| 38 | private static final String ERR_MISSING_PAREN = "missing closing )"; |
| 39 | private static final String ERR_MISSING_REPEAT_ARGUMENT = |
| 40 | "missing argument to repetition operator"; |
| 41 | private static final String ERR_TRAILING_BACKSLASH = "trailing backslash at end of expression"; |
| 42 | private static final String ERR_DUPLICATE_NAMED_CAPTURE = "duplicate capture group name"; |
| 43 | |
| 44 | // Hack to expose ArrayList.removeRange(). |
| 45 | private static class Stack extends ArrayList<Regexp> { |
| 46 | @Override |
| 47 | public void removeRange(int fromIndex, int toIndex) { |
| 48 | super.removeRange(fromIndex, toIndex); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private final String wholeRegexp; |
| 53 | // Flags control the behavior of the parser and record information about |
| 54 | // regexp context. |
| 55 | private int flags; // parse mode flags |
| 56 | |
| 57 | // Stack of parsed expressions. |
| 58 | private final Stack stack = new Stack(); |
| 59 | private Regexp free; |
| 60 | private int numCap = 0; // number of capturing groups seen |
| 61 | private final Map<String, Integer> namedGroups = new HashMap<String, Integer>(); |
| 62 | |
| 63 | Parser(String wholeRegexp, int flags) { |
| 64 | this.wholeRegexp = wholeRegexp; |
| 65 | this.flags = flags; |
| 66 | } |
| 67 | |
| 68 | // Allocate a Regexp, from the free list if possible. |
| 69 | private Regexp newRegexp(Regexp.Op op) { |
| 70 | Regexp re = free; |
| 71 | if (re != null && re.subs != null && re.subs.length > 0) { |
| 72 | free = re.subs[0]; |
| 73 | re.reinit(); |
| 74 | re.op = op; |
| 75 | } else { |
| 76 | re = new Regexp(op); |
| 77 | } |
| 78 | return re; |
| 79 | } |
| 80 | |
| 81 | private void reuse(Regexp re) { |
| 82 | if (re.subs != null && re.subs.length > 0) { |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…