On 8/25/06, Kyrre Nygård <[EMAIL PROTECTED]> wrote:
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
Looks OK, except...
inbuffer = re.sub('\n +', '\n', inbuffer) # Strip trailing whitespace
This will strip spaces at the _beginning_ of line (leading spaces).
inbuffer = re.sub('\t+', '', inbuffer) # Strip trailing tabs
And this will strip _all_ tabs. This is important to remember. More of that later...
And also, I noticed you put <'\n +', '\n', inbuffer> twice, is one enough like in the above example?
Yes and no. I have used to do this operation in two steps. First, before anything, I'll strip all leading spaces from every line of code. Then I'll apply all other rules. And after everything else in place, I'll strip all leading spaces _again_, because some of my rules will produce spaces that will mess up the indentation process later.
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.
Well, it's good to have ambitious goals... A word of warning. Above, I mentioned that it's important to remember that my example will remove _all_ tabulator characters from text. This means that - for example - all lines with indentation inside the code comments will be messed up (remember, usually a tab character equals 8 spaces and I'm removing tabs all together). This leads me to my point: my code does not handle multi-line comments at all. They may look messed-up. A single curly bracket inside of a comment will throw the indentation code out of sync. Also the code does not address line breaks "\", so for example the macro definitions will not be indented correctly. So, it will take some additional work to be able to run the script without any side effects. The script was meant to be run on a source code with poor style and with no (or very few) comments. -Matti _______________________________________________ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"