github.com/denormal/go-gitignore@v0.0.0-20180930084346-ae8ad1d07817/token.go (about)

     1  package gitignore
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // Token represents a parsed token from a .gitignore stream, encapsulating the
     8  // token type, the runes comprising the token, and the position within the
     9  // stream of the first rune of the token.
    10  type Token struct {
    11  	Type TokenType
    12  	Word []rune
    13  	Position
    14  }
    15  
    16  // NewToken returns a Token instance of the given t, represented by the
    17  // word runes, at the stream position pos. If the token type is not know, the
    18  // returned instance will have type BAD.
    19  func NewToken(t TokenType, word []rune, pos Position) *Token {
    20  	// ensure the type is valid
    21  	if t < ILLEGAL || t > BAD {
    22  		t = BAD
    23  	}
    24  
    25  	// return the token
    26  	return &Token{Type: t, Word: word, Position: pos}
    27  } // NewToken()
    28  
    29  // Name returns a string representation of the Token type.
    30  func (t *Token) Name() string {
    31  	return t.Type.String()
    32  } // Name()
    33  
    34  // Token returns the string representation of the Token word.
    35  func (t *Token) Token() string {
    36  	return string(t.Word)
    37  } // Token()
    38  
    39  // String returns a string representation of the Token, encapsulating its
    40  // position in the input stream, its name (i.e. type), and its runes.
    41  func (t *Token) String() string {
    42  	return fmt.Sprintf("%s: %s %q", t.Position.String(), t.Name(), t.Token())
    43  } // String()