From owner-freebsd-questions@FreeBSD.ORG Fri Aug 25 14:09:51 2006 Return-Path: X-Original-To: questions@freebsd.org Delivered-To: freebsd-questions@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id E89D416A4DF for ; Fri, 25 Aug 2006 14:09:51 +0000 (UTC) (envelope-from kyrreny@broadpark.no) Received: from osl1smout1.broadpark.no (osl1smout1.broadpark.no [80.202.4.58]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8A6E643D80 for ; Fri, 25 Aug 2006 14:09:41 +0000 (GMT) (envelope-from kyrreny@broadpark.no) Received: from osl1sminn1.broadpark.no ([80.202.4.59]) by osl1smout1.broadpark.no (Sun Java System Messaging Server 6.1 HotFix 0.05 (built Oct 21 2004)) with ESMTP id <0J4K00GCC4O3TF20@osl1smout1.broadpark.no> for questions@freebsd.org; Fri, 25 Aug 2006 16:09:39 +0200 (CEST) Received: from suria.broadpark.no ([80.203.212.30]) by osl1sminn1.broadpark.no (Sun Java System Messaging Server 6.1 HotFix 0.05 (built Oct 21 2004)) with ESMTP id <0J4K001294O38BM0@osl1sminn1.broadpark.no> for questions@freebsd.org; Fri, 25 Aug 2006 16:09:39 +0200 (CEST) Date: Fri, 25 Aug 2006 16:09:49 +0200 From: Kyrre =?iso-8859-1?Q?Nyg=E5rd?= In-reply-to: <1b15366e0608250531q6187d598h78b02e14ab4b5ac2@mail.gmail.co m> To: "Matti J. Karki" , "W. D." Message-id: <7.0.1.0.2.20060825160431.023a1650@broadpark.no> MIME-version: 1.0 X-Mailer: QUALCOMM Windows Eudora Version 7.0.1.0 Content-type: text/plain; charset=us-ascii; format=flowed Content-transfer-encoding: 7BIT References: <7.0.1.0.2.20060824145822.0194fc10@broadpark.no> <1b15366e0608240618j62d41ad3j537f095b2e566ed5@mail.gmail.com> <7.0.1.0.2.20060824192439.02386de8@broadpark.no> <5.1.0.14.2.20060825064053.01eebec0@209.152.117.178> <1b15366e0608250531q6187d598h78b02e14ab4b5ac2@mail.gmail.com> Cc: questions@freebsd.org Subject: Re: Code beautifiers, anyone? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 25 Aug 2006 14:09:52 -0000 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