Commit 9299ae46 authored by Robert Griesemer's avatar Robert Griesemer

- don't call String method of AST nodes when converting them to text

- make token.Position.String more robust

TBR=rsc
DELTA=20  (10 added, 6 deleted, 4 changed)
OCL=32564
CL=32564
parent c670dc45
...@@ -213,19 +213,21 @@ func nodeText(node interface{}) []byte { ...@@ -213,19 +213,21 @@ func nodeText(node interface{}) []byte {
// Convert x, whatever it is, to text form. // Convert x, whatever it is, to text form.
func toText(x interface{}) []byte { func toText(x interface{}) []byte {
type String interface { String() string } type Stringer interface { String() string }
switch v := x.(type) { switch v := x.(type) {
case []byte: case []byte:
return v; return v;
case string: case string:
return strings.Bytes(v); return strings.Bytes(v);
case String:
return strings.Bytes(v.String());
case ast.Decl: case ast.Decl:
return nodeText(v); return nodeText(v);
case ast.Expr: case ast.Expr:
return nodeText(v); return nodeText(v);
case Stringer:
// last resort (AST nodes get a String method
// from token.Position - don't call that one)
return strings.Bytes(v.String());
} }
var buf bytes.Buffer; var buf bytes.Buffer;
fmt.Fprint(&buf, x); fmt.Fprint(&buf, x);
......
...@@ -353,6 +353,7 @@ func (pos *Position) IsValid() bool { ...@@ -353,6 +353,7 @@ func (pos *Position) IsValid() bool {
func (pos *Position) String() string { func (pos *Position) String() string {
if pos != nil {
s := pos.Filename; s := pos.Filename;
if pos.IsValid() { if pos.IsValid() {
if s != "" { if s != "" {
...@@ -363,5 +364,6 @@ func (pos *Position) String() string { ...@@ -363,5 +364,6 @@ func (pos *Position) String() string {
if s != "" { if s != "" {
return s; return s;
} }
}
return "<unknown position>"; return "<unknown position>";
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment