File: y.go

package info (click to toggle)
golang-github-malfunkt-iprange 0.9.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 116 kB
  • sloc: yacc: 86; makefile: 2
file content (557 lines) | stat: -rw-r--r-- 10,832 bytes parent folder | download | duplicates (2)
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//line ip.y:2
package iprange

import __yyfmt__ "fmt"

//line ip.y:3
import (
	"encoding/binary"
	"net"

	"github.com/pkg/errors"
)

type AddressRangeList []AddressRange

type AddressRange struct {
	Min net.IP
	Max net.IP
}

type octetRange struct {
	min byte
	max byte
}

//line ip.y:26
type ipSymType struct {
	yys       int
	num       byte
	octRange  octetRange
	addrRange AddressRange
	result    AddressRangeList
}

const num = 57346

var ipToknames = [...]string{
	"$end",
	"error",
	"$unk",
	"num",
	"','",
	"' '",
	"'/'",
	"'.'",
	"'*'",
	"'-'",
}
var ipStatenames = [...]string{}

const ipEofCode = 1
const ipErrCode = 2
const ipInitialStackSize = 16

//line ip.y:88

// ParseList takes a list of target specifications and returns a list of ranges,
// even if the list contains a single element.
func ParseList(in string) (AddressRangeList, error) {
	lex := &ipLex{line: []byte(in)}
	errCode := ipParse(lex)
	if errCode != 0 || lex.err != nil {
		return nil, errors.Wrap(lex.err, "could not parse target")
	}
	return lex.output, nil
}

// Parse takes a single target specification and returns a range. It effectively calls ParseList
// and returns the first result
func Parse(in string) (*AddressRange, error) {
	l, err := ParseList(in)
	if err != nil {
		return nil, err
	}
	return &l[0], nil
}

//line yacctab:1
var ipExca = [...]int{
	-1, 1,
	1, -1,
	-2, 0,
}

const ipNprod = 12
const ipPrivate = 57344

var ipTokenNames []string
var ipStates []string

const ipLast = 22

var ipAct = [...]int{

	4, 5, 12, 20, 2, 10, 6, 18, 11, 14,
	9, 17, 16, 13, 15, 8, 1, 7, 3, 19,
	0, 21,
}
var ipPact = [...]int{

	-3, 5, -1000, -2, 0, -8, -1000, -1000, -3, 3,
	10, -3, 7, -1000, -1000, -1000, -1, -1000, -3, -5,
	-3, -1000,
}
var ipPgo = [...]int{

	0, 18, 4, 0, 17, 16, 15,
}
var ipR1 = [...]int{

	0, 5, 5, 6, 6, 2, 2, 1, 3, 3,
	3, 4,
}
var ipR2 = [...]int{

	0, 1, 3, 1, 2, 3, 1, 7, 1, 1,
	1, 3,
}
var ipChk = [...]int{

	-1000, -5, -2, -1, -3, 4, 9, -4, -6, 5,
	7, 8, 10, -2, 6, 4, -3, 4, 8, -3,
	8, -3,
}
var ipDef = [...]int{

	0, -2, 1, 6, 0, 8, 9, 10, 0, 3,
	0, 0, 0, 2, 4, 5, 0, 11, 0, 0,
	0, 7,
}
var ipTok1 = [...]int{

	1, 3, 3, 3, 3, 3, 3, 3, 3, 3,
	3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
	3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
	3, 3, 6, 3, 3, 3, 3, 3, 3, 3,
	3, 3, 9, 3, 5, 10, 8, 7,
}
var ipTok2 = [...]int{

	2, 3, 4,
}
var ipTok3 = [...]int{
	0,
}

var ipErrorMessages = [...]struct {
	state int
	token int
	msg   string
}{}

//line yaccpar:1

/*	parser for yacc output	*/

var (
	ipDebug        = 0
	ipErrorVerbose = false
)

type ipLexer interface {
	Lex(lval *ipSymType) int
	Error(s string)
}

type ipParser interface {
	Parse(ipLexer) int
	Lookahead() int
}

type ipParserImpl struct {
	lval  ipSymType
	stack [ipInitialStackSize]ipSymType
	char  int
}

func (p *ipParserImpl) Lookahead() int {
	return p.char
}

func ipNewParser() ipParser {
	return &ipParserImpl{}
}

const ipFlag = -1000

func ipTokname(c int) string {
	if c >= 1 && c-1 < len(ipToknames) {
		if ipToknames[c-1] != "" {
			return ipToknames[c-1]
		}
	}
	return __yyfmt__.Sprintf("tok-%v", c)
}

func ipStatname(s int) string {
	if s >= 0 && s < len(ipStatenames) {
		if ipStatenames[s] != "" {
			return ipStatenames[s]
		}
	}
	return __yyfmt__.Sprintf("state-%v", s)
}

func ipErrorMessage(state, lookAhead int) string {
	const TOKSTART = 4

	if !ipErrorVerbose {
		return "syntax error"
	}

	for _, e := range ipErrorMessages {
		if e.state == state && e.token == lookAhead {
			return "syntax error: " + e.msg
		}
	}

	res := "syntax error: unexpected " + ipTokname(lookAhead)

	// To match Bison, suggest at most four expected tokens.
	expected := make([]int, 0, 4)

	// Look for shiftable tokens.
	base := ipPact[state]
	for tok := TOKSTART; tok-1 < len(ipToknames); tok++ {
		if n := base + tok; n >= 0 && n < ipLast && ipChk[ipAct[n]] == tok {
			if len(expected) == cap(expected) {
				return res
			}
			expected = append(expected, tok)
		}
	}

	if ipDef[state] == -2 {
		i := 0
		for ipExca[i] != -1 || ipExca[i+1] != state {
			i += 2
		}

		// Look for tokens that we accept or reduce.
		for i += 2; ipExca[i] >= 0; i += 2 {
			tok := ipExca[i]
			if tok < TOKSTART || ipExca[i+1] == 0 {
				continue
			}
			if len(expected) == cap(expected) {
				return res
			}
			expected = append(expected, tok)
		}

		// If the default action is to accept or reduce, give up.
		if ipExca[i+1] != 0 {
			return res
		}
	}

	for i, tok := range expected {
		if i == 0 {
			res += ", expecting "
		} else {
			res += " or "
		}
		res += ipTokname(tok)
	}
	return res
}

func iplex1(lex ipLexer, lval *ipSymType) (char, token int) {
	token = 0
	char = lex.Lex(lval)
	if char <= 0 {
		token = ipTok1[0]
		goto out
	}
	if char < len(ipTok1) {
		token = ipTok1[char]
		goto out
	}
	if char >= ipPrivate {
		if char < ipPrivate+len(ipTok2) {
			token = ipTok2[char-ipPrivate]
			goto out
		}
	}
	for i := 0; i < len(ipTok3); i += 2 {
		token = ipTok3[i+0]
		if token == char {
			token = ipTok3[i+1]
			goto out
		}
	}

out:
	if token == 0 {
		token = ipTok2[1] /* unknown char */
	}
	if ipDebug >= 3 {
		__yyfmt__.Printf("lex %s(%d)\n", ipTokname(token), uint(char))
	}
	return char, token
}

func ipParse(iplex ipLexer) int {
	return ipNewParser().Parse(iplex)
}

func (iprcvr *ipParserImpl) Parse(iplex ipLexer) int {
	var ipn int
	var ipVAL ipSymType
	var ipDollar []ipSymType
	_ = ipDollar // silence set and not used
	ipS := iprcvr.stack[:]

	Nerrs := 0   /* number of errors */
	Errflag := 0 /* error recovery flag */
	ipstate := 0
	iprcvr.char = -1
	iptoken := -1 // iprcvr.char translated into internal numbering
	defer func() {
		// Make sure we report no lookahead when not parsing.
		ipstate = -1
		iprcvr.char = -1
		iptoken = -1
	}()
	ipp := -1
	goto ipstack

ret0:
	return 0

ret1:
	return 1

ipstack:
	/* put a state and value onto the stack */
	if ipDebug >= 4 {
		__yyfmt__.Printf("char %v in %v\n", ipTokname(iptoken), ipStatname(ipstate))
	}

	ipp++
	if ipp >= len(ipS) {
		nyys := make([]ipSymType, len(ipS)*2)
		copy(nyys, ipS)
		ipS = nyys
	}
	ipS[ipp] = ipVAL
	ipS[ipp].yys = ipstate

ipnewstate:
	ipn = ipPact[ipstate]
	if ipn <= ipFlag {
		goto ipdefault /* simple state */
	}
	if iprcvr.char < 0 {
		iprcvr.char, iptoken = iplex1(iplex, &iprcvr.lval)
	}
	ipn += iptoken
	if ipn < 0 || ipn >= ipLast {
		goto ipdefault
	}
	ipn = ipAct[ipn]
	if ipChk[ipn] == iptoken { /* valid shift */
		iprcvr.char = -1
		iptoken = -1
		ipVAL = iprcvr.lval
		ipstate = ipn
		if Errflag > 0 {
			Errflag--
		}
		goto ipstack
	}

ipdefault:
	/* default state action */
	ipn = ipDef[ipstate]
	if ipn == -2 {
		if iprcvr.char < 0 {
			iprcvr.char, iptoken = iplex1(iplex, &iprcvr.lval)
		}

		/* look through exception table */
		xi := 0
		for {
			if ipExca[xi+0] == -1 && ipExca[xi+1] == ipstate {
				break
			}
			xi += 2
		}
		for xi += 2; ; xi += 2 {
			ipn = ipExca[xi+0]
			if ipn < 0 || ipn == iptoken {
				break
			}
		}
		ipn = ipExca[xi+1]
		if ipn < 0 {
			goto ret0
		}
	}
	if ipn == 0 {
		/* error ... attempt to resume parsing */
		switch Errflag {
		case 0: /* brand new error */
			iplex.Error(ipErrorMessage(ipstate, iptoken))
			Nerrs++
			if ipDebug >= 1 {
				__yyfmt__.Printf("%s", ipStatname(ipstate))
				__yyfmt__.Printf(" saw %s\n", ipTokname(iptoken))
			}
			fallthrough

		case 1, 2: /* incompletely recovered error ... try again */
			Errflag = 3

			/* find a state where "error" is a legal shift action */
			for ipp >= 0 {
				ipn = ipPact[ipS[ipp].yys] + ipErrCode
				if ipn >= 0 && ipn < ipLast {
					ipstate = ipAct[ipn] /* simulate a shift of "error" */
					if ipChk[ipstate] == ipErrCode {
						goto ipstack
					}
				}

				/* the current p has no shift on "error", pop stack */
				if ipDebug >= 2 {
					__yyfmt__.Printf("error recovery pops state %d\n", ipS[ipp].yys)
				}
				ipp--
			}
			/* there is no state on the stack with an error shift ... abort */
			goto ret1

		case 3: /* no shift yet; clobber input char */
			if ipDebug >= 2 {
				__yyfmt__.Printf("error recovery discards %s\n", ipTokname(iptoken))
			}
			if iptoken == ipEofCode {
				goto ret1
			}
			iprcvr.char = -1
			iptoken = -1
			goto ipnewstate /* try again in the same state */
		}
	}

	/* reduction by production ipn */
	if ipDebug >= 2 {
		__yyfmt__.Printf("reduce %v in:\n\t%v\n", ipn, ipStatname(ipstate))
	}

	ipnt := ipn
	ippt := ipp
	_ = ippt // guard against "declared and not used"

	ipp -= ipR2[ipn]
	// ipp is now the index of $0. Perform the default action. Iff the
	// reduced production is ε, $1 is possibly out of range.
	if ipp+1 >= len(ipS) {
		nyys := make([]ipSymType, len(ipS)*2)
		copy(nyys, ipS)
		ipS = nyys
	}
	ipVAL = ipS[ipp+1]

	/* consult goto table to find next state */
	ipn = ipR1[ipn]
	ipg := ipPgo[ipn]
	ipj := ipg + ipS[ipp].yys + 1

	if ipj >= ipLast {
		ipstate = ipAct[ipg]
	} else {
		ipstate = ipAct[ipj]
		if ipChk[ipstate] != -ipn {
			ipstate = ipAct[ipg]
		}
	}
	// dummy call; replaced with literal code
	switch ipnt {

	case 1:
		ipDollar = ipS[ippt-1 : ippt+1]
		//line ip.y:41
		{
			ipVAL.result = append(ipVAL.result, ipDollar[1].addrRange)
			iplex.(*ipLex).output = ipVAL.result
		}
	case 2:
		ipDollar = ipS[ippt-3 : ippt+1]
		//line ip.y:46
		{
			ipVAL.result = append(ipDollar[1].result, ipDollar[3].addrRange)
			iplex.(*ipLex).output = ipVAL.result
		}
	case 5:
		ipDollar = ipS[ippt-3 : ippt+1]
		//line ip.y:54
		{
			mask := net.CIDRMask(int(ipDollar[3].num), 32)
			min := ipDollar[1].addrRange.Min.Mask(mask)
			maxInt := binary.BigEndian.Uint32([]byte(min)) +
				0xffffffff -
				binary.BigEndian.Uint32([]byte(mask))
			maxBytes := make([]byte, 4)
			binary.BigEndian.PutUint32(maxBytes, maxInt)
			maxBytes = maxBytes[len(maxBytes)-4:]
			max := net.IP(maxBytes)
			ipVAL.addrRange = AddressRange{
				Min: min.To4(),
				Max: max.To4(),
			}
		}
	case 6:
		ipDollar = ipS[ippt-1 : ippt+1]
		//line ip.y:70
		{
			ipVAL.addrRange = ipDollar[1].addrRange
		}
	case 7:
		ipDollar = ipS[ippt-7 : ippt+1]
		//line ip.y:75
		{
			ipVAL.addrRange = AddressRange{
				Min: net.IPv4(ipDollar[1].octRange.min, ipDollar[3].octRange.min, ipDollar[5].octRange.min, ipDollar[7].octRange.min).To4(),
				Max: net.IPv4(ipDollar[1].octRange.max, ipDollar[3].octRange.max, ipDollar[5].octRange.max, ipDollar[7].octRange.max).To4(),
			}
		}
	case 8:
		ipDollar = ipS[ippt-1 : ippt+1]
		//line ip.y:82
		{
			ipVAL.octRange = octetRange{ipDollar[1].num, ipDollar[1].num}
		}
	case 9:
		ipDollar = ipS[ippt-1 : ippt+1]
		//line ip.y:83
		{
			ipVAL.octRange = octetRange{0, 255}
		}
	case 10:
		ipDollar = ipS[ippt-1 : ippt+1]
		//line ip.y:84
		{
			ipVAL.octRange = ipDollar[1].octRange
		}
	case 11:
		ipDollar = ipS[ippt-3 : ippt+1]
		//line ip.y:86
		{
			ipVAL.octRange = octetRange{ipDollar[1].num, ipDollar[3].num}
		}
	}
	goto ipstack /* stack new state and value */
}