xurtis / atoms

S expression parser for rust
http://crates.io/crates/atoms/
MIT License
3 stars 0 forks source link

Pretty Printer #3

Open xurtis opened 7 years ago

xurtis commented 7 years ago

Currently, all output is through display and printed on a single line. It'd be good to have a pretty printer that nicely formats the output using the following rules:

Condensed format with tight bracing

( (blah blah blah)
  ( blah
    (blah blah blah)
  )
  blah
)

Sparse formatting with loose bracing

( 
  ( blah blah blah )
  ( 
    blah
    ( blah blah blah )
  )
  blah
)
jashank commented 7 years ago

There is much prior art in s-expression pretty printing, though most of it focuses specifically on rules for Lisp-like languages. If that's your target here, you probably want to implement (something like) the X3J13 pretty-printing style or mimic the Guile or Racket pretty printers.

In summary, the former, tightly-braced style makes a good deal more sense as a starting point, but adding behaviours depending on symbols would be useful. Symbols should also always be tightly-braced before and after, as dangling parens or loose parens tend to look bad in the context of Lisps, so:

((blah blah blah)
  (blah
    (blah blah blah))
  blah)
xurtis commented 7 years ago

Let me propose a few rules.

Classifying Values

First, we classify every value as either code or data in an implicit manner.

Secondly, we classify the depth of each list as such:

The hard part after this is using this information to know when to indent an break lines...

fwouts commented 3 years ago

Hi there 👋 I'm a few years late to the party, but was wondering if you intended to merge that pretty-print branch? I've been looking for a Rust sexp pretty-printer and this looks like exactly what I need :)