1// Copyright (c) 2017 Ernest Micklei
2//
3// MIT License
4//
5// Permission is hereby granted, free of charge, to any person obtaining
6// a copy of this software and associated documentation files (the
7// "Software"), to deal in the Software without restriction, including
8// without limitation the rights to use, copy, modify, merge, publish,
9// distribute, sublicense, and/or sell copies of the Software, and to
10// permit persons to whom the Software is furnished to do so, subject to
11// the following conditions:
12//
13// The above copyright notice and this permission notice shall be
14// included in all copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24package proto
25
26import (
27	"fmt"
28	"strconv"
29)
30
31// Range is to specify number intervals (with special end value "max")
32type Range struct {
33	From, To int
34	Max      bool
35}
36
37// String return a single number if from = to. Returns <from> to <to> otherwise unless Max then return <from> to max.
38func (r Range) String() string {
39	if r.Max {
40		return fmt.Sprintf("%d to max", r.From)
41	}
42	if r.From == r.To {
43		return strconv.Itoa(r.From)
44	}
45	return fmt.Sprintf("%d to %d", r.From, r.To)
46}
47
48// parseRanges is used to parse ranges for extensions and reserved
49func parseRanges(p *Parser, n Visitee) (list []Range, err error) {
50	seenTo := false
51	for {
52		pos, tok, lit := p.next()
53		if isString(lit) {
54			return list, p.unexpected(lit, "integer, <to> <max>", n)
55		}
56		switch lit {
57		case ",":
58		case "to":
59			seenTo = true
60		case ";":
61			p.nextPut(pos, tok, lit) // allow for inline comment parsing
62			goto done
63		case "max":
64			if !seenTo {
65				return list, p.unexpected(lit, "to", n)
66			}
67			from := list[len(list)-1]
68			list = append(list[0:len(list)-1], Range{From: from.From, Max: true})
69		default:
70			// must be number
71			i, err := strconv.Atoi(lit)
72			if err != nil {
73				return list, p.unexpected(lit, "range integer", n)
74			}
75			if seenTo {
76				// replace last two ranges with one
77				if len(list) < 1 {
78					p.unexpected(lit, "integer", n)
79				}
80				from := list[len(list)-1]
81				list = append(list[0:len(list)-1], Range{From: from.From, To: i})
82				seenTo = false
83			} else {
84				list = append(list, Range{From: i, To: i})
85			}
86		}
87	}
88done:
89	return
90}
91