-
Notifications
You must be signed in to change notification settings - Fork 6
/
convert.go
428 lines (377 loc) · 10.4 KB
/
convert.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package types
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"sort"
"github.com/gowebapi/webidlparser/ast"
"github.com/gowebapi/webidlparser/parser"
)
var (
ErrStop = errors.New("too many errors")
)
// handle this as global variables is not ideal. However,
// the usage of Basic type inside all templates make it
// a lot of work. The issue will highlight it self if
// we are using multple go routines.
// TransformBasic is used to make modification to BasicInfo
// structure when it's returned from all types. Main usage
// is to change package name references when query for a type.
var TransformBasic = func(t TypeRef, basic BasicInfo) BasicInfo {
return basic
}
type Type interface {
TypeRef
GetRef
AllSourceReferences() []*Ref
// key to use in Convert.Types
key() string
// Used indicate that it's a interface or a type that is used by
// an interface
InUse() bool
SetInUse(value bool)
SetBasic(basic BasicInfo)
TypeID() TypeID
}
type Convert struct {
Types map[string]Type
All []Type
Enums []*Enum
Callbacks []*Callback
Dictionary []*Dictionary
partialDict []*Dictionary
Interface []*Interface
partialIf []*Interface
Merge map[string]MergeLink
mixin map[string]*mixin
partialMixin []*mixin
includes []*includes
Unions []*UnionType
HaveError bool
setup *Setup
}
type UserMsgFn func(ref GetRef, format string, args ...interface{})
type Setup struct {
Package string
Filename string
Warning, Error UserMsgFn
}
type TypeID int
const (
TypeInterface TypeID = iota
TypeEnum
TypeCallback
TypeDictionary
TypeTypeDef
)
func (id TypeID) IsPublic() bool {
return id != TypeTypeDef
}
func NewConvert() *Convert {
return &Convert{
Types: make(map[string]Type),
Enums: []*Enum{},
mixin: make(map[string]*mixin),
Merge: make(map[string]MergeLink),
}
}
// Load is reading a file from disc and Process it
func (t *Convert) Load(setup *Setup) error {
content, err := ioutil.ReadFile(setup.Filename)
if err != nil {
return err
}
return t.Parse(content, setup)
}
// ParseTest is parsing a text and Process it
func (t *Convert) Parse(content []byte, setup *Setup) error {
t.setup = setup
list := &extractTypes{main: t}
// main file parsing
if err := t.parseContent(content, list); err != nil {
return err
}
// queued protocol parsing
list.lineOffset = 1000000
if err := t.parseContent(list.protocol.Bytes(), list); err != nil {
fmt.Println("internal dump of type generated:\n",
string(insertLineNumber(list.protocol.Bytes())),
)
return err
}
return nil
}
func (t *Convert) parseContent(content []byte, list *extractTypes) error {
if len(content) == 0 {
return nil
}
file := parser.Parse(string(content))
trouble := ast.GetAllErrorNodes(file)
if len(trouble) > 0 {
sort.SliceStable(trouble, func(i, j int) bool { return trouble[i].Line < trouble[j].Line })
for _, e := range trouble {
ref := Ref{Filename: list.main.setup.Filename, Line: e.Line}
t.failing(&ref, e.Message)
}
return ErrStop
}
ast.Accept(file, list)
if t.HaveError {
return ErrStop
}
return nil
}
// Evaluate is doing verification on input IDL according
// to WebIDL specification. It also expand types, like removing
// typedef etc.
func (conv *Convert) Evaluate() error {
conv.innerSort()
if conv.processPartialAndMixin(); conv.HaveError {
return ErrStop
}
if conv.processTypeLinks(); conv.HaveError {
return ErrStop
}
if conv.verifyIndividualTypeCheck(); conv.HaveError {
return ErrStop
}
for _, inf := range conv.Interface {
if inf.haveReplacableMethods {
inf.Method = cleanupReplaceMethods(inf.Method)
}
}
conv.releaseMemory()
return nil
}
// processTypeLinks is evaluating all types used
// by interfaces
func (conv *Convert) processTypeLinks() {
for _, q := range conv.Interface {
q.link(conv, make(inuseLogic))
}
for _, q := range conv.Dictionary {
q.link(conv, make(inuseLogic))
}
}
func (conv *Convert) processPartialAndMixin() {
for _, pd := range conv.partialDict {
if candidate, f := conv.Types[pd.key()]; f {
if parent, ok := candidate.(*Dictionary); ok {
parent.merge(pd, conv)
} else {
conv.failing(pd, "trying to add partial dictionary to a non-dictionary type (%T)", candidate)
}
} else {
conv.failing(pd, "dictionary '%s' doesn't exist", pd.key())
}
}
for _, pd := range conv.partialIf {
if candidate, f := conv.Types[pd.key()]; f {
if parent, ok := candidate.(*Interface); ok {
parent.merge(pd, conv)
} else {
conv.failing(pd, "trying to add partial interface to a non-interface type (%T)", candidate)
}
} else if mixin, f := conv.mixin[pd.key()]; f {
mixin.mergeIf(pd, conv)
} else {
conv.failing(pd, "interface '%s' doesn't exist", pd.key())
}
}
for _, pd := range conv.partialMixin {
if parent, f := conv.mixin[pd.Name]; f {
parent.merge(pd, conv)
} else {
conv.failing(pd, "mixin '%s' doesn't exist", pd.Name)
}
}
for _, inc := range conv.includes {
target, found := conv.Types[inc.Name]
if !found {
conv.failing(inc, "include refernce to '%s' that doesn't exist", inc.Name)
continue
}
src, found := conv.mixin[inc.Source]
if !found {
conv.failing(inc, "include is references to '%s' that doesn't exist (or is not an mixin)", inc.Source)
continue
}
if doc, ok := target.(*Interface); ok {
doc.mergeMixin(src, conv)
} else {
conv.failing(inc, "target include existed to be an interface, not %T", target)
}
}
// exapand partial
// sort members
// evaluate inherits?
}
func (t *Convert) add(v Type) {
if v == nil {
return
}
name := v.key()
t.registerTypeName(v, name)
t.Types[name] = v
t.All = append(t.All, v)
}
func (t *Convert) addMixin(m *mixin) {
if m == nil {
return
}
t.registerTypeName(m, m.Name)
t.mixin[m.Name] = m
t.Merge[m.Name] = m
}
func (t *Convert) registerTypeName(ref GetRef, name string) {
if other, f := t.Types[name]; f {
t.failing(ref, "type '%s' already exist at %s", name, other.SourceReference())
return
}
if other, f := t.mixin[name]; f {
t.failing(ref, "type '%s' already exist at %s", name, other.SourceReference())
return
}
}
// Sort is sorting the Enum, Callbacks, Dictionary and Interface.
func (t *Convert) Sort() {
sort.Slice(t.Enums, func(i, j int) bool {
return t.Enums[i].lessThan(t.Enums[j])
})
sort.Slice(t.Callbacks, func(i, j int) bool {
return t.Callbacks[i].lessThan(t.Callbacks[j])
})
sort.Slice(t.Dictionary, func(i, j int) bool {
return t.Dictionary[i].lessThan(t.Dictionary[j])
})
sort.Slice(t.Interface, func(i, j int) bool {
return t.Interface[i].lessThan(t.Interface[j])
})
sort.Slice(t.Unions, func(i, j int) bool {
return t.Unions[i].lessThan(t.Unions[j])
})
}
// innerSort is sorting types according to source file and line.
// it only sort partial and mixin types
func (t *Convert) innerSort() {
sort.Slice(t.partialDict, func(i, j int) bool {
return t.partialDict[i].ref.sourceLessThan(t.partialDict[j].ref)
})
sort.Slice(t.partialIf, func(i, j int) bool {
return t.partialIf[i].ref.sourceLessThan(t.partialIf[j].ref)
})
sort.Slice(t.partialMixin, func(i, j int) bool {
return t.partialMixin[i].refs[0].sourceLessThan(t.partialMixin[j].refs[0])
})
sort.Slice(t.includes, func(i, j int) bool {
return t.includes[i].ref.sourceLessThan(t.includes[j].ref)
})
}
func (t *Convert) failing(ref GetRef, format string, args ...interface{}) {
t.setup.Error(ref, format, args...)
t.HaveError = true
}
func (t *Convert) warning(ref GetRef, format string, args ...interface{}) {
t.setup.Warning(ref, format, args...)
}
func (t *Convert) assertTrue(test bool, ref GetRef, format string, args ...interface{}) {
if !test {
t.failing(ref, format, args...)
}
}
func (t *Convert) warningTrue(test bool, ref GetRef, format string, args ...interface{}) {
if !test {
t.warning(ref, format, args...)
}
}
// releaseMemory is freeing up all internal values to save some memory
func (t *Convert) releaseMemory() {
t.partialDict = nil
t.partialIf = nil
t.mixin = nil
t.partialMixin = nil
t.includes = nil
t.setup = nil
}
type extractTypes struct {
ast.EmptyVisitor
main *Convert
protocol bytes.Buffer
lineOffset int
}
func (t *extractTypes) Enum(value *ast.Enum) bool {
// fmt.Println("evaluate enum ")
next := t.convertEnum(value)
t.main.Enums = append(t.main.Enums, next)
t.main.add(next)
return false
}
func (t *extractTypes) Interface(value *ast.Interface) bool {
// fmt.Println("evaluate interface")
next, partial := t.convertInterface(value)
if partial {
t.main.partialIf = append(t.main.partialIf, next)
} else {
t.main.Interface = append(t.main.Interface, next)
t.main.add(next)
}
return false
}
func (t *extractTypes) Mixin(value *ast.Mixin) bool {
// fmt.Println("evaluate mixim")
next, partial := t.convertMixin(value)
if partial {
t.main.partialMixin = append(t.main.partialMixin, next)
} else {
t.main.addMixin(next)
}
return false
}
func (t *extractTypes) Dictionary(value *ast.Dictionary) bool {
// fmt.Println("evaluate dirctionary")
next, partial := t.convertDictionary(value)
if partial {
t.main.partialDict = append(t.main.partialDict, next)
} else {
t.main.Dictionary = append(t.main.Dictionary, next)
t.main.add(next)
}
return false
}
func (t *extractTypes) Implementation(value *ast.Implementation) {
fmt.Println("evaluate implementation")
parser.Dump(os.Stdout, value)
panic("todo")
}
func (t *extractTypes) Includes(value *ast.Includes) {
// fmt.Println("evaluate includes")
next := t.convertIncludes(value)
t.main.includes = append(t.main.includes, next)
}
func (t *extractTypes) Callback(value *ast.Callback) bool {
// fmt.Println("evaluate callback")
cb := t.convertCallback(value)
t.main.Callbacks = append(t.main.Callbacks, cb)
t.main.add(cb)
return false
}
func (t *extractTypes) Typedef(value *ast.Typedef) bool {
// fmt.Println("evaluate typedef")
next := t.convertTypeDef(value)
t.main.add(next)
return false
}
func (t *extractTypes) failing(ref GetRef, format string, args ...interface{}) {
t.main.failing(ref, format, args...)
}
func (t *extractTypes) warning(ref GetRef, format string, args ...interface{}) {
t.main.warning(ref, format, args...)
}
func (t *extractTypes) assertTrue(test bool, ref GetRef, format string, args ...interface{}) {
t.main.assertTrue(test, ref, format, args...)
}
func (t *extractTypes) warningTrue(test bool, ref GetRef, format string, args ...interface{}) {
t.main.warningTrue(test, ref, format, args...)
}