diff mbox series

[v1,2/3] userdiff: support Java record types

Message ID 20230203232339.216592-3-rybak.a.v@gmail.com (mailing list archive)
State Superseded
Headers show
Series userdiff: Java updates | expand

Commit Message

Andrei Rybak Feb. 3, 2023, 11:23 p.m. UTC
A new kind of class was added in Java 16 -- records.[1]  The syntax of
records is similar to regular classes with one important distinction:
the name of the record class is followed by a mandatory list of
components.  The list is enclosed in parentheses, it may be empty, and
it may immediately follow the name of the class or type parameters, if
any, without separating whitespace.

Code examples:

    public record Example(int i, String s) {
    }

    public record WithTypeParameters<A, B>(A a, B b, String s) {
    }

Support records in the builtin userdiff pattern for Java.  Add "record"
to the alternatives of keywords for kinds of class, and match an opening
parenthesis as the first character right after the type name.

An alternative approach could be to have an optional group that would
match both the opening and the closing parentheses with some way of
matching the declarations of the components.  Just use the simpler
regular expression for now.

[1] detailed description is available in "JEP 395: Records"
    https://openjdk.org/jeps/395

Signed-off-by: Andrei Rybak <rybak.a.v@gmail.com>
---
 t/t4018/java-record                 | 6 ++++++
 t/t4018/java-record-type-parameters | 6 ++++++
 userdiff.c                          | 2 +-
 3 files changed, 13 insertions(+), 1 deletion(-)
 create mode 100644 t/t4018/java-record
 create mode 100644 t/t4018/java-record-type-parameters
diff mbox series

Patch

diff --git a/t/t4018/java-record b/t/t4018/java-record
new file mode 100644
index 0000000000..97aa819dd8
--- /dev/null
+++ b/t/t4018/java-record
@@ -0,0 +1,6 @@ 
+public record RIGHT(int comp1, double comp2, String comp3) {
+    static int ONE;
+    static int TWO;
+    static int THREE;
+    static int ChangeMe;
+}
diff --git a/t/t4018/java-record-type-parameters b/t/t4018/java-record-type-parameters
new file mode 100644
index 0000000000..f62a035cc8
--- /dev/null
+++ b/t/t4018/java-record-type-parameters
@@ -0,0 +1,6 @@ 
+public record RIGHT<A, N extends Number>(A comp1, N comp2, int comp3) {
+    static int ONE;
+    static int TWO;
+    static int THREE;
+    static int ChangeMe;
+}
diff --git a/userdiff.c b/userdiff.c
index 759e22ffff..f92b3029aa 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -171,7 +171,7 @@  PATTERNS("html",
 PATTERNS("java",
 	 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
 	 /* Class, enum, and interface declarations */
-	 "^[ \t]*(([a-z]+[ \t]+)*(class|enum|interface)[ \t]+[A-Za-z][A-Za-z0-9_$]*([ \t]+|<).*)$\n"
+	 "^[ \t]*(([a-z]+[ \t]+)*(class|enum|interface|record)[ \t]+[A-Za-z][A-Za-z0-9_$]*([ \t]+|[<(]).*)$\n"
 	 /* Method definitions; note that constructor signatures are not */
 	 /* matched because they are indistinguishable from method calls. */
 	 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",