]> Cypherpunks repositories - gostls13.git/commitdiff
go/scanner: replace comment with example
authorRobert Griesemer <gri@golang.org>
Fri, 17 Feb 2012 17:26:36 +0000 (09:26 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 17 Feb 2012 17:26:36 +0000 (09:26 -0800)
R=r
CC=golang-dev
https://golang.org/cl/5676074

src/pkg/go/scanner/example_test.go [new file with mode: 0644]
src/pkg/go/scanner/scanner.go

diff --git a/src/pkg/go/scanner/example_test.go b/src/pkg/go/scanner/example_test.go
new file mode 100644 (file)
index 0000000..850e536
--- /dev/null
@@ -0,0 +1,46 @@
+// Copyright 2012 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.
+
+package scanner_test
+
+import (
+       "fmt"
+       "go/scanner"
+       "go/token"
+)
+
+func ExampleScanner_Scan() {
+       // src is the input that we want to tokenize.
+       src := []byte("cos(x) + 1i*sin(x) // Euler")
+
+       // Initialize the scanner.
+       var s scanner.Scanner
+       fset := token.NewFileSet()                      // positions are relative to fset
+       file := fset.AddFile("", fset.Base(), len(src)) // register input "file"
+       s.Init(file, src, /* no error handler: */ nil, scanner.ScanComments)
+
+       // Repeated calls to Scan yield the token sequence found in the input.
+       for {
+               pos, tok, lit := s.Scan()
+               if tok == token.EOF {
+                       break
+               }
+               fmt.Printf("%s\t%s\t%q\n", fset.Position(pos), tok, lit)
+       }
+
+       // output:
+       // 1:1  IDENT   "cos"
+       // 1:4  (       ""
+       // 1:5  IDENT   "x"
+       // 1:6  )       ""
+       // 1:8  +       ""
+       // 1:10 IMAG    "1i"
+       // 1:12 *       ""
+       // 1:13 IDENT   "sin"
+       // 1:16 (       ""
+       // 1:17 IDENT   "x"
+       // 1:18 )       ""
+       // 1:20 ;       "\n"
+       // 1:20 COMMENT "// Euler"
+}
index 458e1f9f37a8a7b69ba07499922dd79aedb65ae2..2395363b0ec88afb2b07b827113dd623f0ad02ba 100644 (file)
@@ -2,21 +2,9 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// Package scanner implements a scanner for Go source text. Takes a []byte as
-// source which can then be tokenized through repeated calls to the Scan
-// function. Typical use:
-//
-//     var s scanner.Scanner
-//     fset := token.NewFileSet()  // position information is relative to fset
-//     file := fset.AddFile(filename, fset.Base(), len(src))  // register file
-//     s.Init(file, src, nil /* no error handler */, 0)
-//     for {
-//             pos, tok, lit := s.Scan()
-//             if tok == token.EOF {
-//                     break
-//             }
-//             // do something here with pos, tok, and lit
-//     }
+// Package scanner implements a scanner for Go source text.
+// It takes a []byte as source which can then be tokenized
+// through repeated calls to the Scan method.
 //
 package scanner