2009-04-23 22:53:01 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
/* The format package implements syntax-directed formatting of arbitrary
|
|
|
|
data structures.
|
|
|
|
|
|
|
|
A format specification consists of a set of named productions in EBNF.
|
|
|
|
The production names correspond to the type names of the data structure
|
|
|
|
to be printed. The production expressions consist of literal values
|
|
|
|
(strings), references to fields, and alternative, grouped, optional,
|
|
|
|
and repetitive sub-expressions.
|
|
|
|
|
2009-04-28 20:11:37 -06:00
|
|
|
When printing a value, its type name is used to look up the production
|
2009-04-25 17:36:17 -06:00
|
|
|
to be printed. Literal values are printed as is, field references are
|
2009-04-28 20:11:37 -06:00
|
|
|
resolved and the respective field values are printed instead (using their
|
|
|
|
type-specific productions), and alternative, grouped, optional, and
|
2009-04-25 17:36:17 -06:00
|
|
|
repetitive sub-expressions are printed depending on whether they contain
|
|
|
|
"empty" fields or not. A field is empty if its value is nil.
|
|
|
|
*/
|
2009-04-23 22:53:01 -06:00
|
|
|
package format
|
|
|
|
|
|
|
|
import (
|
2009-04-28 20:11:37 -06:00
|
|
|
"flag";
|
2009-04-23 22:53:01 -06:00
|
|
|
"fmt";
|
|
|
|
"go/scanner";
|
|
|
|
"go/token";
|
|
|
|
"io";
|
|
|
|
"os";
|
2009-04-25 17:36:17 -06:00
|
|
|
"reflect";
|
|
|
|
"strconv";
|
2009-05-06 17:28:18 -06:00
|
|
|
"strings";
|
2009-04-23 22:53:01 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
// TODO should probably do this in a different way
|
|
|
|
var (
|
|
|
|
debug = flag.Bool("d", false, "debug mode");
|
|
|
|
trace = flag.Bool("t", false, "trace mode");
|
|
|
|
)
|
2009-04-28 20:11:37 -06:00
|
|
|
|
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Format representation
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
type (
|
2009-05-08 12:22:57 -06:00
|
|
|
Formatter func(w io.Writer, value interface{}, name string) bool;
|
2009-05-01 20:32:41 -06:00
|
|
|
FormatterMap map[string]Formatter;
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
// A production expression is built from the following nodes.
|
|
|
|
//
|
|
|
|
type (
|
|
|
|
expr interface {
|
2009-04-25 17:36:17 -06:00
|
|
|
String() string;
|
2009-04-24 18:22:58 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
alternative struct {
|
|
|
|
x, y expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
sequence struct {
|
|
|
|
x, y expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
literal struct {
|
|
|
|
// TODO should there be other types or should it all be string literals?
|
|
|
|
value []byte;
|
|
|
|
};
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
indentation struct {
|
|
|
|
iexpr expr; // outdent if nil
|
|
|
|
};
|
|
|
|
|
|
|
|
field struct {
|
|
|
|
fname string; // including "^", "*"
|
|
|
|
tname string; // "" if no tname specified
|
|
|
|
};
|
|
|
|
|
|
|
|
negation struct {
|
|
|
|
neg expr;
|
|
|
|
};
|
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
option struct {
|
2009-05-01 20:32:41 -06:00
|
|
|
opt expr;
|
2009-04-24 18:22:58 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
repetition struct {
|
2009-05-01 20:32:41 -06:00
|
|
|
rep expr;
|
|
|
|
div expr;
|
2009-04-24 18:22:58 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
custom struct {
|
|
|
|
name string;
|
2009-05-01 20:32:41 -06:00
|
|
|
form Formatter
|
2009-04-24 18:22:58 -06:00
|
|
|
};
|
2009-04-23 22:53:01 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
func (x *alternative) String() string {
|
|
|
|
return fmt.Sprintf("(%v | %v)", x.x, x.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (x *sequence) String() string {
|
|
|
|
return fmt.Sprintf("%v %v", x.x, x.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
func (x *literal) String() string {
|
|
|
|
return strconv.Quote(string(x.value));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (x *indentation) String() string {
|
|
|
|
if x.iexpr != nil {
|
|
|
|
fmt.Sprintf(">> %s", x.iexpr);
|
|
|
|
}
|
|
|
|
return "<<";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
func (x *field) String() string {
|
2009-05-01 20:32:41 -06:00
|
|
|
if x.tname == "" {
|
|
|
|
return x.fname;
|
2009-04-25 17:36:17 -06:00
|
|
|
}
|
2009-05-01 20:32:41 -06:00
|
|
|
return x.fname + " : " + x.tname;
|
2009-04-25 17:36:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
func (x *negation) String() string {
|
|
|
|
return fmt.Sprintf("!%v", x.neg);
|
2009-04-25 17:36:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (x *option) String() string {
|
2009-05-01 20:32:41 -06:00
|
|
|
return fmt.Sprintf("[%v]", x.opt);
|
2009-04-25 17:36:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (x *repetition) String() string {
|
2009-05-01 20:32:41 -06:00
|
|
|
if x.div == nil {
|
|
|
|
return fmt.Sprintf("{%v}", x.rep);
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("{%v / %v}", x.rep, x.div);
|
2009-04-25 17:36:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (x *custom) String() string {
|
2009-05-01 20:32:41 -06:00
|
|
|
return "<" + x.name + ">";
|
2009-04-25 17:36:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* A Format is a set of production expressions. A new format is
|
|
|
|
created explicitly by calling Parse, or implicitly by one of
|
|
|
|
the Xprintf functions.
|
|
|
|
|
|
|
|
Formatting rules are specified in the following syntax:
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
Format = Production { ";" Production } [ ";" ] .
|
|
|
|
Production = Name "=" Expression .
|
2009-04-25 17:36:17 -06:00
|
|
|
Name = identifier { "." identifier } .
|
2009-05-01 20:32:41 -06:00
|
|
|
Expression = [ Term ] { "|" [ Term ] } .
|
2009-04-25 17:36:17 -06:00
|
|
|
Term = Factor { Factor } .
|
2009-05-01 20:32:41 -06:00
|
|
|
Factor = string_literal | Indent | Field | Negation | Group | Option | Repetition .
|
|
|
|
Indent = ">>" Factor | "<<" .
|
|
|
|
Field = ( "^" | "*" | Name ) [ ":" Name ] .
|
|
|
|
Negation = "!" Factor .
|
2009-04-25 17:36:17 -06:00
|
|
|
Group = "(" Expression ")" .
|
|
|
|
Option = "[" Expression "]" .
|
|
|
|
Repetition = "{" Expression "}" .
|
|
|
|
|
|
|
|
The syntax of white space, comments, identifiers, and string literals is
|
|
|
|
the same as in Go.
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
A production name corresponds to a Go type name of the form
|
|
|
|
|
|
|
|
PackageName.TypeName
|
|
|
|
|
|
|
|
(for instance format.Format). A production of the form
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
Name;
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
specifies a package name which is prepended to all subsequent production
|
|
|
|
names:
|
|
|
|
|
|
|
|
format;
|
|
|
|
Format = ... // this production matches the type format.Format
|
|
|
|
|
|
|
|
The basic operands of productions are string literals, field names, and
|
|
|
|
designators. String literals are printed as is, unless they contain a
|
|
|
|
single %-style format specifier (such as "%d"). In that case, they are
|
|
|
|
used as the format for fmt.Printf, with the current value as argument.
|
|
|
|
|
|
|
|
The designator "^" stands for the current value; a "*" denotes indirection
|
|
|
|
(pointers, arrays, maps, and interfaces).
|
|
|
|
|
|
|
|
A field may contain a format specifier of the form
|
|
|
|
|
|
|
|
: Expression
|
|
|
|
|
|
|
|
which specifies the field format irrespective of the field type.
|
|
|
|
|
|
|
|
Default formats are used for types without specific formating rules:
|
|
|
|
The "%v" format is used for values of all types expect pointer, array,
|
|
|
|
map, and interface types. They are using the "^" designator.
|
|
|
|
|
|
|
|
TODO complete this description
|
|
|
|
*/
|
2009-04-24 18:22:58 -06:00
|
|
|
type Format map [string] expr;
|
2009-04-23 22:53:01 -06:00
|
|
|
|
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-04-23 22:53:01 -06:00
|
|
|
// Parsing
|
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
/* TODO
|
2009-04-25 17:36:17 -06:00
|
|
|
- have a format to select type name, field tag, field offset?
|
|
|
|
- use field tag as default format for that field
|
2009-04-24 18:22:58 -06:00
|
|
|
*/
|
|
|
|
|
2009-04-23 22:53:01 -06:00
|
|
|
type parser struct {
|
2009-05-06 17:28:18 -06:00
|
|
|
// scanning
|
2009-04-23 22:53:01 -06:00
|
|
|
scanner scanner.Scanner;
|
|
|
|
pos token.Position; // token position
|
|
|
|
tok token.Token; // one token look-ahead
|
|
|
|
lit []byte; // token literal
|
2009-05-06 17:28:18 -06:00
|
|
|
|
|
|
|
// error handling
|
|
|
|
errors io.ByteBuffer; // errors.Len() > 0 if there were errors
|
|
|
|
lastline int;
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// The parser implements the scanner.ErrorHandler interface.
|
|
|
|
func (p *parser) Error(pos token.Position, msg string) {
|
|
|
|
if pos.Line != p.lastline {
|
|
|
|
// only report error if not on the same line as previous error
|
|
|
|
// in the hope to reduce number of follow-up errors reported
|
2009-05-06 17:28:18 -06:00
|
|
|
fmt.Fprintf(&p.errors, "%d:%d: %s\n", pos.Line, pos.Column, msg);
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
p.lastline = pos.Line;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *parser) next() {
|
|
|
|
p.pos, p.tok, p.lit = p.scanner.Scan();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *parser) error_expected(pos token.Position, msg string) {
|
|
|
|
msg = "expected " + msg;
|
|
|
|
if pos.Offset == p.pos.Offset {
|
|
|
|
// the error happened at the current position;
|
|
|
|
// make the error message more specific
|
|
|
|
msg += ", found '" + p.tok.String() + "'";
|
|
|
|
if p.tok.IsLiteral() {
|
|
|
|
msg += " " + string(p.lit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.Error(pos, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *parser) expect(tok token.Token) token.Position {
|
|
|
|
pos := p.pos;
|
|
|
|
if p.tok != tok {
|
|
|
|
p.error_expected(pos, "'" + tok.String() + "'");
|
|
|
|
}
|
|
|
|
p.next(); // make progress in any case
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
func (p *parser) parseIdentifier() string {
|
2009-04-23 22:53:01 -06:00
|
|
|
name := string(p.lit);
|
|
|
|
p.expect(token.IDENT);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
func (p *parser) parseName() string {
|
|
|
|
name := p.parseIdentifier();
|
2009-04-23 22:53:01 -06:00
|
|
|
for p.tok == token.PERIOD {
|
|
|
|
p.next();
|
2009-04-24 18:22:58 -06:00
|
|
|
name = name + "." + p.parseIdentifier();
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *parser) parseValue() []byte {
|
|
|
|
if p.tok != token.STRING {
|
|
|
|
p.expect(token.STRING);
|
2009-04-25 17:36:17 -06:00
|
|
|
return nil; // TODO should return something else?
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
// TODO get rid of back-and-forth conversions
|
|
|
|
// (change value to string?)
|
|
|
|
s, err := strconv.Unquote(string(p.lit));
|
|
|
|
if err != nil {
|
|
|
|
panic("scanner error?");
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
2009-05-08 16:40:14 -06:00
|
|
|
|
2009-04-23 22:53:01 -06:00
|
|
|
p.next();
|
2009-04-25 17:36:17 -06:00
|
|
|
return io.StringBytes(s);
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
func (p *parser) parseFactor() (x expr)
|
2009-04-24 18:22:58 -06:00
|
|
|
func (p *parser) parseExpr() expr
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
func (p *parser) parseField() expr {
|
2009-05-01 20:32:41 -06:00
|
|
|
var fname string;
|
2009-04-23 22:53:01 -06:00
|
|
|
switch p.tok {
|
2009-04-24 18:22:58 -06:00
|
|
|
case token.XOR:
|
2009-05-01 20:32:41 -06:00
|
|
|
fname = "^";
|
2009-04-24 18:22:58 -06:00
|
|
|
p.next();
|
2009-04-23 22:53:01 -06:00
|
|
|
case token.MUL:
|
2009-05-01 20:32:41 -06:00
|
|
|
fname = "*";
|
2009-04-24 18:22:58 -06:00
|
|
|
p.next();
|
2009-04-23 22:53:01 -06:00
|
|
|
case token.IDENT:
|
2009-05-01 20:32:41 -06:00
|
|
|
// TODO use reflect.ExpandType() to lookup a field
|
|
|
|
// during parse-time if posssible
|
|
|
|
fname = p.parseName();
|
2009-04-24 18:22:58 -06:00
|
|
|
default:
|
2009-04-25 17:36:17 -06:00
|
|
|
return nil;
|
2009-04-24 18:22:58 -06:00
|
|
|
}
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
var tname string;
|
2009-04-24 18:22:58 -06:00
|
|
|
if p.tok == token.COLON {
|
|
|
|
p.next();
|
2009-05-01 20:32:41 -06:00
|
|
|
tname = p.parseName();
|
2009-04-24 18:22:58 -06:00
|
|
|
}
|
2009-05-08 16:40:14 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
return &field{fname, tname};
|
2009-04-24 18:22:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *parser) parseFactor() (x expr) {
|
|
|
|
switch p.tok {
|
2009-04-23 22:53:01 -06:00
|
|
|
case token.STRING:
|
2009-04-24 18:22:58 -06:00
|
|
|
x = &literal{p.parseValue()};
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
case token.SHR:
|
|
|
|
p.next();
|
|
|
|
x = &indentation{p.parseFactor()};
|
|
|
|
|
|
|
|
case token.SHL:
|
|
|
|
p.next();
|
|
|
|
x = &indentation{nil};
|
|
|
|
|
|
|
|
case token.NOT:
|
|
|
|
p.next();
|
|
|
|
x = &negation{p.parseFactor()};
|
|
|
|
|
2009-04-23 22:53:01 -06:00
|
|
|
case token.LPAREN:
|
|
|
|
p.next();
|
2009-04-24 18:22:58 -06:00
|
|
|
x = p.parseExpr();
|
2009-04-23 22:53:01 -06:00
|
|
|
p.expect(token.RPAREN);
|
|
|
|
|
|
|
|
case token.LBRACK:
|
|
|
|
p.next();
|
2009-04-24 18:22:58 -06:00
|
|
|
x = &option{p.parseExpr()};
|
2009-04-23 22:53:01 -06:00
|
|
|
p.expect(token.RBRACK);
|
|
|
|
|
|
|
|
case token.LBRACE:
|
|
|
|
p.next();
|
2009-05-01 20:32:41 -06:00
|
|
|
x = p.parseExpr();
|
|
|
|
var div expr;
|
|
|
|
if p.tok == token.QUO {
|
|
|
|
p.next();
|
|
|
|
div = p.parseExpr();
|
|
|
|
}
|
|
|
|
x = &repetition{x, div};
|
2009-04-23 22:53:01 -06:00
|
|
|
p.expect(token.RBRACE);
|
|
|
|
|
|
|
|
default:
|
2009-04-25 17:36:17 -06:00
|
|
|
x = p.parseField();
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
func (p *parser) parseTerm() expr {
|
2009-04-23 22:53:01 -06:00
|
|
|
x := p.parseFactor();
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
if x != nil {
|
|
|
|
for {
|
|
|
|
y := p.parseFactor();
|
|
|
|
if y == nil {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
x = &sequence{x, y};
|
2009-04-25 17:36:17 -06:00
|
|
|
}
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
func (p *parser) parseExpr() expr {
|
2009-04-23 22:53:01 -06:00
|
|
|
x := p.parseTerm();
|
|
|
|
|
|
|
|
for p.tok == token.OR {
|
|
|
|
p.next();
|
|
|
|
y := p.parseTerm();
|
2009-04-24 18:22:58 -06:00
|
|
|
x = &alternative{x, y};
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
func (p *parser) parseProd() (string, expr) {
|
|
|
|
name := p.parseName();
|
|
|
|
p.expect(token.ASSIGN);
|
|
|
|
x := p.parseExpr();
|
2009-05-08 16:40:14 -06:00
|
|
|
return name, x;
|
2009-05-01 20:32:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-23 22:53:01 -06:00
|
|
|
func (p *parser) parseFormat() Format {
|
|
|
|
format := make(Format);
|
2009-05-08 16:40:14 -06:00
|
|
|
|
2009-04-23 22:53:01 -06:00
|
|
|
for p.tok != token.EOF {
|
|
|
|
pos := p.pos;
|
2009-05-01 20:32:41 -06:00
|
|
|
name, x := p.parseProd();
|
|
|
|
|
|
|
|
// add production to format
|
|
|
|
if t, found := format[name]; !found {
|
|
|
|
format[name] = x;
|
|
|
|
} else {
|
|
|
|
p.Error(pos, "production already declared: " + name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.tok == token.SEMICOLON {
|
2009-04-25 17:36:17 -06:00
|
|
|
p.next();
|
|
|
|
} else {
|
2009-05-01 20:32:41 -06:00
|
|
|
break;
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
}
|
2009-05-01 20:32:41 -06:00
|
|
|
p.expect(token.EOF);
|
2009-05-08 16:40:14 -06:00
|
|
|
|
2009-04-23 22:53:01 -06:00
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-06 17:28:18 -06:00
|
|
|
type formatError string
|
|
|
|
|
|
|
|
func (p formatError) String() string {
|
2009-05-08 16:40:14 -06:00
|
|
|
return string(p);
|
2009-05-06 17:28:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func readSource(src interface{}) ([]byte, os.Error) {
|
|
|
|
if src == nil {
|
|
|
|
return nil, formatError("src is nil");
|
|
|
|
}
|
2009-04-23 22:53:01 -06:00
|
|
|
|
|
|
|
switch s := src.(type) {
|
|
|
|
case string:
|
2009-05-06 17:28:18 -06:00
|
|
|
return io.StringBytes(s), nil;
|
|
|
|
|
2009-04-23 22:53:01 -06:00
|
|
|
case []byte:
|
2009-05-06 17:28:18 -06:00
|
|
|
if s == nil {
|
|
|
|
return nil, formatError("src is nil");
|
|
|
|
}
|
|
|
|
return s, nil;
|
|
|
|
|
2009-04-23 22:53:01 -06:00
|
|
|
case *io.ByteBuffer:
|
|
|
|
// is io.Read, but src is already available in []byte form
|
2009-05-06 17:28:18 -06:00
|
|
|
if s == nil {
|
|
|
|
return nil, formatError("src is nil");
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
2009-05-06 17:28:18 -06:00
|
|
|
return s.Data(), nil;
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
case io.Reader:
|
2009-04-23 22:53:01 -06:00
|
|
|
var buf io.ByteBuffer;
|
2009-05-06 17:28:18 -06:00
|
|
|
n, err := io.Copy(s, &buf);
|
|
|
|
if err != nil {
|
|
|
|
return nil, err;
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
2009-05-06 17:28:18 -06:00
|
|
|
return buf.Data(), nil
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
|
2009-05-06 17:28:18 -06:00
|
|
|
return nil, formatError("src type not supported");
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
// Parse parses a set of format productions. The format src may be
|
|
|
|
// a string, a []byte, or implement io.Read. The result is a Format
|
|
|
|
// if no errors occured; otherwise Parse returns nil.
|
|
|
|
//
|
2009-05-06 17:28:18 -06:00
|
|
|
func Parse(src interface{}, fmap FormatterMap) (f Format, err os.Error) {
|
|
|
|
s, err := readSource(src);
|
|
|
|
if err != nil {
|
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse format description
|
2009-04-23 22:53:01 -06:00
|
|
|
var p parser;
|
2009-05-06 17:28:18 -06:00
|
|
|
p.scanner.Init(s, &p, false);
|
2009-04-23 22:53:01 -06:00
|
|
|
p.next();
|
2009-05-06 17:28:18 -06:00
|
|
|
f = p.parseFormat();
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-05-06 17:28:18 -06:00
|
|
|
// add custom formatters, if any
|
|
|
|
for name, form := range fmap {
|
|
|
|
if t, found := f[name]; !found {
|
|
|
|
f[name] = &custom{name, form};
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(&p.errors, "formatter already declared: %s", name);
|
2009-05-01 20:32:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-06 17:28:18 -06:00
|
|
|
if p.errors.Len() > 0 {
|
|
|
|
return nil, formatError(string(p.errors.Data()));
|
|
|
|
}
|
2009-05-08 16:40:14 -06:00
|
|
|
|
2009-05-06 17:28:18 -06:00
|
|
|
return f, nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func ParseOrDie(src interface{}, fmap FormatterMap) Format {
|
|
|
|
f, err := Parse(src, fmap);
|
|
|
|
if err != nil {
|
|
|
|
panic(err.String());
|
|
|
|
}
|
|
|
|
return f;
|
2009-05-01 20:32:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (f Format) Dump() {
|
|
|
|
for name, form := range f {
|
|
|
|
fmt.Printf("%s = %v;\n", name, form);
|
|
|
|
}
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Formatting
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-05-06 17:28:18 -06:00
|
|
|
func getField(v reflect.StructValue, fieldname string) reflect.Value {
|
2009-04-23 22:53:01 -06:00
|
|
|
t := v.Type().(reflect.StructType);
|
2009-05-06 17:28:18 -06:00
|
|
|
for i := 0; i < t.Len(); i++ {
|
2009-04-23 22:53:01 -06:00
|
|
|
name, typ, tag, offset := t.Field(i);
|
|
|
|
if name == fieldname {
|
2009-05-06 17:28:18 -06:00
|
|
|
return v.Field(i);
|
|
|
|
} else if name == "" {
|
|
|
|
// anonymous field - check type name
|
|
|
|
// TODO this is only going down one level - fix
|
|
|
|
if strings.HasSuffix(typ.Name(), "." + fieldname) {
|
|
|
|
return v.Field(i);
|
|
|
|
}
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
}
|
2009-05-06 17:28:18 -06:00
|
|
|
panicln(fmt.Sprintf("no field %s int %s", fieldname, t.Name()));
|
|
|
|
return nil;
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
func typename(value reflect.Value) string {
|
|
|
|
name := value.Type().Name();
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
if name != "" {
|
|
|
|
return name;
|
|
|
|
}
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.ArrayKind: name = "array";
|
|
|
|
case reflect.BoolKind: name = "bool";
|
|
|
|
case reflect.ChanKind: name = "chan";
|
2009-05-01 20:32:41 -06:00
|
|
|
case reflect.DotDotDotKind: name = "ellipsis";
|
2009-04-24 18:22:58 -06:00
|
|
|
case reflect.FloatKind: name = "float";
|
|
|
|
case reflect.Float32Kind: name = "float32";
|
|
|
|
case reflect.Float64Kind: name = "float64";
|
|
|
|
case reflect.FuncKind: name = "func";
|
|
|
|
case reflect.IntKind: name = "int";
|
|
|
|
case reflect.Int16Kind: name = "int16";
|
|
|
|
case reflect.Int32Kind: name = "int32";
|
|
|
|
case reflect.Int64Kind: name = "int64";
|
|
|
|
case reflect.Int8Kind: name = "int8";
|
|
|
|
case reflect.InterfaceKind: name = "interface";
|
|
|
|
case reflect.MapKind: name = "map";
|
|
|
|
case reflect.PtrKind: name = "pointer";
|
|
|
|
case reflect.StringKind: name = "string";
|
|
|
|
case reflect.StructKind: name = "struct";
|
|
|
|
case reflect.UintKind: name = "uint";
|
|
|
|
case reflect.Uint16Kind: name = "uint16";
|
|
|
|
case reflect.Uint32Kind: name = "uint32";
|
|
|
|
case reflect.Uint64Kind: name = "uint64";
|
|
|
|
case reflect.Uint8Kind: name = "uint8";
|
|
|
|
case reflect.UintptrKind: name = "uintptr";
|
|
|
|
}
|
2009-05-08 16:40:14 -06:00
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
return name;
|
|
|
|
}
|
2009-04-23 22:53:01 -06:00
|
|
|
|
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
var defaults = map [int] expr {
|
2009-05-01 20:32:41 -06:00
|
|
|
reflect.ArrayKind: &field{"*", ""},
|
|
|
|
reflect.DotDotDotKind: &field{"*", ""},
|
|
|
|
reflect.InterfaceKind: &field{"*", ""},
|
|
|
|
reflect.MapKind: &field{"*", ""},
|
|
|
|
reflect.PtrKind: &field{"*", ""},
|
|
|
|
reflect.StringKind: &literal{io.StringBytes("%s")},
|
2009-04-25 17:36:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var catchAll = &literal{io.StringBytes("%v")};
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
func (f Format) getFormat(name string, value reflect.Value) expr {
|
|
|
|
/*
|
|
|
|
if name == "nil" {
|
|
|
|
fmt.Printf("value = %T %v, kind = %d\n", value, value, value.Kind());
|
|
|
|
panic();
|
|
|
|
}
|
|
|
|
*/
|
2009-05-08 16:40:14 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
if fexpr, found := f[name]; found {
|
2009-04-25 17:36:17 -06:00
|
|
|
return fexpr;
|
|
|
|
}
|
2009-05-08 16:40:14 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
if *debug {
|
|
|
|
fmt.Printf("no production for type: %s\n", name);
|
|
|
|
}
|
2009-04-25 17:36:17 -06:00
|
|
|
|
|
|
|
// no fexpr found - return kind-specific default value, if any
|
|
|
|
if fexpr, found := defaults[value.Kind()]; found {
|
|
|
|
return fexpr;
|
2009-04-24 18:22:58 -06:00
|
|
|
}
|
2009-04-25 17:36:17 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
if *debug {
|
|
|
|
fmt.Printf("no default for type: %s\n", name);
|
|
|
|
}
|
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
return catchAll;
|
2009-04-24 18:22:58 -06:00
|
|
|
}
|
2009-04-23 22:53:01 -06:00
|
|
|
|
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
// Count the number of printf-style '%' formatters in s.
|
|
|
|
//
|
|
|
|
func percentCount(s []byte) int {
|
|
|
|
n := 0;
|
2009-05-01 20:32:41 -06:00
|
|
|
for i := 0; i < len(s); i++ {
|
2009-04-24 18:22:58 -06:00
|
|
|
if s[i] == '%' {
|
2009-05-01 20:32:41 -06:00
|
|
|
i++;
|
|
|
|
if i >= len(s) || s[i] != '%' { // don't count "%%"
|
|
|
|
n++;
|
|
|
|
}
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
}
|
2009-04-24 18:22:58 -06:00
|
|
|
return n;
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
func rawPrintf(w io.Writer, format []byte, value reflect.Value) {
|
2009-05-01 20:32:41 -06:00
|
|
|
// TODO find a better way to do this
|
|
|
|
x := value.Interface();
|
|
|
|
switch percentCount(format) {
|
|
|
|
case 0: w.Write(format);
|
|
|
|
case 1: fmt.Fprintf(w, string(format), x);
|
|
|
|
case 2: fmt.Fprintf(w, string(format), x, x);
|
|
|
|
case 3: fmt.Fprintf(w, string(format), x, x, x);
|
|
|
|
case 4: fmt.Fprintf(w, string(format), x, x, x, x);
|
|
|
|
default: panic("no support for more than 4 '%'-format chars yet");
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
// TODO this should become a Go built-in
|
|
|
|
func push(dst []int, x int) []int {
|
|
|
|
n := len(dst);
|
|
|
|
if n > cap(dst) {
|
|
|
|
panic("dst too small");
|
2009-04-25 17:36:17 -06:00
|
|
|
}
|
2009-05-01 20:32:41 -06:00
|
|
|
dst = dst[0 : n+1];
|
|
|
|
dst[n] = x;
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func append(dst, src []byte) []byte {
|
|
|
|
n, m := len(dst), len(src);
|
|
|
|
if n+m > cap(dst) {
|
|
|
|
panic("dst too small");
|
|
|
|
}
|
|
|
|
dst = dst[0 : n+m];
|
|
|
|
for i := 0; i < m; i++ {
|
|
|
|
dst[n+i] = src[i];
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type state struct {
|
|
|
|
f Format;
|
2009-05-08 16:40:14 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
// indentation
|
|
|
|
indent_text []byte;
|
|
|
|
indent_widths []int;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ps *state) init(f Format) {
|
|
|
|
ps.f = f;
|
|
|
|
ps.indent_text = make([]byte, 0, 1000); // TODO don't use fixed cap
|
|
|
|
ps.indent_widths = make([]int, 0, 100); // TODO don't use fixed cap
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ps *state) indent(text []byte) {
|
|
|
|
ps.indent_widths = push(ps.indent_widths, len(ps.indent_text));
|
|
|
|
ps.indent_text = append(ps.indent_text, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ps *state) outdent() {
|
|
|
|
i := len(ps.indent_widths);
|
|
|
|
if i > 0 {
|
|
|
|
ps.indent_text = ps.indent_text[0 : ps.indent_widths[i-1]];
|
|
|
|
ps.indent_widths = ps.indent_widths[0 : i-1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
func (ps *state) printIndented(w io.Writer, s []byte) {
|
2009-05-01 20:32:41 -06:00
|
|
|
// replace each '\n' with the indent + '\n'
|
|
|
|
i0 := 0;
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if s[i] == '\n' {
|
|
|
|
w.Write(s[i0 : i+1]);
|
|
|
|
w.Write(ps.indent_text);
|
|
|
|
i0 = i+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Write(s[i0 : len(s)]);
|
2009-04-28 20:11:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
func (ps *state) printf(w io.Writer, format []byte, value reflect.Value) {
|
2009-05-01 20:32:41 -06:00
|
|
|
if len(ps.indent_widths) == 0 {
|
|
|
|
// no indentation
|
|
|
|
rawPrintf(w, format, value);
|
|
|
|
} else {
|
|
|
|
// print into temporary buffer
|
|
|
|
var buf io.ByteBuffer;
|
|
|
|
rawPrintf(&buf, format, value);
|
|
|
|
ps.printIndented(w, buf.Data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
func (ps *state) print(w io.Writer, fexpr expr, value reflect.Value, index, level int) bool
|
2009-04-25 17:36:17 -06:00
|
|
|
|
2009-04-28 20:11:37 -06:00
|
|
|
// Returns true if a non-empty field value was found.
|
2009-05-08 12:22:57 -06:00
|
|
|
func (ps *state) print0(w io.Writer, fexpr expr, value reflect.Value, index, level int) bool {
|
2009-04-25 17:36:17 -06:00
|
|
|
if fexpr == nil {
|
2009-04-24 18:22:58 -06:00
|
|
|
return true;
|
2009-04-25 17:36:17 -06:00
|
|
|
}
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
switch t := fexpr.(type) {
|
2009-04-24 18:22:58 -06:00
|
|
|
case *alternative:
|
2009-04-25 17:36:17 -06:00
|
|
|
// - print the contents of the first alternative with a non-empty field
|
|
|
|
// - result is true if there is at least one non-empty field
|
2009-04-24 18:22:58 -06:00
|
|
|
var buf io.ByteBuffer;
|
2009-05-01 20:32:41 -06:00
|
|
|
if ps.print(&buf, t.x, value, 0, level) {
|
2009-04-24 18:22:58 -06:00
|
|
|
w.Write(buf.Data());
|
2009-04-28 20:11:37 -06:00
|
|
|
return true;
|
2009-04-25 17:36:17 -06:00
|
|
|
} else {
|
2009-05-01 20:32:41 -06:00
|
|
|
var buf io.ByteBuffer;
|
|
|
|
if ps.print(&buf, t.y, value, 0, level) {
|
2009-04-25 17:36:17 -06:00
|
|
|
w.Write(buf.Data());
|
2009-04-28 20:11:37 -06:00
|
|
|
return true;
|
2009-04-25 17:36:17 -06:00
|
|
|
}
|
2009-04-24 18:22:58 -06:00
|
|
|
}
|
2009-04-28 20:11:37 -06:00
|
|
|
return false;
|
2009-04-24 18:22:58 -06:00
|
|
|
|
|
|
|
case *sequence:
|
2009-04-25 17:36:17 -06:00
|
|
|
// - print the contents of the sequence
|
|
|
|
// - result is true if there is no empty field
|
|
|
|
// TODO do we need to buffer here? why not?
|
2009-05-01 20:32:41 -06:00
|
|
|
b1 := ps.print(w, t.x, value, index, level);
|
|
|
|
b2 := ps.print(w, t.y, value, index, level);
|
2009-04-25 17:36:17 -06:00
|
|
|
return b1 && b2;
|
2009-04-24 18:22:58 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
case *literal:
|
|
|
|
// - print the literal
|
|
|
|
// - result is always true (literal is never empty)
|
|
|
|
ps.printf(w, t.value, value);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case *indentation:
|
|
|
|
if t.iexpr != nil {
|
|
|
|
// indent
|
|
|
|
var buf io.ByteBuffer;
|
|
|
|
ps.print(&buf, t.iexpr, value, index, level);
|
|
|
|
ps.indent(buf.Data());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// outdent
|
|
|
|
ps.outdent();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
case *field:
|
2009-04-25 17:36:17 -06:00
|
|
|
// - print the contents of the field
|
|
|
|
// - format is either the field format or the type-specific format
|
|
|
|
// - TODO look at field tag for default format
|
|
|
|
// - result is true if the field is not empty
|
2009-05-01 20:32:41 -06:00
|
|
|
switch t.fname {
|
2009-04-24 18:22:58 -06:00
|
|
|
case "^":
|
2009-04-25 17:36:17 -06:00
|
|
|
// identity - value doesn't change
|
|
|
|
|
|
|
|
case "*":
|
|
|
|
// indirect
|
|
|
|
switch v := value.(type) {
|
|
|
|
case reflect.ArrayValue:
|
2009-05-01 20:32:41 -06:00
|
|
|
if v.Len() <= index {
|
2009-04-24 18:22:58 -06:00
|
|
|
return false;
|
|
|
|
}
|
2009-04-25 17:36:17 -06:00
|
|
|
value = v.Elem(index);
|
|
|
|
|
|
|
|
case reflect.MapValue:
|
|
|
|
panic("reflection support for maps incomplete");
|
|
|
|
|
2009-04-28 20:11:37 -06:00
|
|
|
case reflect.PtrValue:
|
2009-05-01 20:32:41 -06:00
|
|
|
if v.Get() == nil {
|
2009-04-28 20:11:37 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value = v.Sub();
|
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
case reflect.InterfaceValue:
|
2009-05-01 20:32:41 -06:00
|
|
|
if v.Get() == nil {
|
2009-04-24 18:22:58 -06:00
|
|
|
return false;
|
|
|
|
}
|
2009-04-25 17:36:17 -06:00
|
|
|
value = v.Value();
|
|
|
|
|
|
|
|
default:
|
2009-05-01 20:32:41 -06:00
|
|
|
// TODO fix this
|
|
|
|
panic(fmt.Sprintf("error: * does not apply to `%s`\n", value.Type().Name()));
|
2009-04-28 20:11:37 -06:00
|
|
|
}
|
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
default:
|
2009-04-25 17:36:17 -06:00
|
|
|
// field
|
|
|
|
if s, is_struct := value.(reflect.StructValue); is_struct {
|
2009-05-06 17:28:18 -06:00
|
|
|
value = getField(s, t.fname);
|
2009-04-23 22:53:01 -06:00
|
|
|
} else {
|
2009-05-01 20:32:41 -06:00
|
|
|
// TODO fix this
|
|
|
|
panic(fmt.Sprintf("error: %s has no field `%s`\n", value.Type().Name(), t.fname));
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
2009-04-24 18:22:58 -06:00
|
|
|
}
|
2009-04-25 17:36:17 -06:00
|
|
|
|
|
|
|
// determine format
|
2009-05-01 20:32:41 -06:00
|
|
|
tname := t.tname;
|
|
|
|
if tname == "" {
|
|
|
|
tname = typename(value)
|
2009-04-25 17:36:17 -06:00
|
|
|
}
|
2009-05-01 20:32:41 -06:00
|
|
|
fexpr = ps.f.getFormat(tname, value);
|
2009-04-25 17:36:17 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
return ps.print(w, fexpr, value, index, level);
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
case *negation:
|
|
|
|
// TODO is this operation useful at all?
|
|
|
|
// print the contents of the option if is contains an empty field
|
|
|
|
var buf io.ByteBuffer;
|
|
|
|
if !ps.print(&buf, t.neg, value, 0, level) {
|
|
|
|
w.Write(buf.Data());
|
|
|
|
}
|
2009-04-24 18:22:58 -06:00
|
|
|
return true;
|
2009-05-08 16:40:14 -06:00
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
case *option:
|
2009-04-25 17:36:17 -06:00
|
|
|
// print the contents of the option if it contains a non-empty field
|
2009-04-24 18:22:58 -06:00
|
|
|
var buf io.ByteBuffer;
|
2009-05-01 20:32:41 -06:00
|
|
|
if ps.print(&buf, t.opt, value, 0, level) {
|
2009-04-24 18:22:58 -06:00
|
|
|
w.Write(buf.Data());
|
|
|
|
}
|
2009-04-28 20:11:37 -06:00
|
|
|
return true;
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
case *repetition:
|
|
|
|
// print the contents of the repetition while there is a non-empty field
|
2009-04-28 20:11:37 -06:00
|
|
|
var buf io.ByteBuffer;
|
2009-05-01 20:32:41 -06:00
|
|
|
for i := 0; ps.print(&buf, t.rep, value, i, level); i++ {
|
|
|
|
if i > 0 {
|
|
|
|
ps.print(w, t.div, value, i, level);
|
|
|
|
}
|
2009-04-28 20:11:37 -06:00
|
|
|
w.Write(buf.Data());
|
|
|
|
buf.Reset();
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
2009-04-28 20:11:37 -06:00
|
|
|
return true;
|
2009-05-08 16:40:14 -06:00
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
case *custom:
|
2009-05-01 20:32:41 -06:00
|
|
|
var buf io.ByteBuffer;
|
|
|
|
if t.form(&buf, value.Interface(), t.name) {
|
|
|
|
ps.printIndented(w, buf.Data());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
2009-04-25 17:36:17 -06:00
|
|
|
|
2009-04-24 18:22:58 -06:00
|
|
|
panic("unreachable");
|
|
|
|
return false;
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-28 20:11:37 -06:00
|
|
|
func printTrace(indent int, format string, a ...) {
|
|
|
|
const dots =
|
|
|
|
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "
|
|
|
|
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ";
|
|
|
|
const n = len(dots);
|
|
|
|
i := 2*indent;
|
|
|
|
for ; i > n; i -= n {
|
|
|
|
fmt.Print(dots);
|
|
|
|
}
|
|
|
|
fmt.Print(dots[0 : i]);
|
|
|
|
fmt.Printf(format, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
func (ps *state) print(w io.Writer, fexpr expr, value reflect.Value, index, level int) bool {
|
2009-05-01 20:32:41 -06:00
|
|
|
if *trace {
|
2009-04-28 20:11:37 -06:00
|
|
|
printTrace(level, "%v, %d {\n", fexpr, /*value.Interface(), */index);
|
|
|
|
}
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
result := ps.print0(w, fexpr, value, index, level+1);
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
if *trace {
|
2009-04-28 20:11:37 -06:00
|
|
|
printTrace(level, "} %v\n", result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-25 17:36:17 -06:00
|
|
|
// TODO proper error reporting
|
|
|
|
|
|
|
|
// Fprint formats each argument according to the format f
|
|
|
|
// and writes to w.
|
|
|
|
//
|
2009-05-08 12:22:57 -06:00
|
|
|
func (f Format) Fprint(w io.Writer, args ...) {
|
2009-04-25 17:36:17 -06:00
|
|
|
value := reflect.NewValue(args).(reflect.StructValue);
|
|
|
|
for i := 0; i < value.Len(); i++ {
|
2009-05-06 17:28:18 -06:00
|
|
|
fld := value.Field(i);
|
2009-05-01 20:32:41 -06:00
|
|
|
var ps state;
|
|
|
|
ps.init(f);
|
|
|
|
ps.print(w, f.getFormat(typename(fld), fld), fld, 0, 0);
|
2009-04-25 17:36:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-28 20:11:37 -06:00
|
|
|
// Print formats each argument according to the format f
|
2009-04-25 17:36:17 -06:00
|
|
|
// and writes to standard output.
|
|
|
|
//
|
|
|
|
func (f Format) Print(args ...) {
|
2009-05-01 20:32:41 -06:00
|
|
|
f.Fprint(os.Stdout, args);
|
2009-04-25 17:36:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-28 20:11:37 -06:00
|
|
|
// Sprint formats each argument according to the format f
|
2009-04-25 17:36:17 -06:00
|
|
|
// and returns the resulting string.
|
|
|
|
//
|
|
|
|
func (f Format) Sprint(args ...) string {
|
|
|
|
var buf io.ByteBuffer;
|
|
|
|
f.Fprint(&buf, args);
|
|
|
|
return string(buf.Data());
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|