2

Some background:

I enjoy doing code-golf challenges in Java, where the goal is to do a certain task in as few bytes/characters as possible. As a simply example: checking if an integer n is a prime with an as short as possible (lambda) function is 47 bytes:

n->{for(int i=2;i<n;n=n%i++<1?0:n);return n>1;}

Try it online.

Code-golf answers are usually very unreadable, lacking white-spaces and new-lines, only using single-character variable, method and class names, might give compiler warnings, etc. But we don't care about none of that as long as it's as short as possible in code-golfing.

So, that's code-golfing in a nutshell. Although I've already answered loads of code-golfing challenges in Java, there is one function that I wish Java had on multiple occasions: Using the capture group of a regex for something similar to JavaScript or Retina.

For example, it would be great if these things would be possible in Java:
A:

// Converting the capture group to an Integer:
s->n->s.replaceAll("\\D*(\\d+)",new Integer("$1")+n+"")
// i.e. "test123" & 7 will become 130 (123 + 7)

// Getting the length of a capture group:
s->s.replaceAll("(\\D*(\\d+))",new Integer("$2")+"$1".length()+"")
// i.e. "test123" will become 130 (123 + the length of "test123")

// Getting a character or substring of a capture group:
s->s.replaceAll(".(.+)",s+"$1".charAt(0))
// i.e. "test" will become "teste" ("test" + "est".charAt(0))

Of course these things above are possible with some small modifications:
B:

s->n->new Integer(s.replaceAll("\\D*(\\d+)","$1"))+n+""
s->new Integer(s.replaceAll("(\\D*(\\d+))","$2"))+s.replaceAll("(\\D*(\\d+))","$1").length()+""
s->s.replaceAll(".(.+)",s)+s.replaceAll(".(.+)","$1").charAt(0)

Try it online.

Question itself:

Now my question: Is it possible to create a plugin that modifies the compiler code, so the replaceAll with capture groups displayed at A above would be modified to those displayed at B. Basically creating a plugin/library/compiler-flags or anything that modifies the pre-compiled code when capture groups are used in String#replaceAll or String#replaceFirst specifically.

PS: I'm not sure yet whether the code at A is how I want it in the end, maybe I'll make it more like JavaScript (or Retina) instead:

// Syntax used above at A:
s->s.replaceAll("\\D*(\\d+)","$1".length()+"")
// JavaScript syntax alternative:
s->s.replaceAll("\\D*(\\d+)",a->a.length()+"") // a-> is the capture group 1
// Retina syntax alternative:
s->s.replaceAll("\\D*(\\d+)",new Integer("$#1")+"") // $#1 is the length of capture group 1

but that's irrelevant for the question itself: how to create a pre-compile plugin doing a certain modification to the String#replaceAll(...,"$#") or String#replaceFirst(...,"$#") parts of the code. (And whether this is even possible to begin with.)

Community
  • 1
  • 1
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • The 2nd "Try it online" link throws errors – c0der Apr 11 '18 at 16:32
  • @c0der I know, that's the point. I currently catch the error of the code I want to work with this pre-compiler plugin, and use the longer work-around versions instead to give the results. As you can see the errors are catched and printed to STDOUT, not uncaught and thrown at STDERR (debug-window on TIO). – Kevin Cruijssen Apr 11 '18 at 17:16

1 Answers1

0

I never visited Code Golf but I think , you need a Transpiler. Google for "Building Transpiler". You can also approach for Compiler or Interpreter. Use ANTLR to specify A and B grammars OR hardcode it in java then map them according to lexer and parser. This is not difficult but i think lengthy process for an individual because you need to map everything offered by B in A.