Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have code fragments in a text file and have to find out what features (e.g is there a getter/mutator in a line) are associated with a specified line in a code fragment. For example, in a given code fragment.

C#
1 public HTMLEditor() {
2  //install the source configuration
3  setSourceViewerConfiguration(new HTMLConfiguration());
4  //install the document provider
5  setDocumentProvider(new HTMLDocumentProvider()); }
6 protected void createActions() {
7  super.createActions();
8 //... add other editor actions here
9 }}


Line 2,4 and 8 contains a comment and i can check it simply by writing following source code. Lets say

Java
public boolean containsComment(String line) {
    // if line contains comment
    if(line.contains("//")){
        return true;            
    }else{
        return false;
    }
}


A line can contain more than one feature e.g line 5 in above snippet has a set method and new keyword too. Also, words such as new or get or etc should only be checked in source code and not in comments part. Same like this there are many such cases; Same as my containsComment which is easier to check other features such as exception checking, new or other keywords checking are little trickier and needs some complex code to handle.

I have solved most of the features in this way except these three...I am explaining each with an example and need an immediate and urgent help.

Contains an anonymous class
Java
ObjectInterestedInFooObjects() {
   result = someLookup.lookupResult(Foo.class);
    result.addLookupListener(this);
     resultChanged(null);
 }


No of parameters in a mathod
Java
putValue(Action.Name,NbBundle.getMessage(FooAction.class, "LBL_Action"));


Retention or reflection in annotation

Java
@ActionID(
         category = "Build",
        id = "com.foo.bar.OnJavaPackageAction")


I am looking forward to immediate help for these. I think some regex might help me but i am not sure. Looking forward to your help.
Posted
Comments
Sergey Alexandrovich Kryukov 26-May-14 10:25am    
Help with what? This is not even a question. This is a very naive attempt to make a text scanner/parser for an interpreter or a compiler. Perhaps you need to learn existing courses on parsers... Your attempt is not a bad thing, it will help your to understand the subject better when you read about the existing approaches...
—SA
NajamN 26-May-14 11:37am    
I am just trying to extract some features from a given code fragment. I would appreciate if someone will help me in making a regex for given scenarios. It is neither a course not a subject and not even an assignment.
Sergey Alexandrovich Kryukov 26-May-14 12:05pm    
I mentioned the course not because I thought that you are using some course of have some assignment, but because I advise you to read such course matter. I don't advise you to take your prospective regex approach seriously. This is not how a really working text scanner/parser can be created and work in a reliable and supportable manner. Really, read about existing scanner designs and principles.
—SA
Richard MacCutchan 26-May-14 12:35pm    
It's most unlikely that you can use a regex to parse source code, as there are too many different possibilities. You als hve the problem that a single source statement can be split over multiple lines, may have embedded comments ... etc. Try Google for "text parser" to get some samples and articles that may help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900