Home | History | Annotate | Download | only in scanner
      1 // Copyright 2012 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.
      4 
      5 package scanner_test
      6 
      7 import (
      8 	"fmt"
      9 	"go/scanner"
     10 	"go/token"
     11 )
     12 
     13 func ExampleScanner_Scan() {
     14 	// src is the input that we want to tokenize.
     15 	src := []byte("cos(x) + 1i*sin(x) // Euler")
     16 
     17 	// Initialize the scanner.
     18 	var s scanner.Scanner
     19 	fset := token.NewFileSet()                      // positions are relative to fset
     20 	file := fset.AddFile("", fset.Base(), len(src)) // register input "file"
     21 	s.Init(file, src, nil /* no error handler */, scanner.ScanComments)
     22 
     23 	// Repeated calls to Scan yield the token sequence found in the input.
     24 	for {
     25 		pos, tok, lit := s.Scan()
     26 		if tok == token.EOF {
     27 			break
     28 		}
     29 		fmt.Printf("%s\t%s\t%q\n", fset.Position(pos), tok, lit)
     30 	}
     31 
     32 	// output:
     33 	// 1:1	IDENT	"cos"
     34 	// 1:4	(	""
     35 	// 1:5	IDENT	"x"
     36 	// 1:6	)	""
     37 	// 1:8	+	""
     38 	// 1:10	IMAG	"1i"
     39 	// 1:12	*	""
     40 	// 1:13	IDENT	"sin"
     41 	// 1:16	(	""
     42 	// 1:17	IDENT	"x"
     43 	// 1:18	)	""
     44 	// 1:20	;	"\n"
     45 	// 1:20	COMMENT	"// Euler"
     46 }
     47