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
329990d5
Commit
329990d5
authored
Jul 06, 2011
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
exp/template: remove the need for a goroutine.
R=golang-dev, adg CC=golang-dev
https://golang.org/cl/4626095
parent
4657d7d7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
14 deletions
+20
-14
lex.go
src/pkg/exp/template/lex.go
+13
-12
lex_test.go
src/pkg/exp/template/lex_test.go
+7
-2
No files found.
src/pkg/exp/template/lex.go
View file @
329990d5
...
...
@@ -113,6 +113,7 @@ type stateFn func(*lexer) stateFn
type
lexer
struct
{
name
string
// the name of the input; used only for error reports.
input
string
// the string being scanned.
state
stateFn
// the next lexing function to enter
pos
int
// current position in the input.
start
int
// start position of this item.
width
int
// width of last rune read from input.
...
...
@@ -182,27 +183,27 @@ func (l *lexer) errorf(format string, args ...interface{}) stateFn {
return
nil
}
// run lexes the input by executing state functions until nil.
func
(
l
*
lexer
)
run
()
{
for
state
:=
lexText
;
state
!=
nil
;
{
state
=
state
(
l
)
}
close
(
l
.
items
)
}
// nextItem returns the next item from the input.
func
(
l
*
lexer
)
nextItem
()
item
{
return
<-
l
.
items
for
{
select
{
case
item
:=
<-
l
.
items
:
return
item
default
:
l
.
state
=
l
.
state
(
l
)
}
}
panic
(
"not reached"
)
}
// lex
launches a new scanner and returns the channel of items
.
// lex
creates a new scanner for the input string
.
func
lex
(
name
,
input
string
)
*
lexer
{
l
:=
&
lexer
{
name
:
name
,
input
:
input
,
items
:
make
(
chan
item
),
state
:
lexText
,
items
:
make
(
chan
item
,
2
),
// Two items of buffering is sufficient for all state functions
}
go
l
.
run
()
return
l
}
...
...
src/pkg/exp/template/lex_test.go
View file @
329990d5
...
...
@@ -128,14 +128,19 @@ var lexTests = []lexTest{
// collect gathers the emitted items into a slice.
func
collect
(
t
*
lexTest
)
(
items
[]
item
)
{
l
:=
lex
(
t
.
name
,
t
.
input
)
for
i
:=
range
l
.
items
{
items
=
append
(
items
,
i
)
for
{
item
:=
l
.
nextItem
()
items
=
append
(
items
,
item
)
if
item
.
typ
==
itemEOF
||
item
.
typ
==
itemError
{
break
}
}
return
}
func
TestLex
(
t
*
testing
.
T
)
{
for
_
,
test
:=
range
lexTests
{
println
(
test
.
name
)
items
:=
collect
(
&
test
)
if
!
reflect
.
DeepEqual
(
items
,
test
.
items
)
{
t
.
Errorf
(
"%s: got
\n\t
%v
\n
expected
\n\t
%v"
,
test
.
name
,
items
,
test
.
items
)
...
...
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