Skip to content

Commit 4336026

Browse files
committed
Fix nested type constructors raising error
Replace non-capturing groups by positive lookahead and lookbehind, because they don't consume the source String and allow for first and last group to overlap when two constructors are directly nested. Fixes processing#4652
1 parent e426579 commit 4336026

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

java/src/processing/mode/java/pdex/SourceUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ public static List<Edit> parseProgramImports(CharSequence source,
5555

5656

5757

58+
// Positive lookahead and lookbehind are needed to match all type constructors
59+
// in code like `int(byte(245))` where first bracket matches as last
60+
// group in "^int(" but also as a first group in "(byte(". Lookahead and
61+
// lookbehind won't consume the shared character.
5862
public static final Pattern TYPE_CONSTRUCTOR_REGEX =
59-
Pattern.compile("(?:^|\\W)(int|char|float|boolean|byte)(?:\\s*\\()",
63+
Pattern.compile("(?<=^|\\W)(int|char|float|boolean|byte)(?=\\s*\\()",
6064
Pattern.MULTILINE);
6165

6266
public static List<Edit> replaceTypeConstructors(CharSequence source) {

0 commit comments

Comments
 (0)