parse for a function definition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Elizabeth H
    New Member
    • Jan 2011
    • 19

    parse for a function definition

    I need to search for a particular function in a shell script and replace it with the modified function for which I can use sed inplace editor. But I am not able to correctly parse for the function. Can I have a regular expression to search for the function which is of the form
    function()
    {
    no other braces in between
    }
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    If there's no braces that makes it a lot easier.
    Try function[^{]*{[^}]*}

    Comment

    • Elizabeth H
      New Member
      • Jan 2011
      • 19

      #3
      I am sorry but it does have some braces inside the function.. so I dont know how to parse until the end of function

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Try what @Rabbit put in above? You search from the beginning of the function with the opening bracket until the closing bracket. Did you try what he posted? Did it work?

        Comment

        • miller
          Recognized Expert Top Contributor
          • Oct 2006
          • 1086

          #5
          In perldoc perlre, search for the heading (?PARNO).

          This may be too advanced for you, but the below code parses out the bar function from the DATA block.

          Code:
          my $data = do {local $/; <DATA>};
          
          my $brackets_re = qr{
          	(
          	\{
          	(?:
          		(?> [^{}]+ )
          	|
          		(?-1)
          	)*
          	\}
          	)
          }sx;
          
          $data =~ s/(bar\(\) $brackets_re)//;
          
          print $data;
          
          1;
          
          __DATA__
          foo() {
          	# Fake stuff
          }
          
          bar() {
          	if (1) {
          	
          	}
          	
          	for () {
          		if (0) {
          			if (1) {
          			
          			}
          		}
          	}
          }
          
          baz() {
          	# Fake stuff
          }

          Comment

          • rovf
            New Member
            • May 2010
            • 41

            #6
            But in this particular case, the opening brace and the closing brace for the function body always are in the beginning of the line, aren't they?

            Comment

            • Elizabeth H
              New Member
              • Jan 2011
              • 19

              #7
              I tried what @rabbit mentioned , but didnt work. Guess it was coz I said the function doesnt have any braces inside it.

              Comment

              • rovf
                New Member
                • May 2010
                • 41

                #8
                This doesn't really answer my question.... Because when the function braces are only in column 1, and the other braces are not in column 1, you can use this to distinguish them, by just searching for braces at the beginning of a line.

                Comment

                • Elizabeth H
                  New Member
                  • Jan 2011
                  • 19

                  #9
                  Will you be able to still help me with the above question. I am still not able to parse the function correctly

                  Comment

                  • rovf
                    New Member
                    • May 2010
                    • 41

                    #10
                    ???

                    What exactly was unclear with my last response from March 21?

                    Comment

                    • Elizabeth H
                      New Member
                      • Jan 2011
                      • 19

                      #11
                      Thanks for the quick response. Like you had mentioned above, function braces are only in column 1, and the other braces are not in column 1 but not sure how to do the search I need to insert a new statement just before the closing curly brace of the function. Is function parsing the best way to do this or do I have any other option?

                      Comment

                      • rovf
                        New Member
                        • May 2010
                        • 41

                        #12
                        If you sure that *all* your function's closing curlies are in column 1, and no other curly brace is in column 1, and you want to insert something just before the function's closing curly brace, things are trivial. You just read your file line by line, and whenever you encounter the closing of a function, you insert your new statement.

                        However this solution is so obvious, that I there must be some hidden problem I don't understand yet. Did I overlook something?

                        Comment

                        • miller
                          Recognized Expert Top Contributor
                          • Oct 2006
                          • 1086

                          #13
                          I already showed you how to do this, Elizabeth. Here is a more complete example demonstrating how to use what I showed you to more specifically add statements to the end of a specific function:

                          Code:
                          use strict;
                          use warnings;
                          
                          my $data = do {local $/; <DATA>};
                          
                          $data =~ s~(bar\(\)\s*({(?:(?>[^{}]+)|(?-1))*}))~
                          	my $func = $1;
                          
                          	# Remove last }, add it back later
                          	chop $func;
                          
                          	# Add new statements to end, including trailing }
                          	$func .= "
                          	# This is just a trailing statement
                          }";
                          
                          	$func
                          ~ex;
                          
                          print $data;
                          
                          1;
                          
                          __DATA__
                          foo() {
                          	# Fake stuff
                          }
                          
                          bar() {
                          	if (1) {
                          	
                          	}
                          	
                          	for () {
                          		if (0) {
                          			if (1) {
                          			
                          			}
                          		}
                          	}
                          }
                          
                          baz() {
                          	# Fake stuff
                          }
                          - Miller

                          Comment

                          • Elizabeth H
                            New Member
                            • Jan 2011
                            • 19

                            #14
                            Thnks alot..it wrks wellll....I dnt speak Perl much..so not much sure on how to modify.. can I have it in way where I can use this .pl file to call different files..and apply the same changes to all files(ie, parse out the specific fn in all files and add a trailing statement to all)? Again thnks for ya help!

                            Comment

                            • Elizabeth H
                              New Member
                              • Jan 2011
                              • 19

                              #15
                              @ROVF: I was told that perl might be a good way to do this...Since i dnt speak Perl much, it takes me more time to implement even the basic things, but you had got it right...I guess I was thinking it a bit too complex...Thnks again for ya quick responses and ya help

                              Comment

                              Working...