views:

151

answers:

3

I'd like to write a code beautifier and i thought of using Ruby to do it. Could someone show me a place to get started? I've seen a lot of code beautifiers online but I've never come across any tutorials on how to write one. Is this a very challenging task for someone who's never undertaken any projects such as writing a compiler, parser, etc. before?

(Is there another langauge which would be more well suited for this kind of task, excluding C/C++?)

Thank you.

+1  A: 

Python has an interesting feature - it exposes its own parser to scripts. There are examples that use the AST - abstract syntax tree - and do the pretty printing.

I'm not aware that Ruby exposes its own parser to its scripts in such a way, but there are parsers for Ruby written in Ruby here.

Will
curious the downvotes without comments
Will
+2  A: 

On this site : a code beautifier for ruby.

rubyBeautifier

germanlinux
+3  A: 

Well... I think the initial steps are what you'd do for any project.

Write a list of requirements. Describe a user interface to your program, that you like and won't prevent you meeting those requirements. Now you can write down more of a "code" design, and pick the language that would be easiest for you to meet that design.

Here's some requirements off the top of my head:

  • Supports code beautifying of these languages: Ruby, Python, Perl
  • Output code behaves identically to input
  • Output has consistent use of tabs/spaces
  • Output has consistent function naming convention
  • Output has consistent variable naming convention
  • Output has matching braces and indentation

Make as many as you want, it's your program. ;p I was kidding about the Perl, but I think every language you support is going to add a more work.

axus
+1. It's all about finding your requirements and then seeing which tool best meets those, just as for any problem. E.g. a requirement of "I'd like to learn Ruby" could be just as important as others and skew things in that direction. :)
Roger Pate
Could you tell me what tools I have to use? Do I need to use tools like lex and yacc or are those intended for something else? I did read somewhere that I need to parse the code into tokens or something after which I can handle it but that's all I know about. Thanks.
Mridang Agarwalla
Tokenizing lines will definitely be part of your program. Look at the rubyBeautifier germanlinux linked to, it's not a long program. It used regular expressions, which is perfect for this. They are hard to learn, but very useful.I don't know Ruby, but it looks like you can use .split(delimiter) function on a string to chop it into tokens. Regular expressions are probably the way to go, though.
axus