Commit 4657d7d7 authored by Rob Pike's avatar Rob Pike

exp/template: remove the visibility of the token channel from the parser.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/4675053
parent 13d048a2
...@@ -190,15 +190,20 @@ func (l *lexer) run() { ...@@ -190,15 +190,20 @@ func (l *lexer) run() {
close(l.items) close(l.items)
} }
// nextItem returns the next item from the input.
func (l *lexer) nextItem() item {
return <-l.items
}
// lex launches a new scanner and returns the channel of items. // lex launches a new scanner and returns the channel of items.
func lex(name, input string) (*lexer, chan item) { func lex(name, input string) *lexer {
l := &lexer{ l := &lexer{
name: name, name: name,
input: input, input: input,
items: make(chan item), items: make(chan item),
} }
go l.run() go l.run()
return l, l.items return l
} }
// state functions // state functions
......
...@@ -127,8 +127,8 @@ var lexTests = []lexTest{ ...@@ -127,8 +127,8 @@ var lexTests = []lexTest{
// collect gathers the emitted items into a slice. // collect gathers the emitted items into a slice.
func collect(t *lexTest) (items []item) { func collect(t *lexTest) (items []item) {
_, tokens := lex(t.name, t.input) l := lex(t.name, t.input)
for i := range tokens { for i := range l.items {
items = append(items, i) items = append(items, i)
} }
return return
......
...@@ -23,7 +23,6 @@ type Template struct { ...@@ -23,7 +23,6 @@ type Template struct {
// Parsing only; cleared after parse. // Parsing only; cleared after parse.
set *Set set *Set
lex *lexer lex *lexer
tokens <-chan item
token item // token lookahead for parser token item // token lookahead for parser
havePeek bool havePeek bool
} }
...@@ -33,7 +32,7 @@ func (t *Template) next() item { ...@@ -33,7 +32,7 @@ func (t *Template) next() item {
if t.havePeek { if t.havePeek {
t.havePeek = false t.havePeek = false
} else { } else {
t.token = <-t.tokens t.token = t.lex.nextItem()
} }
return t.token return t.token
} }
...@@ -48,7 +47,7 @@ func (t *Template) peek() item { ...@@ -48,7 +47,7 @@ func (t *Template) peek() item {
if t.havePeek { if t.havePeek {
return t.token return t.token
} }
t.token = <-t.tokens t.token = t.lex.nextItem()
t.havePeek = true t.havePeek = true
return t.token return t.token
} }
...@@ -508,15 +507,15 @@ func (t *Template) recover(errp *os.Error) { ...@@ -508,15 +507,15 @@ func (t *Template) recover(errp *os.Error) {
} }
// startParse starts the template parsing from the lexer. // startParse starts the template parsing from the lexer.
func (t *Template) startParse(set *Set, lex *lexer, tokens <-chan item) { func (t *Template) startParse(set *Set, lex *lexer) {
t.root = nil t.root = nil
t.set = set t.set = set
t.lex, t.tokens = lex, tokens t.lex = lex
} }
// stopParse terminates parsing. // stopParse terminates parsing.
func (t *Template) stopParse() { func (t *Template) stopParse() {
t.set, t.lex, t.tokens = nil, nil, nil t.set, t.lex = nil, nil
} }
// atEOF returns true if, possibly after spaces, we're at EOF. // atEOF returns true if, possibly after spaces, we're at EOF.
...@@ -543,8 +542,7 @@ func (t *Template) atEOF() bool { ...@@ -543,8 +542,7 @@ func (t *Template) atEOF() bool {
// Parse parses the template definition string to construct an internal representation // Parse parses the template definition string to construct an internal representation
// of the template for execution. // of the template for execution.
func (t *Template) Parse(s string) (err os.Error) { func (t *Template) Parse(s string) (err os.Error) {
lexer, tokens := lex(t.name, s) t.startParse(nil, lex(t.name, s))
t.startParse(nil, lexer, tokens)
defer t.recover(&err) defer t.recover(&err)
t.parse(true) t.parse(true)
t.stopParse() t.stopParse()
...@@ -554,8 +552,7 @@ func (t *Template) Parse(s string) (err os.Error) { ...@@ -554,8 +552,7 @@ func (t *Template) Parse(s string) (err os.Error) {
// ParseInSet parses the template definition string to construct an internal representation // ParseInSet parses the template definition string to construct an internal representation
// of the template for execution. Function bindings are checked against those in the set. // of the template for execution. Function bindings are checked against those in the set.
func (t *Template) ParseInSet(s string, set *Set) (err os.Error) { func (t *Template) ParseInSet(s string, set *Set) (err os.Error) {
lexer, tokens := lex(t.name, s) t.startParse(set, lex(t.name, s))
t.startParse(set, lexer, tokens)
defer t.recover(&err) defer t.recover(&err)
t.parse(true) t.parse(true)
t.stopParse() t.stopParse()
......
...@@ -66,11 +66,11 @@ func (s *Set) recover(errp *os.Error) { ...@@ -66,11 +66,11 @@ func (s *Set) recover(errp *os.Error) {
// Parse parses the file into a set of named templates. // Parse parses the file into a set of named templates.
func (s *Set) Parse(text string) (err os.Error) { func (s *Set) Parse(text string) (err os.Error) {
defer s.recover(&err) defer s.recover(&err)
lex, tokens := lex("set", text) lex := lex("set", text)
const context = "define clause" const context = "define clause"
for { for {
t := New("set") // name will be updated once we know it. t := New("set") // name will be updated once we know it.
t.startParse(s, lex, tokens) t.startParse(s, lex)
// Expect EOF or "{{ define name }}". // Expect EOF or "{{ define name }}".
if t.atEOF() { if t.atEOF() {
return return
......
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