github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/cmd/asm/internal/lex/slice.go (about)

     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  	// TODO: Col is only called when defining a macro and all it cares about is increasing
    48  	// position to discover whether there is a blank before the parenthesis.
    49  	// We only get here if defining a macro inside a macro.
    50  	// This imperfect implementation means we cannot tell the difference between
    51  	//	#define A #define B(x) x
    52  	// and
    53  	//	#define A #define B (x) x
    54  	// The first has definition of B has an argument, the second doesn't. Because we let
    55  	// text/scanner strip the blanks for us, this is extremely rare, hard to fix, and not worth it.
    56  	return s.pos
    57  }
    58  
    59  func (s *Slice) SetPos(line int, file string) {
    60  	// Cannot happen because we only have slices of already-scanned
    61  	// text, but be prepared.
    62  	s.line = line
    63  	s.fileName = file
    64  }
    65  
    66  func (s *Slice) Close() {
    67  }