forked from golang/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
67 lines (56 loc) · 1.58 KB
/
slice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lex
import "text/scanner"
// A Slice reads from a slice of Tokens.
type Slice struct {
tokens []Token
fileName string
line int
pos int
}
func NewSlice(fileName string, line int, tokens []Token) *Slice {
return &Slice{
tokens: tokens,
fileName: fileName,
line: line,
pos: -1, // Next will advance to zero.
}
}
func (s *Slice) Next() ScanToken {
s.pos++
if s.pos >= len(s.tokens) {
return scanner.EOF
}
return s.tokens[s.pos].ScanToken
}
func (s *Slice) Text() string {
return s.tokens[s.pos].text
}
func (s *Slice) File() string {
return s.fileName
}
func (s *Slice) Line() int {
return s.line
}
func (s *Slice) Col() int {
// TODO: Col is only called when defining a macro and all it cares about is increasing
// position to discover whether there is a blank before the parenthesis.
// We only get here if defining a macro inside a macro.
// This imperfect implementation means we cannot tell the difference between
// #define A #define B(x) x
// and
// #define A #define B (x) x
// The first has definition of B has an argument, the second doesn't. Because we let
// text/scanner strip the blanks for us, this is extremely rare, hard to fix, and not worth it.
return s.pos
}
func (s *Slice) SetPos(line int, file string) {
// Cannot happen because we only have slices of already-scanned
// text, but be prepared.
s.line = line
s.fileName = file
}
func (s *Slice) Close() {
}