Source file src/cmd/asm/internal/lex/slice.go

     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 (
     8  	"text/scanner"
     9  
    10  	"cmd/internal/src"
    11  )
    12  
    13  // A Slice reads from a slice of Tokens.
    14  type Slice struct {
    15  	tokens []Token
    16  	base   *src.PosBase
    17  	line   int
    18  	pos    int
    19  }
    20  
    21  func NewSlice(base *src.PosBase, line int, tokens []Token) *Slice {
    22  	return &Slice{
    23  		tokens: tokens,
    24  		base:   base,
    25  		line:   line,
    26  		pos:    -1, // Next will advance to zero.
    27  	}
    28  }
    29  
    30  func (s *Slice) Next() ScanToken {
    31  	s.pos++
    32  	if s.pos >= len(s.tokens) {
    33  		return scanner.EOF
    34  	}
    35  	return s.tokens[s.pos].ScanToken
    36  }
    37  
    38  func (s *Slice) Text() string {
    39  	return s.tokens[s.pos].text
    40  }
    41  
    42  func (s *Slice) File() string {
    43  	return s.base.Filename()
    44  }
    45  
    46  func (s *Slice) Base() *src.PosBase {
    47  	return s.base
    48  }
    49  
    50  func (s *Slice) SetBase(base *src.PosBase) {
    51  	// Cannot happen because we only have slices of already-scanned text,
    52  	// but be prepared.
    53  	s.base = base
    54  }
    55  
    56  func (s *Slice) Line() int {
    57  	return s.line
    58  }
    59  
    60  func (s *Slice) Col() int {
    61  	// TODO: Col is only called when defining a macro and all it cares about is increasing
    62  	// position to discover whether there is a blank before the parenthesis.
    63  	// We only get here if defining a macro inside a macro.
    64  	// This imperfect implementation means we cannot tell the difference between
    65  	//	#define A #define B(x) x
    66  	// and
    67  	//	#define A #define B (x) x
    68  	// The first definition of B has an argument, the second doesn't. Because we let
    69  	// text/scanner strip the blanks for us, this is extremely rare, hard to fix, and not worth it.
    70  	return s.pos
    71  }
    72  
    73  func (s *Slice) Close() {
    74  }
    75  

View as plain text