github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/compiler/scanner/example_test.go (about)

     1  package scanner_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/hirochachacha/plua/compiler/scanner"
     8  	"github.com/hirochachacha/plua/compiler/token"
     9  )
    10  
    11  func ExampleScan() {
    12  	f, err := os.Open("testdata/example.lua")
    13  	if err != nil {
    14  		panic(err)
    15  	}
    16  
    17  	s := scanner.Scan(f, "@"+"testdata/example.lua", 0)
    18  
    19  	for {
    20  		tok, err := s.Token()
    21  		if err != nil {
    22  			panic(err)
    23  		}
    24  		if tok.Type == token.EOF {
    25  			break
    26  		}
    27  		fmt.Printf("line: %d, column: %d, tok: %s, lit: %s\n", tok.Pos.Line, tok.Pos.Column, tok.Type, tok.Lit)
    28  	}
    29  
    30  	// Output:
    31  	// line: 3, column: 1, tok: NAME, lit: print
    32  	// line: 3, column: 7, tok: STRING, lit: "Hello World"
    33  }