cmd/guru: support streaming plain and -json output

Visible changes:
- "referrers" queries now emit a stream of results,
  so they start appearing quickly even in large queries.
  We no longer report the total number of matches.
- packageReferrers now also uses AfterTypeCheck hook and streaming.
- XML support has been dropped.
- The -format flag has been replaced by -json.

JSON protocol changes:
- The enclosing Result struct has been removed.
- Likewise the 'mode' field (since the caller knows it already)
- "freevars" and "referrers" now emit a stream of objects
  In the case of referrers, the first object has a different from the rest.
- The "referrers" results include the text of the matching line
  (parity with -json=false)

Implementation details:
- the concurrency-safe q.Output function can be called
  many times, each with a queryResult to print.
- fset is no longer saved in Query (cleaner)
- queryResult methods renamed PrintPlain, JSON

Change-Id: I41a4e3f57f266fcf043ece4045bca82c6f6a356f
Reviewed-on: https://go-review.googlesource.com/21397
Reviewed-by: Michael Matloob <matloob@golang.org>
diff --git a/cmd/guru/callstack.go b/cmd/guru/callstack.go
index b2160b9..a187072 100644
--- a/cmd/guru/callstack.go
+++ b/cmd/guru/callstack.go
@@ -96,12 +96,11 @@
 		}
 	}
 
-	q.Fset = fset
-	q.result = &callstackResult{
+	q.Output(fset, &callstackResult{
 		qpos:     qpos,
 		target:   target,
 		callpath: callpath,
-	}
+	})
 	return nil
 }
 
@@ -111,7 +110,7 @@
 	callpath []*callgraph.Edge
 }
 
-func (r *callstackResult) display(printf printfFunc) {
+func (r *callstackResult) PrintPlain(printf printfFunc) {
 	if r.callpath != nil {
 		printf(r.qpos, "Found a call path from root to %s", r.target)
 		printf(r.target, "%s", r.target)
@@ -124,7 +123,7 @@
 	}
 }
 
-func (r *callstackResult) toSerial(res *serial.Result, fset *token.FileSet) {
+func (r *callstackResult) JSON(fset *token.FileSet) []byte {
 	var callers []serial.Caller
 	for i := len(r.callpath) - 1; i >= 0; i-- { // (innermost first)
 		edge := r.callpath[i]
@@ -134,9 +133,9 @@
 			Desc:   edge.Description(),
 		})
 	}
-	res.Callstack = &serial.CallStack{
+	return toJSON(&serial.CallStack{
 		Pos:     fset.Position(r.target.Pos()).String(),
 		Target:  r.target.String(),
 		Callers: callers,
-	}
+	})
 }