|
| 1 | +package org.perlonjava.runtime; |
| 2 | + |
| 3 | +import java.util.AbstractMap; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Set; |
| 8 | +import java.util.regex.Matcher; |
| 9 | +import java.util.regex.Pattern; |
| 10 | +import java.util.regex.PatternSyntaxException; |
| 11 | + |
| 12 | +import static org.perlonjava.runtime.RuntimeScalarCache.scalarUndef; |
| 13 | + |
| 14 | +/** |
| 15 | + * HashSpecialVariable provides a dynamic view over named capturing groups |
| 16 | + * in a Matcher object, reflecting the current state of the Matcher. |
| 17 | + * This implements the Perl special variable %+. |
| 18 | + */ |
| 19 | +public class HashSpecialVariable extends AbstractMap<String, RuntimeScalar> { |
| 20 | + |
| 21 | + private final Matcher matcher; |
| 22 | + private final Map<String, Integer> namedGroups; |
| 23 | + |
| 24 | + /** |
| 25 | + * Constructs a HashSpecialVariable for the given Matcher. |
| 26 | + * |
| 27 | + * @param matcher the Matcher object to query for named capturing groups |
| 28 | + */ |
| 29 | + public HashSpecialVariable(Matcher matcher) { |
| 30 | + this.matcher = matcher; |
| 31 | + this.namedGroups = extractNamedGroups(matcher.pattern()); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Extracts named groups and their indices from the given pattern. |
| 36 | + * |
| 37 | + * @param pattern the regex pattern |
| 38 | + * @return a map of named group names to their indices |
| 39 | + */ |
| 40 | + private Map<String, Integer> extractNamedGroups(Pattern pattern) { |
| 41 | + Map<String, Integer> namedGroups = new HashMap<>(); |
| 42 | + String regex = pattern.toString(); |
| 43 | + Matcher groupMatcher = Pattern.compile("\\(\\?<([a-zA-Z][a-zA-Z0-9]*)>").matcher(regex); |
| 44 | + int index = 1; // Group indices start at 1 |
| 45 | + |
| 46 | + while (groupMatcher.find()) { |
| 47 | + String groupName = groupMatcher.group(1); |
| 48 | + namedGroups.put(groupName, index++); |
| 49 | + } |
| 50 | + |
| 51 | + return namedGroups; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public Set<Entry<String, RuntimeScalar>> entrySet() { |
| 56 | + Set<Entry<String, RuntimeScalar>> entries = new HashSet<>(); |
| 57 | + for (String name : namedGroups.keySet()) { |
| 58 | + int groupIndex = namedGroups.get(name); |
| 59 | + if (groupIndex != -1 && matcher.group(name) != null) { |
| 60 | + entries.add(new SimpleEntry<>(name, new RuntimeScalar(matcher.group(name)))); |
| 61 | + } |
| 62 | + } |
| 63 | + return entries; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public RuntimeScalar get(Object key) { |
| 68 | + if (key instanceof String) { |
| 69 | + String name = (String) key; |
| 70 | + int groupIndex = namedGroups.getOrDefault(name, -1); |
| 71 | + if (groupIndex != -1 && matcher.group(groupIndex) != null) { |
| 72 | + return new RuntimeScalar(matcher.group(groupIndex)); |
| 73 | + } |
| 74 | + } |
| 75 | + return scalarUndef; |
| 76 | + } |
| 77 | +} |
0 commit comments