1

In Java, is it possible to generate the corresponding curly brace for a sample of Java source code, given the source code as a string, as well as the start of the curly brace?

public class FindFunctionStartAndEnd{
    public static Object findCorrespondingCurlyBrace(String sourceCode, string startingPosition){
        //return the string position that corresponds to the matching curly brace
        //the input string should be the source code of a Java class
        //if sourceCode is not valid Java code, return false
    }
}
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • Many Java IDEs (such as Eclipse) highlight the corresponding curly brace when the cursor is in front of one of the curly braces. I'm curious about how most IDEs would implement this feature. – Anderson Green Feb 21 '13 at 00:40
  • @OliCharlesworth Which parser can be used for this purpose, and who are "they"? – Anderson Green Feb 21 '13 at 00:40
  • In order to do correct matching of curly braces, I'd need to find a way to handle curly braces inside string literals, like in this example: public static void printStuff(){ System.out.println("This method could be tricky to parse. }")} – Anderson Green Feb 21 '13 at 00:46
  • 1
    This is a more complex question: http://stackoverflow.com/q/5497210/422353 – madth3 Feb 21 '13 at 00:46
  • @OliCharlesworth (It appears that there are many Java parsing libraries).[https://www.google.com/#hl=en&safe=active&sclient=psy-ab&q=java+parser&oq=java+parser&gs_l=hp.3..0l4.7328.161163.1.161359.13.11.0.2.2.0.155.1159.7j4.11.0.les%3B..0.0...1c.1.4.psy-ab.ZeP47JVbDq0&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&bvm=bv.42661473,d.dmQ&fp=50b26647ad5972f8&biw=1366&bih=596] Which one are you referring to? – Anderson Green Feb 21 '13 at 00:51
  • 1
    @AndersonGreen Why don't you look into Eclipse's source code? (Though I believe the code will not be easy to read.) – Alvin Wong Feb 21 '13 at 00:52
  • @AlvinWong Also, I'm not sure why this question was downvoted. Is there anything I can do to improve it, and is there anything wrong with it (in the way that it's currently written?) – Anderson Green Feb 21 '13 at 00:54
  • This question was downvoted (not by me) because it shows no effort. It almost makes me wonder if this is a homework assignment. A person who is genuinely interested in solving this problem would do some research to figure out how to approach it. As the comments indicate, a small amount of research should lead you to check out some existing, open-source solutions. The first thing I would do would be to check out the Eclipse source code, which has been mentioned. – jahroy Feb 21 '13 at 00:59
  • @AndersonGreen I didn't downvote this question and I wouldn't, so don't ask me. – Alvin Wong Feb 21 '13 at 01:01
  • possible duplicate of [Java : parse java source code, extract methods](http://stackoverflow.com/questions/2206065/java-parse-java-source-code-extract-methods) – Anderson Green Feb 21 '13 at 01:09

1 Answers1

2

You would need to run through the String and count open and closing brackets beginning at the starting position and match them up. The problem will be, there are several cases in which you need to ignore them (here are just a couple of examples):

  • in a line comment (between a // and a "new line")
  • in a block comment (between /* (not hidden by a line comment) and */)
  • in String literals (between two matching "'s (ignoring escapes (\", but watch for \\" or \\\"!)))
  • in char literals (between two matching ''s)
  • possibly more

You can either keep counters of all these as you run through the String, or you could come up with a Regular Expression that will match the entire function body (including all these cases in the list) and then apply it once and see where your matching index ends up after it found the body.

Either one should be a bit involved to do right in all cases, so I recommend hitting up Google to see if you can find some examples of pre-made regular expressions, for example.

Markus A.
  • 12,349
  • 8
  • 52
  • 116
  • I wonder if there are any existing Java parsers that could make this task easier. I'm almost certain that there are - many Java IDEs have implemented this feature before. :/ – Anderson Green Feb 21 '13 at 00:52
  • @AndersonGreen From the link in the comments above Antlr seems to be able to do this. I'm not sure if performance will be a concern for you, but the fastest solution would probably be either a regular expression specifically for this case (rather than a full-blown grammar-parser) or your own counting method. Any general Java parser will likely add a huge amount of unneeded overhead. – Markus A. Feb 21 '13 at 00:54
  • The counting method would probably not even be too difficult to get started with. Just build a first version and keep expanding its logic as you come across more and more exceptions (like escaped `"`'s inside Strings that you need to ignore (`"...\"..."`) when looking for the String-end). Not sure, though, if you want to just use it for yourself and this debugging approach is ok, or if it needs to be production ready. – Markus A. Feb 21 '13 at 00:57
  • I think that counting is better, since OP does not need to build a complete grammar tree. – Javier Feb 21 '13 at 01:15
  • 1
    You cannot solve this by applying a single big regular expression. – Oliver Charlesworth Feb 21 '13 at 08:50
  • @OliCharlesworth For a completely general case, you are probably right... Matching nested curly braces would be difficult, I guess. But skipping over the comments, string literals, etc, should be doable, no? So, you should be able to come up with a RegEx that can match any function body up to a given nesting level for the curly braces, no? It'd be a huge mess, but it should work. – Markus A. Feb 21 '13 at 16:29
  • You can use regexes to recognize the atoms of Java language, e.g., lexemes. From there, its pretty easy to write a loop that counts up when it sees a '{' and down when it sees a '}', and that pretty much solves your problem. So, you can't do with with just a regex, but you can do it reasonably simply with a set of them, a loop and a counter. – Ira Baxter Mar 20 '13 at 03:06