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