Hello rust-dev! The initial framework for the new fmt! syntax extension landed
recently, and I think it's time to start bikeshedding formatting syntax. To
recap what the state of the world is now:

* The goal of this new system is to support internationalization functions as
  well as everyday use cases.
* The placeholder for an argument is now `{}` instead of `%`
* There are now the possibility of named arguments. These names are all just
  rust identifiers to identify an argumnt.

The support which has landed consists of a runtime, the compile-time support to
back it, and the syntax extension to generate all the code. You can actually use
it today with the `ifmt!` macro, although nothing about formatting will work
(except for selecting which formatter you'd like). To get a flavor of what the
new syntax looks like (and the current interim formatting syntax), take a look
at: 
https://github.com/mozilla/rust/blob/ffb670ffcd69ed8e7cd13a7f06375ede752349e2/src/test/run-pass/ifmt.rs

My current idea for the syntax is something along the lines of:

    { [argument and format args] [, method information] }

Where everything in [] is optional. From this, the `{}`-style placeholders are
pretty much here to stay at this point (they support nesting), and I don't think
that there's much to discuss in the "method information" section of things
(although I could be wrong!). What I'd mainly like to bikeshed is the
[argument and format args] section. The purpose of this section is to identify
which argument should fill this placeholder and how it should be formatted.
Implementation-wise, this will invoke the corresponding formatting trait's
method on one of the arguments with a "Formatter" struct argument which contains
information about the requested format. It would then be up to each
implementation to respect the formatting arguments.

So without further ado, here's some options that should be considered:

1. The current fmt! style syntax:

    As described in extfmt.rs, the syntax for these modifiers are:

        Format := '%' Parameter? Flag* Width? Precision? Type
        Parameter := [0-9]+ '$'
        Flag := [ 0#+-]
        Width := Parameter | [0-9]+
        Precision := '.' [0-9]+
        Type := [bcdfiostuxX?]

    This gets you things like:

        %x
        %08x
        %.*s
        %10s
        %-10s
        %.10s

    So to extrapolate this to ifmt, I would basically replace this to be:

        Format := Parameter? (':' Flag* Width? Precision? Type?)?
        Parameter := [0-9]+ | Identifier

    Which would yield formattings of the style:

        {:x}        // hex number
        {0:08x}     // format the 0th argument as hex
        {foo:.*s}   // format the argument named 'foo' as a string
        {:-10s}     // format the next argument as a left-aligned string

2. Python-like modifiers
   -- http://docs.python.org/3/library/string.html#formatstrings

    This would have a syntax like:

      format_spec :=
[argument][':'[[fill]align][sign]['#'][width][.precision][type]]
      fill := character
      align := '<' | '>'
      sign := '+' | '-'
      width := count
      precision := count | '*'
      type := identifier | ''
      count := parameter | integer
      parameter := integer '$'

    This is similar to the above syntax except that it's a bit clearer what the
    alignment is. Some things can look slightly odd though

        {: <+#10.10d}   // means something, but who knows what?

    Also note that in this syntax everything is optional, if you just provide
    something like `{}` it's the same as `%?` today

Those are my ideas for now, and I was wondering what others thought about the
syntax. Some important things which I think need to be expressable:

1. The width of the thing being formatted
2. Precision for floats along with digit widths for integers
3. Specific fill characters would be nice
4. "%.*s" is actually a pretty useful modifier in C, I'd love to keep it in some
   way shape or form
5. It should be fairly terse to format something the default way.

What do others think? I certainly don't want to leave out important use cases
that I forgot!
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to