Code beautifiers, anyone?

Kyrre Nygård kyrreny at broadpark.no
Fri Aug 25 14:09:52 UTC 2006


At 14:31 25.08.2006, Matti J. Karki wrote:

>No doubt :)
>
>There's no comments in the code, because usually my scripts are
>disposable. As I said, I do those case by case.
>
>The code should be quite clear for most parts, but the bunch of
>regexps at the beginning of the code do the following things:
>
>inbuffer = re.sub('\n +', '\n', inbuffer) # This strips all spaces
>from the beginning of every line of code.
>
>inbuffer = re.sub('\t+', '', inbuffer) # This does the same for tab 
>characters.
>
>inbuffer = re.sub('\) *?\n\{', ') {', inbuffer) # This moves all curly
>braces where I want them to be, i.e. at the end of the line.
>
>inbuffer = re.sub('\) *?{', ') {', inbuffer) # This removes all extra
>spaces between the closing bracket ) and the opening curly bracket {.
>
>inbuffer = re.sub('else *?\n{', 'else {\n', inbuffer) # This fixes
>curly brackets in the else clauses.
>
>inbuffer = re.sub('{ *?(.+?\n)', '{\n\g<1>', inbuffer) # Sometimes
>there is code where curly brackets are all in the same line and the
>contents of the brackets are between the brackets. This moves the
>contents to new line.
>
>inbuffer = re.sub('(\n.+?)}', '\g<1>\n}', inbuffer) # This takes care
>of the closing bracket at the above case.
>
>inbuffer = re.sub('\n +', '\n', inbuffer) # This just cleans up all
>spaces that may appear when arranging the code.
>
>The rest of the script is just a simple indenting machine, which
>indents the code using four spaces as a single level of indentation.
>The indentation will be placed after every newline character and
>indentation will be increased and decreased based on the occurrences
>of curly brackets.
>
>So, basically (and now I'm just trying to remember from top of my
>head, it's been some time, I dealt with this particular source code)
>the script will do the following steps with the code:
>
>step 1) the original piece of code
>
>int main()
>{
>    char *c = {'a', 'b', 'c'};
>
>    print("hello, world");
>    if (true)
>    {
>          printf("ok");
>    }
>    else
>    {
>        printf("umm...");
>    }
>}
>
>step 2) stripping all indentation
>
>int main()
>{
>char *c = {'a', 'b', 'c'};
>
>print("hello, world");
>if (true)
>{
>printf("ok");
>}
>else
>{
>printf("umm...");
>}
>}
>
>step 3) applying the rest of the regexp rules and indenting with the for loop
>
>int main() {
>    char *c = {
>        'a', 'b', 'c'
>    };
>
>    print("hello, world");
>    if (true) {
>        printf("ok");
>    }
>    else {
>        printf("umm...");
>    }
>}
>
>Hope this clears my script a little bit.
>
>
>        -Matti

Very amazing man, I'm impressed by your enthusiasm for correctness.

In your script, do these comments look alright then?

(I simplified them a bit)

inbuffer = re.sub('\) *?\n\{', ') {', inbuffer) # Move curly brackets 
to the end of lines
inbuffer = re.sub('\) *?{', ') {', inbuffer) # Remove spaces between 
closing brackets and opening curly brackets
inbuffer = re.sub('else *?\n{', 'else {\n', inbuffer) # Fix curly 
brackets in `else' clauses
inbuffer = re.sub('{ *?(.+?\n)', '{\n\g<1>', inbuffer) # Break up the 
content of curly brackets
inbuffer = re.sub('(\n.+?)}', '\g<1>\n}', inbuffer) # Take care of 
closing brackets from the above rule
inbuffer = re.sub('\n +', '\n', inbuffer) # Strip trailing whitespace
inbuffer = re.sub('\t+', '', inbuffer) # Strip trailing tabs

And also, I noticed you put <'\n +', '\n', inbuffer> twice,
is one enough like in the above example?

Thank you man, your script has taught me a lot already about Python.

I brought this case up at the OpenBSD mailinglist as well
and they pointed me to http://en.wikipedia.org/wiki/Kernel_Normal_Form

So now I have two more steps in my search for the perfect tools to 
beautify all my code:

01 Design a normal form for all my languages
02 Convert your script to Ruby

After that, I can't wait to run it over the FreeBSD codebase and watch
the added value it gets. Then I can start selling the script to governments.
Just kidding :) But it would be nice to reverse engineer all those commercial
code parsers that hunt for bugs and create my own that I'll eventually
hook up with some artificial intelligence.

All the best,
Kyrre



More information about the freebsd-questions mailing list