Home | History | Annotate | Download | only in lex
      1 // Copyright 2015 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package lex
      6 
      7 import "text/scanner"
      8 
      9 // A Slice reads from a slice of Tokens.
     10 type Slice struct {
     11 	tokens   []Token
     12 	fileName string
     13 	line     int
     14 	pos      int
     15 }
     16 
     17 func NewSlice(fileName string, line int, tokens []Token) *Slice {
     18 	return &Slice{
     19 		tokens:   tokens,
     20 		fileName: fileName,
     21 		line:     line,
     22 		pos:      -1, // Next will advance to zero.
     23 	}
     24 }
     25 
     26 func (s *Slice) Next() ScanToken {
     27 	s.pos++
     28 	if s.pos >= len(s.tokens) {
     29 		return scanner.EOF
     30 	}
     31 	return s.tokens[s.pos].ScanToken
     32 }
     33 
     34 func (s *Slice) Text() string {
     35 	return s.tokens[s.pos].text
     36 }
     37 
     38 func (s *Slice) File() string {
     39 	return s.fileName
     40 }
     41 
     42 func (s *Slice) Line() int {
     43 	return s.line
     44 }
     45 
     46 func (s *Slice) Col() int {
     47 	// Col is only called when defining a macro, which can't reach here.
     48 	panic("cannot happen: slice col")
     49 }
     50 
     51 func (s *Slice) SetPos(line int, file string) {
     52 	// Cannot happen because we only have slices of already-scanned
     53 	// text, but be prepared.
     54 	s.line = line
     55 	s.fileName = file
     56 }
     57 
     58 func (s *Slice) Close() {
     59 }
     60