File: example_test.go

package info (click to toggle)
golang-github-go-git-gcfg 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 256 kB
  • sloc: makefile: 12
file content (54 lines) | stat: -rw-r--r-- 1,328 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 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"
	"log"

	"github.com/go-git/gcfg/v2/scanner"
	"github.com/go-git/gcfg/v2/token"
)

func ExampleScanner_Scan() {
	// src is the input that we want to tokenize.
	src := []byte(`[profile "A"]
color = blue ; Comment`)

	// Initialize the scanner.
	var s scanner.Scanner
	fset := token.NewFileSet()                           // positions are relative to fset
	file, err := fset.AddFile("", fset.Base(), len(src)) // register input "file"
	if err != nil {
		log.Fatalf("failed to add file: %v", err)
	}
	err = s.Init(file, src, nil /* no error handler */, scanner.ScanComments)
	if err != nil {
		log.Fatalf("failed to initialize scanner: %v", err)
	}

	// Repeated calls to Scan yield the token sequence found in the input.
	for {
		pos, tok, lit, err := s.Scan()
		if err != nil {
			log.Fatalf("failed to scan: %v", err)
		}
		if tok == token.EOF {
			break
		}
		fmt.Printf("%s\t%q\t%q\n", fset.Position(pos), tok, lit)
	}

	// output:
	// 1:1	"["	""
	// 1:2	"IDENT"	"profile"
	// 1:10	"STRING"	"\"A\""
	// 1:13	"]"	""
	// 1:14	"\n"	""
	// 2:1	"IDENT"	"color"
	// 2:7	"="	""
	// 2:9	"STRING"	"blue"
	// 2:14	"COMMENT"	"; Comment"
}