(StringIterator t)
| 1038 | // Pre: t at "(?". Post: t after ")". |
| 1039 | // Sets numCap. |
| 1040 | private void parsePerlFlags(StringIterator t) throws PatternSyntaxException { |
| 1041 | int startPos = t.pos(); |
| 1042 | |
| 1043 | // Check for named captures, first introduced in Python's regexp library. |
| 1044 | // As usual, there are three slightly different syntaxes: |
| 1045 | // |
| 1046 | // (?P<name>expr) the original, introduced by Python |
| 1047 | // (?<name>expr) the .NET alteration, adopted by Perl 5.10 |
| 1048 | // (?'name'expr) another .NET alteration, adopted by Perl 5.10 |
| 1049 | // |
| 1050 | // Perl 5.10 gave in and implemented the Python version too, |
| 1051 | // but they claim that the last two are the preferred forms. |
| 1052 | // PCRE and languages based on it (specifically, PHP and Ruby) |
| 1053 | // support all three as well. EcmaScript 4 uses only the Python form. |
| 1054 | // |
| 1055 | // In both the open source world (via Code Search) and the |
| 1056 | // Google source tree, (?P<name>expr) and (?<name>expr) are the |
| 1057 | // dominant forms of named captures and both are supported. |
| 1058 | String s = t.rest(); |
| 1059 | if (s.startsWith("(?P<") || s.startsWith("(?<")) { |
| 1060 | // Pull out name. |
| 1061 | int begin = s.charAt(2) == 'P' ? 4 : 3; |
| 1062 | int end = s.indexOf('>', begin); |
| 1063 | if (end < 0) { |
| 1064 | throw new PatternSyntaxException(ERR_INVALID_NAMED_CAPTURE, s); |
| 1065 | } |
| 1066 | String name = s.substring(begin, end); // "name" |
| 1067 | t.skipString(name); |
| 1068 | t.skip(begin + 1); // "(?P<>" or "(?<>" |
| 1069 | if (!isValidCaptureName(name)) { |
| 1070 | throw new PatternSyntaxException( |
| 1071 | ERR_INVALID_NAMED_CAPTURE, s.substring(0, end + 1)); // "(?P<name>" or "(?<name>" |
| 1072 | } |
| 1073 | // Like ordinary capture, but named. |
| 1074 | Regexp re = op(Regexp.Op.LEFT_PAREN); |
| 1075 | re.cap = ++numCap; |
| 1076 | if (namedGroups.put(name, numCap) != null) { |
| 1077 | throw new PatternSyntaxException(ERR_DUPLICATE_NAMED_CAPTURE, name); |
| 1078 | } |
| 1079 | re.name = name; |
| 1080 | return; |
| 1081 | } |
| 1082 | |
| 1083 | // Non-capturing group. Might also twiddle Perl flags. |
| 1084 | t.skip(2); // "(?" |
| 1085 | int flags = this.flags; |
| 1086 | int sign = +1; |
| 1087 | boolean sawFlag = false; |
| 1088 | loop: |
| 1089 | while (t.more()) { |
| 1090 | int c = t.pop(); |
| 1091 | switch (c) { |
| 1092 | default: |
| 1093 | break loop; |
| 1094 | |
| 1095 | // Flags. |
| 1096 | case 'i': |
| 1097 | flags |= RE2.FOLD_CASE; |
no test coverage detected