Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a code beautifier [closed]

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++?)

like image 746
Mridang Agarwalla Avatar asked Oct 11 '10 06:10

Mridang Agarwalla


People also ask

What does a code beautifier do?

A program that improves the presentation of programming source code. Based on an analysis of the syntax, it indents lines appropriately and squashes extraneous blank spaces and lines in order to produce more readable code.

Why do we format code?

Code formatting provides you with many opportunities to subtly communicate your intent to a reader. Far from being a backwater best left to draconian "style guides", code formatting is often your reader's first encounter with your system. It deserves attention and care.


2 Answers

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.

like image 187
Will Avatar answered Oct 17 '22 02:10

Will


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.

like image 37
axus Avatar answered Oct 17 '22 00:10

axus