github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/asm/internal/lex/stack.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 ( 8 "text/scanner" 9 10 "github.com/gagliardetto/golang-go/cmd/internal/src" 11 ) 12 13 // A Stack is a stack of TokenReaders. As the top TokenReader hits EOF, 14 // it resumes reading the next one down. 15 type Stack struct { 16 tr []TokenReader 17 } 18 19 // Push adds tr to the top (end) of the input stack. (Popping happens automatically.) 20 func (s *Stack) Push(tr TokenReader) { 21 s.tr = append(s.tr, tr) 22 } 23 24 func (s *Stack) Next() ScanToken { 25 tos := s.tr[len(s.tr)-1] 26 tok := tos.Next() 27 for tok == scanner.EOF && len(s.tr) > 1 { 28 tos.Close() 29 // Pop the topmost item from the stack and resume with the next one down. 30 s.tr = s.tr[:len(s.tr)-1] 31 tok = s.Next() 32 } 33 return tok 34 } 35 36 func (s *Stack) Text() string { 37 return s.tr[len(s.tr)-1].Text() 38 } 39 40 func (s *Stack) File() string { 41 return s.Base().Filename() 42 } 43 44 func (s *Stack) Base() *src.PosBase { 45 return s.tr[len(s.tr)-1].Base() 46 } 47 48 func (s *Stack) SetBase(base *src.PosBase) { 49 s.tr[len(s.tr)-1].SetBase(base) 50 } 51 52 func (s *Stack) Line() int { 53 return s.tr[len(s.tr)-1].Line() 54 } 55 56 func (s *Stack) Col() int { 57 return s.tr[len(s.tr)-1].Col() 58 } 59 60 func (s *Stack) Close() { // Unused. 61 }