From ab4eb7353f73cf6fe51e97a8a76bb3a5b3b2b20f Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Wed, 16 Nov 2016 01:36:20 +0100 Subject: [PATCH] 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 #4652 --- java/src/processing/mode/java/pdex/SourceUtils.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/java/src/processing/mode/java/pdex/SourceUtils.java b/java/src/processing/mode/java/pdex/SourceUtils.java index 66703b246..8a1a20693 100644 --- a/java/src/processing/mode/java/pdex/SourceUtils.java +++ b/java/src/processing/mode/java/pdex/SourceUtils.java @@ -55,8 +55,12 @@ public class SourceUtils { + // Positive lookahead and lookbehind are needed to match all type constructors + // in code like `int(byte(245))` where first bracket matches as last + // group in "^int(" but also as a first group in "(byte(". Lookahead and + // lookbehind won't consume the shared character. public static final Pattern TYPE_CONSTRUCTOR_REGEX = - Pattern.compile("(?:^|\\W)(int|char|float|boolean|byte)(?:\\s*\\()", + Pattern.compile("(?<=^|\\W)(int|char|float|boolean|byte)(?=\\s*\\()", Pattern.MULTILINE); public static List replaceTypeConstructors(CharSequence source) {