aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/varlink/go/varlink/idl/idl.go
blob: 27f21a759658d7e0c84da9a3d0c0b13999e4aabb (plain)
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Package idl provides a varlink interface description parser.
package idl

import (
	"bytes"
	"fmt"
	"regexp"
)

// Valid TypeKind values.
const (
	TypeBool = iota
	TypeInt
	TypeFloat
	TypeString
	TypeArray
	TypeMaybe
	TypeStruct
	TypeEnum
	TypeAlias
)

// TypeKind specifies the type of an Type.
type TypeKind uint

// Type represents a varlink type. Types are method input and output parameters,
// error output parameters, or custom defined types in the interface description.
type Type struct {
	Kind        TypeKind
	ElementType *Type
	Alias       string
	Fields      []TypeField
}

// TypeField is a named member of a TypeStruct.
type TypeField struct {
	Name string
	Type *Type
}

// Alias represents a named Type in the interface description.
type Alias struct {
	Name string
	Doc  string
	Type *Type
}

// Method represents a method defined in the interface description.
type Method struct {
	Name string
	Doc  string
	In   *Type
	Out  *Type
}

// Error represents an error defined in the interface description.
type Error struct {
	Name string
	Type *Type
}

// IDL represents a parsed varlink interface description with types, methods, errors and
// documentation.
type IDL struct {
	Name        string
	Doc         string
	Description string
	Members     []interface{}
	Aliases     map[string]*Alias
	Methods     map[string]*Method
	Errors      map[string]*Error
}

type parser struct {
	input       string
	position    int
	lineStart   int
	lastComment bytes.Buffer
}

func (p *parser) next() int {
	r := -1

	if p.position < len(p.input) {
		r = int(p.input[p.position])
	}

	p.position++
	return r
}

func (p *parser) backup() {
	p.position--
}

func (p *parser) advance() bool {
	for {
		char := p.next()

		if char == '\n' {
			p.lineStart = p.position
			p.lastComment.Reset()

		} else if char == ' ' || char == '\t' {
			// ignore

		} else if char == '#' {
			p.next()
			start := p.position
			for {
				c := p.next()
				if c < 0 || c == '\n' {
					p.backup()
					break
				}
			}
			if p.lastComment.Len() > 0 {
				p.lastComment.WriteByte('\n')
			}
			p.lastComment.WriteString(p.input[start:p.position])
			p.next()

		} else {
			p.backup()
			break
		}
	}

	return p.position < len(p.input)
}

func (p *parser) advanceOnLine() {
	for {
		char := p.next()
		if char != ' ' {
			p.backup()
			return
		}
	}
}

func (p *parser) readKeyword() string {
	start := p.position

	for {
		char := p.next()
		if char < 'a' || char > 'z' {
			p.backup()
			break
		}
	}

	return p.input[start:p.position]
}

func (p *parser) readInterfaceName() string {
	start := p.position
	dnrx := regexp.MustCompile(`^[a-z]+(\.[a-z0-9]+([-][a-z0-9]+)*)+`)
	name := dnrx.FindString(p.input[start:])
	if name != "" {
		if len(name) > 255 {
			return ""
		}
		p.position += len(name)
		return name
	}
	xdnrx := regexp.MustCompile(`^xn--[a-z0-9]+(\.[a-z0-9]+([-][a-z0-9]+)*)+`)
	name = xdnrx.FindString(p.input[start:])
	if name != "" {
		if len(name) > 255 {
			return ""
		}
		p.position += len(name)
		return name
	}
	return ""
}

func (p *parser) readFieldName() string {
	start := p.position

	char := p.next()
	if char < 'a' || char > 'z' {
		p.backup()
		return ""
	}

	for {
		char := p.next()
		if (char < 'A' || char > 'Z') && (char < 'a' || char > 'z') && (char < '0' || char > '9') && char != '_' {
			p.backup()
			break
		}
	}

	return p.input[start:p.position]
}

func (p *parser) readTypeName() string {
	start := p.position

	for {
		char := p.next()
		if (char < 'A' || char > 'Z') && (char < 'a' || char > 'z') && (char < '0' || char > '9') {
			p.backup()
			break
		}
	}

	return p.input[start:p.position]
}

func (p *parser) readStructType() *Type {
	if p.next() != '(' {
		p.backup()
		return nil
	}

	t := &Type{Kind: TypeStruct}
	t.Fields = make([]TypeField, 0)

	char := p.next()
	if char != ')' {
		p.backup()

		for {
			field := TypeField{}

			p.advance()
			field.Name = p.readFieldName()
			if field.Name == "" {
				return nil
			}

			p.advance()

			// Enums have no types, they are just a list of names
			if p.next() == ':' {
				if t.Kind == TypeEnum {
					return nil
				}

				p.advance()
				field.Type = p.readType()
				if field.Type == nil {
					return nil
				}

			} else {
				t.Kind = TypeEnum
				p.backup()
			}

			t.Fields = append(t.Fields, field)

			p.advance()
			char = p.next()
			if char != ',' {
				break
			}
		}

		if char != ')' {
			return nil
		}
	}

	return t
}

func (p *parser) readType() *Type {
	var t *Type

	switch p.next() {
	case '?':
		e := p.readType()
		if e == nil {
			return nil
		}
		t = &Type{Kind: TypeMaybe, ElementType: e}

	case '[':
		if p.next() != ']' {
			return nil
		}
		e := p.readType()
		if e == nil {
			return nil
		}
		t = &Type{Kind: TypeArray, ElementType: e}

	default:
		p.backup()
		if keyword := p.readKeyword(); keyword != "" {
			switch keyword {
			case "bool":
				t = &Type{Kind: TypeBool}

			case "int":
				t = &Type{Kind: TypeInt}

			case "float":
				t = &Type{Kind: TypeFloat}

			case "string":
				t = &Type{Kind: TypeString}
			}

		} else if name := p.readTypeName(); name != "" {
			t = &Type{Kind: TypeAlias, Alias: name}

		} else if t = p.readStructType(); t == nil {
			return nil
		}
	}

	return t
}

func (p *parser) readAlias(idl *IDL) (*Alias, error) {
	a := &Alias{}

	p.advance()
	a.Doc = p.lastComment.String()
	a.Name = p.readTypeName()
	if a.Name == "" {
		return nil, fmt.Errorf("missing type name")
	}

	p.advance()
	a.Type = p.readType()
	if a.Type == nil {
		return nil, fmt.Errorf("missing type declaration")
	}

	return a, nil
}

func (p *parser) readMethod(idl *IDL) (*Method, error) {
	m := &Method{}

	p.advance()
	m.Doc = p.lastComment.String()
	m.Name = p.readTypeName()
	if m.Name == "" {
		return nil, fmt.Errorf("missing method type")
	}

	p.advance()
	m.In = p.readType()
	if m.In == nil {
		return nil, fmt.Errorf("missing method input")
	}

	p.advance()
	one := p.next()
	two := p.next()
	if (one != '-') || two != '>' {
		return nil, fmt.Errorf("missing method '->' operator")
	}

	p.advance()
	m.Out = p.readType()
	if m.Out == nil {
		return nil, fmt.Errorf("missing method output")
	}

	return m, nil
}

func (p *parser) readError(idl *IDL) (*Error, error) {
	e := &Error{}

	p.advance()
	e.Name = p.readTypeName()
	if e.Name == "" {
		return nil, fmt.Errorf("missing error name")
	}

	p.advanceOnLine()
	e.Type = p.readType()

	return e, nil
}

func (p *parser) readIDL() (*IDL, error) {
	if keyword := p.readKeyword(); keyword != "interface" {
		return nil, fmt.Errorf("missing interface keyword")
	}

	idl := &IDL{
		Members: make([]interface{}, 0),
		Aliases: make(map[string]*Alias),
		Methods: make(map[string]*Method),
		Errors:  make(map[string]*Error),
	}

	p.advance()
	idl.Doc = p.lastComment.String()
	idl.Name = p.readInterfaceName()
	if idl.Name == "" {
		return nil, fmt.Errorf("interface name")
	}

	for {
		if !p.advance() {
			break
		}

		switch keyword := p.readKeyword(); keyword {
		case "type":
			a, err := p.readAlias(idl)
			if err != nil {
				return nil, err
			}

			idl.Members = append(idl.Members, a)
			idl.Aliases[a.Name] = a

		case "method":
			m, err := p.readMethod(idl)
			if err != nil {
				return nil, err
			}

			idl.Members = append(idl.Members, m)
			if _, ok := idl.Methods[m.Name]; ok {
				return nil, fmt.Errorf("method `%s` already defined", m.Name)
			}
			idl.Methods[m.Name] = m

		case "error":
			e, err := p.readError(idl)
			if err != nil {
				return nil, err
			}

			idl.Members = append(idl.Members, e)
			idl.Errors[e.Name] = e

		default:
			return nil, fmt.Errorf("unknown keyword '%s'", keyword)
		}
	}

	return idl, nil
}

// New parses a varlink interface description.
func New(description string) (*IDL, error) {
	p := &parser{input: description}

	p.advance()
	idl, err := p.readIDL()
	if err != nil {
		return nil, err
	}

	if len(idl.Methods) == 0 {
		return nil, fmt.Errorf("no methods defined")
	}

	idl.Description = description
	return idl, nil
}