MCPcopy Create free account
hub / github.com/google/re2j / parsePerlFlags

Method parsePerlFlags

java/com/google/re2j/Parser.java:1040–1144  ·  view source on GitHub ↗
(StringIterator t)

Source from the content-addressed store, hash-verified

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;

Callers 1

parseInternalMethod · 0.95

Calls 12

isValidCaptureNameMethod · 0.95
opMethod · 0.95
posMethod · 0.80
restMethod · 0.80
substringMethod · 0.80
skipStringMethod · 0.80
putMethod · 0.80
moreMethod · 0.80
fromMethod · 0.80
indexOfMethod · 0.45
skipMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected