1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 /* The datafmt package implements syntax-directed, type-driven formatting
6 of arbitrary data structures. Formatting a data structure consists of
7 two phases: first, a parser reads a format specification and builds a
8 "compiled" format. Then, the format can be applied repeatedly to
9 arbitrary values. Applying a format to a value evaluates to a []byte
10 containing the formatted value bytes, or nil.
12 A format specification is a set of package declarations and format rules:
14 Format = [ Entry { ";" Entry } [ ";" ] ] .
15 Entry = PackageDecl | FormatRule .
17 (The syntax of a format specification is presented in the same EBNF
18 notation as used in the Go language specification. The syntax of white
19 space, comments, identifiers, and string literals is the same as in Go.)
21 A package declaration binds a package name (such as 'ast') to a
22 package import path (such as '"go/ast"'). Each package used (in
23 a type name, see below) must be declared once before use.
25 PackageDecl = PackageName ImportPath .
26 PackageName = identifier .
29 A format rule binds a rule name to a format expression. A rule name
30 may be a type name or one of the special names 'default' or '/'.
31 A type name may be the name of a predeclared type (for example, 'int',
32 'float32', etc.), the package-qualified name of a user-defined type
33 (for example, 'ast.MapType'), or an identifier indicating the structure
34 of unnamed composite types ('array', 'chan', 'func', 'interface', 'map',
35 or 'ptr'). Each rule must have a unique name; rules can be declared in
38 FormatRule = RuleName "=" Expression .
39 RuleName = TypeName | "default" | "/" .
40 TypeName = [ PackageName "." ] identifier .
42 To format a value, the value's type name is used to select the format rule
43 (there is an override mechanism, see below). The format expression of the
44 selected rule specifies how the value is formatted. Each format expression,
45 when applied to a value, evaluates to a byte sequence or nil.
47 In its most general form, a format expression is a list of alternatives,
48 each of which is a sequence of operands:
50 Expression = [ Sequence ] { "|" [ Sequence ] } .
51 Sequence = Operand { Operand } .
53 The formatted result produced by an expression is the result of the first
54 alternative sequence that evaluates to a non-nil result; if there is no
55 such alternative, the expression evaluates to nil. The result produced by
56 an operand sequence is the concatenation of the results of its operands.
57 If any operand in the sequence evaluates to nil, the entire sequence
60 There are five kinds of operands:
62 Operand = Literal | Field | Group | Option | Repetition .
64 Literals evaluate to themselves, with two substitutions. First,
65 %-formats expand in the manner of fmt.Printf, with the current value
66 passed as the parameter. Second, the current indentation (see below)
67 is inserted after every newline or form feed character.
71 This table shows string literals applied to the value 42 and the
72 corresponding formatted result:
79 A field operand is a field name optionally followed by an alternate
80 rule name. The field name may be an identifier or one of the special
83 Field = FieldName [ ":" RuleName ] .
84 FieldName = identifier | "@" | "*" .
86 If the field name is an identifier, the current value must be a struct,
87 and there must be a field with that name in the struct. The same lookup
88 rules apply as in the Go language (for instance, the name of an anonymous
89 field is the unqualified type name). The field name denotes the field
90 value in the struct. If the field is not found, formatting is aborted
91 and an error message is returned. (TODO consider changing the semantics
92 such that if a field is not found, it evaluates to nil).
94 The special name '@' denotes the current value.
96 The meaning of the special name '*' depends on the type of the current
99 array, slice types array, slice element (inside {} only, see below)
100 interfaces value stored in interface
101 pointers value pointed to by pointer
103 (Implementation restriction: channel, function and map types are not
104 supported due to missing reflection support).
106 Fields are evaluated as follows: If the field value is nil, or an array
107 or slice element does not exist, the result is nil (see below for details
108 on array/slice elements). If the value is not nil the field value is
109 formatted (recursively) using the rule corresponding to its type name,
110 or the alternate rule name, if given.
112 The following example shows a complete format specification for a
113 struct 'myPackage.Point'. Assume the package
115 package myPackage // in directory myDir/myPackage
121 Applying the format specification
123 myPackage "myDir/myPackage";
127 myPackage.Point = name "{" x ", " y:hexInt "}";
129 to the value myPackage.Point{"foo", 3, 15} results in
133 Finally, an operand may be a grouped, optional, or repeated expression.
134 A grouped expression ("group") groups a more complex expression (body)
135 so that it can be used in place of a single operand:
137 Group = "(" [ Indentation ">>" ] Body ")" .
138 Indentation = Expression .
141 A group body may be prefixed by an indentation expression followed by '>>'.
142 The indentation expression is applied to the current value like any other
143 expression and the result, if not nil, is appended to the current indentation
144 during the evaluation of the body (see also formatting state, below).
146 An optional expression ("option") is enclosed in '[]' brackets.
148 Option = "[" Body "]" .
150 An option evaluates to its body, except that if the body evaluates to nil,
151 the option expression evaluates to an empty []byte. Thus an option's purpose
152 is to protect the expression containing the option from a nil operand.
154 A repeated expression ("repetition") is enclosed in '{}' braces.
156 Repetition = "{" Body [ "/" Separator ] "}" .
157 Separator = Expression .
159 A repeated expression is evaluated as follows: The body is evaluated
160 repeatedly and its results are concatenated until the body evaluates
161 to nil. The result of the repetition is the (possibly empty) concatenation,
162 but it is never nil. An implicit index is supplied for the evaluation of
163 the body: that index is used to address elements of arrays or slices. If
164 the corresponding elements do not exist, the field denoting the element
165 evaluates to nil (which in turn may terminate the repetition).
167 The body of a repetition may be followed by a '/' and a "separator"
168 expression. If the separator is present, it is invoked between repetitions
171 The following example shows a complete format specification for formatting
172 a slice of unnamed type. Applying the specification
175 array = { * / ", " }; // array is the type name for an unnamed slice
177 to the value '[]int{2, 3, 5, 7}' results in
181 Default rule: If a format rule named 'default' is present, it is used for
182 formatting a value if no other rule was found. A common default rule is
186 to provide default formatting for basic types without having to specify
187 a specific rule for each basic type.
189 Global separator rule: If a format rule named '/' is present, it is
190 invoked with the current value between literals. If the separator
191 expression evaluates to nil, it is ignored.
193 For instance, a global separator rule may be used to punctuate a sequence
194 of values with commas. The rules:
199 will format an argument list by printing each one in its default format,
200 separated by a comma and a space.
215 // ----------------------------------------------------------------------------
216 // Format representation
218 // Custom formatters implement the Formatter function type.
219 // A formatter is invoked with the current formatting state, the
220 // value to format, and the rule name under which the formatter
221 // was installed (the same formatter function may be installed
222 // under different names). The formatter may access the current state
223 // to guide formatting and use State.Write to append to the state's
226 // A formatter must return a boolean value indicating if it evaluated
227 // to a non-nil value (true), or a nil value (false).
229 type Formatter func(state *State, value interface{}, ruleName string) bool
232 // A FormatterMap is a set of custom formatters.
233 // It maps a rule name to a formatter function.
235 type FormatterMap map[string]Formatter
238 // A parsed format expression is built from the following nodes.
243 alternatives []expr // x | y | z
245 sequence []expr // x y z
247 literal [][]byte // a list of string segments, possibly starting with '%'
250 fieldName string // including "@", "*"
251 ruleName string // "" if no rule name specified
255 indent, body expr // (indent >> body)
263 body, separator expr // {body / separator}
273 // A Format is the result of parsing a format specification.
274 // The format may be applied repeatedly to format values.
276 type Format map[string]expr
279 // ----------------------------------------------------------------------------
282 // An application-specific environment may be provided to Format.Apply;
283 // the environment is available inside custom formatters via State.Env().
284 // Environments must implement copying; the Copy method must return an
285 // complete copy of the receiver. This is necessary so that the formatter
286 // can save and restore an environment (in case of an absent expression).
288 // If the Environment doesn't change during formatting (this is under
289 // control of the custom formatters), the Copy function can simply return
290 // the receiver, and thus can be very light-weight.
292 type Environment interface {
297 // State represents the current formatting state.
298 // It is provided as argument to custom formatters.
301 fmt Format // format in use
302 env Environment // user-supplied environment
303 errors chan os.Error // not chan *Error (errors <- nil would be wrong!)
304 hasOutput bool // true after the first literal has been written
305 indent bytes.Buffer // current indentation
306 output bytes.Buffer // format output
307 linePos token.Position // position of line beginning (Column == 0)
308 default_ expr // possibly nil
309 separator expr // possibly nil
313 func newState(fmt Format, env Environment, errors chan os.Error) *State {
318 s.linePos = token.Position{Line: 1}
320 // if we have a default rule, cache it's expression for fast access
321 if x, found := fmt["default"]; found {
325 // if we have a global separator rule, cache it's expression for fast access
326 if x, found := fmt["/"]; found {
334 // Env returns the environment passed to Format.Apply.
335 func (s *State) Env() interface{} { return s.env }
338 // LinePos returns the position of the current line beginning
339 // in the state's output buffer. Line numbers start at 1.
341 func (s *State) LinePos() token.Position { return s.linePos }
344 // Pos returns the position of the next byte to be written to the
345 // output buffer. Line numbers start at 1.
347 func (s *State) Pos() token.Position {
348 offs := s.output.Len()
349 return token.Position{Line: s.linePos.Line, Column: offs - s.linePos.Offset, Offset: offs}
353 // Write writes data to the output buffer, inserting the indentation
354 // string after each newline or form feed character. It cannot return an error.
356 func (s *State) Write(data []byte) (int, os.Error) {
359 for i, ch := range data {
360 if ch == '\n' || ch == '\f' {
361 // write text segment and indentation
362 n1, _ := s.output.Write(data[i0 : i+1])
363 n2, _ := s.output.Write(s.indent.Bytes())
366 s.linePos.Offset = s.output.Len()
370 n3, _ := s.output.Write(data[i0:])
375 type checkpoint struct {
379 linePos token.Position
383 func (s *State) save() checkpoint {
384 saved := checkpoint{nil, s.hasOutput, s.output.Len(), s.linePos}
386 saved.env = s.env.Copy()
392 func (s *State) restore(m checkpoint) {
394 s.output.Truncate(m.outputLen)
398 func (s *State) error(msg string) {
399 s.errors <- os.NewError(msg)
404 // TODO At the moment, unnamed types are simply mapped to the default
405 // names below. For instance, all unnamed arrays are mapped to
406 // 'array' which is not really sufficient. Eventually one may want
407 // to be able to specify rules for say an unnamed slice of T.
410 func typename(typ reflect.Type) string {
412 case *reflect.ArrayType:
414 case *reflect.SliceType:
416 case *reflect.ChanType:
418 case *reflect.FuncType:
420 case *reflect.InterfaceType:
422 case *reflect.MapType:
424 case *reflect.PtrType:
430 func (s *State) getFormat(name string) expr {
431 if fexpr, found := s.fmt[name]; found {
435 if s.default_ != nil {
439 s.error(fmt.Sprintf("no format rule for type: '%s'", name))
444 // eval applies a format expression fexpr to a value. If the expression
445 // evaluates internally to a non-nil []byte, that slice is appended to
446 // the state's output buffer and eval returns true. Otherwise, eval
447 // returns false and the state remains unchanged.
449 func (s *State) eval(fexpr expr, value reflect.Value, index int) bool {
450 // an empty format expression always evaluates
451 // to a non-nil (but empty) []byte
456 switch t := fexpr.(type) {
458 // append the result of the first alternative that evaluates to
459 // a non-nil []byte to the state's output
461 for _, x := range t {
462 if s.eval(x, value, index) {
470 // append the result of all operands to the state's output
471 // unless a nil result is encountered
473 for _, x := range t {
474 if !s.eval(x, value, index) {
482 // write separator, if any
484 // not the first literal
485 if s.separator != nil {
486 sep := s.separator // save current separator
487 s.separator = nil // and disable it (avoid recursion)
489 if !s.eval(sep, value, index) {
492 s.separator = sep // enable it again
496 // write literal segments
497 for _, lit := range t {
498 if len(lit) > 1 && lit[0] == '%' {
499 // segment contains a %-format at the beginning
501 // "%%" is printed as a single "%"
504 // use s instead of s.output to get indentation right
505 fmt.Fprintf(s, string(lit), value.Interface())
508 // segment contains no %-formats
512 return true // a literal never evaluates to nil
515 // determine field value
518 // field value is current value
521 // indirection: operation is type-specific
522 switch v := value.(type) {
523 case *reflect.ArrayValue:
524 if v.Len() <= index {
527 value = v.Elem(index)
529 case *reflect.SliceValue:
530 if v.IsNil() || v.Len() <= index {
533 value = v.Elem(index)
535 case *reflect.MapValue:
536 s.error("reflection support for maps incomplete")
538 case *reflect.PtrValue:
544 case *reflect.InterfaceValue:
550 case *reflect.ChanValue:
551 s.error("reflection support for chans incomplete")
553 case *reflect.FuncValue:
554 s.error("reflection support for funcs incomplete")
557 s.error(fmt.Sprintf("error: * does not apply to `%s`", value.Type()))
561 // value is value of named field
562 var field reflect.Value
563 if sval, ok := value.(*reflect.StructValue); ok {
564 field = sval.FieldByName(t.fieldName)
566 // TODO consider just returning false in this case
567 s.error(fmt.Sprintf("error: no field `%s` in `%s`", t.fieldName, value.Type()))
574 ruleName := t.ruleName
576 // no alternate rule name, value type determines rule
577 ruleName = typename(value.Type())
579 fexpr = s.getFormat(ruleName)
582 if !s.eval(fexpr, value, index) {
589 // remember current indentation
590 indentLen := s.indent.Len()
592 // update current indentation
594 s.eval(t.indent, value, index)
595 // if the indentation evaluates to nil, the state's output buffer
596 // didn't change - either way it's ok to append the difference to
597 // the current identation
598 s.indent.Write(s.output.Bytes()[mark.outputLen:s.output.Len()])
604 if !s.eval(t.body, value, index) {
610 s.indent.Truncate(indentLen)
614 // evaluate the body and append the result to the state's output
615 // buffer unless the result is nil
617 if !s.eval(t.body, value, 0) { // TODO is 0 index correct?
620 return true // an option never evaluates to nil
623 // evaluate the body and append the result to the state's output
624 // buffer until a result is nil
627 // write separator, if any
628 if i > 0 && t.separator != nil {
629 // nil result from separator is ignored
631 if !s.eval(t.separator, value, i) {
635 if !s.eval(t.body, value, i) {
640 return true // a repetition never evaluates to nil
643 // invoke the custom formatter to obtain the result
645 if !t.fun(s, value.Interface(), t.ruleName) {
657 // Eval formats each argument according to the format
658 // f and returns the resulting []byte and os.Error. If
659 // an error occurred, the []byte contains the partially
660 // formatted result. An environment env may be passed
661 // in which is available in custom formatters through
662 // the state parameter.
664 func (f Format) Eval(env Environment, args ...interface{}) ([]byte, os.Error) {
666 return nil, os.NewError("format is nil")
669 errors := make(chan os.Error)
670 s := newState(f, env, errors)
673 for _, v := range args {
674 fld := reflect.NewValue(v)
676 errors <- os.NewError("nil argument")
680 if !s.eval(s.getFormat(typename(fld.Type())), fld, 0) { // TODO is 0 index correct?
684 errors <- nil // no errors
688 return s.output.Bytes(), err
692 // ----------------------------------------------------------------------------
693 // Convenience functions
695 // Fprint formats each argument according to the format f
696 // and writes to w. The result is the total number of bytes
697 // written and an os.Error, if any.
699 func (f Format) Fprint(w io.Writer, env Environment, args ...interface{}) (int, os.Error) {
700 data, err := f.Eval(env, args...)
702 // TODO should we print partial result in case of error?
709 // Print formats each argument according to the format f
710 // and writes to standard output. The result is the total
711 // number of bytes written and an os.Error, if any.
713 func (f Format) Print(args ...interface{}) (int, os.Error) {
714 return f.Fprint(os.Stdout, nil, args...)
718 // Sprint formats each argument according to the format f
719 // and returns the resulting string. If an error occurs
720 // during formatting, the result string contains the
721 // partially formatted result followed by an error message.
723 func (f Format) Sprint(args ...interface{}) string {
725 _, err := f.Fprint(&buf, nil, args...)
727 var i interface{} = args
728 fmt.Fprintf(&buf, "--- Sprint(%s) failed: %v", fmt.Sprint(i), err)