Commit 8f06e217 authored by Robert Griesemer's avatar Robert Griesemer

text/scanner: use correct token position in example

While at it, unindent source text so column values are easier
to read, remove unnecessary text in output, and simplify the
loop.

Fixes #20346.

Change-Id: I0fde02b9e4242383da427f4cf4c6c13dd0ab3b47
Reviewed-on: https://go-review.googlesource.com/43450
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 380aa884
...@@ -12,28 +12,25 @@ import ( ...@@ -12,28 +12,25 @@ import (
func Example() { func Example() {
const src = ` const src = `
// This is scanned code. // This is scanned code.
if a > 10 { if a > 10 {
someParsable = text someParsable = text
}` }`
var s scanner.Scanner var s scanner.Scanner
s.Filename = "example"
s.Init(strings.NewReader(src)) s.Init(strings.NewReader(src))
var tok rune s.Filename = "example"
for tok != scanner.EOF { for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
tok = s.Scan() fmt.Printf("%s: %s\n", s.Position, s.TokenText())
fmt.Println("At position", s.Pos(), ":", s.TokenText())
} }
// Output: // Output:
// At position example:3:4 : if // example:3:1: if
// At position example:3:6 : a // example:3:4: a
// At position example:3:8 : > // example:3:6: >
// At position example:3:11 : 10 // example:3:8: 10
// At position example:3:13 : { // example:3:11: {
// At position example:4:15 : someParsable // example:4:2: someParsable
// At position example:4:17 : = // example:4:15: =
// At position example:4:22 : text // example:4:17: text
// At position example:5:3 : } // example:5:1: }
// At position example:5:3 :
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment