0

I want to parse and get the contents within the braces, which actually is as follows.

-(void)testfunction
{
   //do something
   integer myVal = 0;
   NSString *testString = @"Test";

   myVal = [someObject test];
   if(myVal >10)
   {
       NSLog(@"Test Succeed");
   }
   else
   {
       NSLog(@"Test Succeed");
       if(testString)
       {
            NSLog("Still");
       }
    }  
}

I am expecting the output as all the text in the testFunction.

Is there a simpler logic to parse?

I am using NSString and developing for MAC OS and not iPhone.

Regards, Nirav

4
  • I suggest you try to search for compiler tutorials, and learn how parsing code works in a compiler. Commented Jul 17, 2012 at 8:44
  • I know about the compilers ! Was thinking if there is a simpler solution for Mac-OS or objective C Commented Jul 17, 2012 at 8:50
  • Should it be able to handle comments, strings etc that can include {}? then you probably need a proper parser somehow. If the functions always start with {} as first character in the line then you might get away with some regex/substring hack. What will it be used for? Commented Jul 17, 2012 at 10:02
  • Currently I am planning only for the functions without comments etc.. Commented Jul 17, 2012 at 10:12

2 Answers 2

1

What you have to do is build a program that recognizes the start of a function, and reads it until it reaches the closing brace, basically what a compiler does.

The simplest way is probably to use a regular expression to find the function header, and then read line by line (or char by char) counting the opening and closing braces. Remember that the code you read may not always be formatted well, and the closing brace can actually be in the middle of a line with code and/or comments before and after.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Joachim. I have almost implemented the same thing. I am counting the opening and closing braces. Thanks any ways.
1

The basic building block for parsing NSStrings is the NSScanner class.

More basic is plain C-style strtok().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.