...
1
2
3
4
5
6
7
8 package src
9
10 import "fmt"
11
12
13 type XPos struct {
14 index int32
15 lico
16 }
17
18
19 var NoXPos XPos
20
21
22
23
24 func (p XPos) IsKnown() bool {
25 return p.index != 0 || p.Line() != 0
26 }
27
28
29
30 func (p XPos) Before(q XPos) bool {
31 n, m := p.index, q.index
32 return n < m || n == m && p.lico < q.lico
33 }
34
35
36 func (p XPos) SameFile(q XPos) bool {
37 return p.index == q.index
38 }
39
40
41 func (p XPos) SameFileAndLine(q XPos) bool {
42 return p.index == q.index && p.lico.SameLine(q.lico)
43 }
44
45
46
47 func (p XPos) After(q XPos) bool {
48 n, m := p.index, q.index
49 return n > m || n == m && p.lico > q.lico
50 }
51
52
53 func (p XPos) WithNotStmt() XPos {
54 p.lico = p.lico.withNotStmt()
55 return p
56 }
57
58
59 func (p XPos) WithDefaultStmt() XPos {
60 p.lico = p.lico.withDefaultStmt()
61 return p
62 }
63
64
65 func (p XPos) WithIsStmt() XPos {
66 p.lico = p.lico.withIsStmt()
67 return p
68 }
69
70
71
72
73
74
75 func (p XPos) WithBogusLine() XPos {
76 if p.index == 0 {
77
78 panic("Assigning a bogus line to XPos with no file will cause mysterious downstream failures.")
79 }
80 p.lico = makeBogusLico()
81 return p
82 }
83
84
85 func (p XPos) WithXlogue(x PosXlogue) XPos {
86 p.lico = p.lico.withXlogue(x)
87 return p
88 }
89
90
91 func (p XPos) LineNumber() string {
92 if !p.IsKnown() {
93 return "?"
94 }
95 return p.lico.lineNumber()
96 }
97
98
99 func (p XPos) ColumnNumber() string {
100 if !p.IsKnown() {
101 return "?"
102 }
103 return fmt.Sprintf("%d", p.lico.Col())
104 }
105
106
107
108
109 func (p XPos) FileIndex() int32 {
110 return p.index
111 }
112
113 func (p XPos) LineNumberHTML() string {
114 if !p.IsKnown() {
115 return "?"
116 }
117 return p.lico.lineNumberHTML()
118 }
119
120
121 func (p XPos) AtColumn1() XPos {
122 p.lico = p.lico.atColumn1()
123 return p
124 }
125
126
127
128 type PosTable struct {
129 baseList []*PosBase
130 indexMap map[*PosBase]int
131 nameMap map[string]int
132 }
133
134
135
136 func (t *PosTable) XPos(pos Pos) XPos {
137 return XPos{t.baseIndex(pos.base), pos.lico}
138 }
139
140 func (t *PosTable) baseIndex(base *PosBase) int32 {
141 if base == nil {
142 return 0
143 }
144
145 if i, ok := t.indexMap[base]; ok {
146 return int32(i)
147 }
148
149 if base.fileIndex >= 0 {
150 panic("PosBase already registered with a PosTable")
151 }
152
153 if t.indexMap == nil {
154 t.baseList = append(t.baseList, nil)
155 t.indexMap = make(map[*PosBase]int)
156 t.nameMap = make(map[string]int)
157 }
158
159 i := len(t.baseList)
160 t.indexMap[base] = i
161 t.baseList = append(t.baseList, base)
162
163 fileIndex, ok := t.nameMap[base.absFilename]
164 if !ok {
165 fileIndex = len(t.nameMap)
166 t.nameMap[base.absFilename] = fileIndex
167 }
168 base.fileIndex = fileIndex
169
170 return int32(i)
171 }
172
173
174
175 func (t *PosTable) Pos(p XPos) Pos {
176 var base *PosBase
177 if p.index != 0 {
178 base = t.baseList[p.index]
179 }
180 return Pos{base, p.lico}
181 }
182
183
184 func (t *PosTable) FileTable() []string {
185
186
187
188 fileLUT := make([]string, len(t.nameMap))
189 for str, i := range t.nameMap {
190 fileLUT[i] = str
191 }
192 return fileLUT
193 }
194
View as plain text