Tricky SED matching and replace pattern for reformatting source codes brackets

Tricky SED matching and replace pattern for reformatting source codes brackets

Post by Matt Bens » Sat, 14 Aug 2004 00:28:22



Ok, I know, the recommended bracket style for source codes (e.g. in java) is like:

   if (.....)
   {
      command statements
   }

But all the curly brackets at the beginning is for me rather bugging and makes me nervous.
I prefer a style like

   if (.....) {
      command statements }

where different nesting levels are visualized by different indention.
I don't want to discuss this style at this place.

However I need a good sed search and replace pattern which converts a source code file with old
curly bracketing into my personal style.

This should be theoretically rather easy. I only need a pattern which replaces

   blank-blank-curlybracket

   or

   controlreturn-blank-curlybracket

   or

   controlreturn-curlybracket

   into

   blank-curlybracket

iteratively as often as necessary for the whole file.
Notice that before a closing curly bracket there should be exactly one blank.

I feel that this is somehow feasible with sed but I cannot find the magic pattern.

Can someone help me?

Matt

 
 
 

Tricky SED matching and replace pattern for reformatting source codes brackets

Post by Dan Espe » Sat, 14 Aug 2004 01:13:17



> Ok, I know, the recommended bracket style for source codes (e.g. in java) is like:

>    if (.....)
>    {
>       command statements
>    }

> But all the curly brackets at the beginning is for me rather bugging and makes me nervous.
> I prefer a style like

>    if (.....) {
>       command statements }

I don't think you are going to find sed up to the task.
Handling newlines is tricky.

GNU indent with the -br flag will do the job.

 
 
 

Tricky SED matching and replace pattern for reformatting source codes brackets

Post by Buck Turgidso » Sat, 14 Aug 2004 03:21:20


Quote:

> This should be theoretically rather easy. I only need a pattern which
replaces

>    blank-blank-curlybracket

>    or

>    controlreturn-blank-curlybracket

>    or

>    controlreturn-curlybracket

>    into

>    blank-curlybracket

> iteratively as often as necessary for the whole file.
> Notice that before a closing curly bracket there should be exactly one
blank.

Do you want to transform all closing braces, or just the one that closes the
method?  Would your regex pick the correct closing curly brace from the
following?  If it is the latter.  You may need to "slurp" the whole file
into a buffer using perl (see "undef $/") and start counting and matching
opening and closing brackets.  If it is the former, you might try using vi.

    private static void processDirList(String dirName) {
        File dirObj = new File (dirName);
        if ( dirObj.exists() == true ) {
            if ( dirObj.isDirectory() == true ) {
                File [] fileList = dirObj.listFiles();
                for ( int i = 0; i < fileList.length; i++ ) {
                    if ( fileList[i].isDirectory() ) {
                        processDirList (fileList[i].getAbsolutePath());
                    } else if ( fileList[i].isFile() ) {
                        try {
                            processFile(fileList[i].getAbsolutePath());
                        } catch ( Exception e ) {
                            System.out.println ("processDirList: " + e);
                        }
                    }
                }
            }
        }
    }

 
 
 

Tricky SED matching and replace pattern for reformatting source codes brackets

Post by Walter Brisco » Sat, 14 Aug 2004 07:23:20




writes
Why do you consider you are topical in 5 newsgroups? The thread has
disintegrated as correspondents have selected different subsets of the
original groups. I stick to your original set.

Quote:

>> Ok, I know, the recommended bracket style for source codes (e.g. in
>>java) is like:

>>    if (.....)
>>    {
>>       command statements
>>    }

>> But all the curly brackets at the beginning is for me rather bugging
>>and makes me nervous.
>> I prefer a style like

>>    if (.....) {
>>       command statements }

>I don't think you are going to find sed up to the task.

I disagree with that advice but doubt the OP will want to spend enough
time to develop a solution. I threw the following together to transform
the supplied data:- "N;s/) *\n *{/) {/;s/ *\n *}/ }/"

Quote:>Handling newlines is tricky.

>GNU indent with the -br flag will do the job.

I agree that ANY tool beats rolling your own. If a tool does most of the
work, do the rest with sed. ;)
--
Walter Briscoe
 
 
 

Tricky SED matching and replace pattern for reformatting source codes brackets

Post by Ed Morto » Sat, 14 Aug 2004 22:25:33



> Ok, I know, the recommended bracket style for source codes (e.g. in java) is like:

>    if (.....)
>    {
>       command statements
>    }

> But all the curly brackets at the beginning is for me rather bugging and makes me nervous.
> I prefer a style like

>    if (.....) {
>       command statements }

> where different nesting levels are visualized by different indention.
> I don't want to discuss this style at this place.

If this language su[pports blocks (as C does) what would you want to do
with this:

        commands1
        {
            commands2
        }
        commands3

I assume you wouldn't want it to become:

        commands1 {
           commands2 }
        commands3

as that would be very misleading.

If the language supports commenst (also as C does), what would you do with:

        if (...)
        /*
        {
        */
        commands

I assume you wouldn't wan't it to become:

        if (...)
        /* {
            */
             commands

with no end to the indentation.

What would you do with existing code like:

        if(...) { commands }

I assume you'd want it to become:

        if(...) {
            commands }

but I don't believe that fits the patterns you specify below

What would you do with:

        if (condition){
             commands
        /*(condition)*/}

which also doesn't fit your patterns.

- Show quoted text -

Quote:> However I need a good sed search and replace pattern which converts a source code file with old
> curly bracketing into my personal style.

> This should be theoretically rather easy. I only need a pattern which replaces

>    blank-blank-curlybracket

>    or

>    controlreturn-blank-curlybracket

>    or

>    controlreturn-curlybracket

>    into

>    blank-curlybracket

> iteratively as often as necessary for the whole file.
> Notice that before a closing curly bracket there should be exactly one blank.

> I feel that this is somehow feasible with sed but I cannot find the magic pattern.

I doubt it. The list I gave above is just a few cases off the top of my
head. You really need a tool that understands the language (i.e. a
parser) to pull of this kind of thing. Get yourself a beautifier tool
(e.g,. "cb" is one for C). If (as I suspect) no tool supports the
somewhat dubious format you want, just accept it and move on....

        Ed.

- Show quoted text -

Quote:> Can someone help me?

> Matt

 
 
 

Tricky SED matching and replace pattern for reformatting source codes brackets

Post by Tris Orendorf » Sun, 15 Aug 2004 02:25:36




Quote:> Ok, I know, the recommended bracket style for source codes (e.g. in
> java) is like:

For Java formatting I recommend Jalopy, available at http://sourceforge.net/projects/jalopy/, to format
code to your heart's content.  It works in all the top IDEs, ant and the command line.

--
Sincerely,

Tris Orendorff

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d++ s+:- a+ C+ UL++++ P+ L+ E- W+ N++ o- K++ w+ O+ M !V PS+ PE Y+ PGP t+ !5 X- R- tv--- b++
DI++ D+ G++ e++ h---- r+++ y+++
------END GEEK CODE BLOCK------

 
 
 

1. How to replace a whole line (matching a pattern) in a text filke with SED ?

Assume I have a text file blah.dat. In that file there can be a line (or multiple lines)
among other lines containing the text fragment pattern "version" (without quotes).
Other characters can appear in these lines as well.

Now I want to replace ALL lines in this file with the text line "vers. release"
(without quotes). All other lines should remain untouched.
Order of lines should remain as well.

Example blah.dat:

kikik ikik ik kik kik  
hello version endofline
rtrtrtrtrtrtrt trtrtr

afterwards:

kikik ikik ik kik kik  
vers. release
rtrtrtrtrtrtrt trtrtr

I guess that this can be done most comfortable with the sed.
How do I do that ?

Thx
Matt

2. Installing VMWare workstation on Linux

3. Pattern matching and extracting the data which matches the pattern

4. Help needed to setup printer (Please)

5. Sed question: Replacing numbers between brackets...

6. matrox m3D

7. vi replace second matching pattern in every line - Help Please sorry for double posting

8. Linux fail to recognize SONY CDU76E CDROM and MS Bus Mouse,Help!

9. sed search replace with use of array, tricky ?!

10. sed pattern matching

11. sed: output matched pattern?

12. print out just the pattern matched using sed (performance)

13. Sed pattern matching