Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
ac6357b4
Commit
ac6357b4
authored
Feb 17, 2012
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
go/scanner: replace comment with example
R=r CC=golang-dev
https://golang.org/cl/5676074
parent
4f5ffe56
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
15 deletions
+49
-15
example_test.go
src/pkg/go/scanner/example_test.go
+46
-0
scanner.go
src/pkg/go/scanner/scanner.go
+3
-15
No files found.
src/pkg/go/scanner/example_test.go
0 → 100644
View file @
ac6357b4
// 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"
}
src/pkg/go/scanner/scanner.go
View file @
ac6357b4
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment