-
Notifications
You must be signed in to change notification settings - Fork 59
/
base_lexer.go
216 lines (188 loc) · 3.41 KB
/
base_lexer.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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package sdp
import (
"errors"
"fmt"
"io"
"strconv"
)
var errDocumentStart = errors.New("already on document start")
type syntaxError struct {
s string
i int
}
func (e syntaxError) Error() string {
if e.i < 0 {
e.i = 0
}
return fmt.Sprintf("sdp: syntax error at pos %d: %s", e.i, strconv.QuoteToASCII(e.s[e.i:e.i+1]))
}
type baseLexer struct {
value string
pos int
}
func (l baseLexer) syntaxError() error {
return syntaxError{s: l.value, i: l.pos - 1}
}
func (l *baseLexer) unreadByte() error {
if l.pos <= 0 {
return errDocumentStart
}
l.pos--
return nil
}
func (l *baseLexer) readByte() (byte, error) {
if l.pos >= len(l.value) {
return byte(0), io.EOF
}
ch := l.value[l.pos]
l.pos++
return ch, nil
}
func (l *baseLexer) nextLine() error {
for {
ch, err := l.readByte()
if errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return err
}
if !isNewline(ch) {
return l.unreadByte()
}
}
}
func (l *baseLexer) readWhitespace() error {
for {
ch, err := l.readByte()
if errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return err
}
if !isWhitespace(ch) {
return l.unreadByte()
}
}
}
func (l *baseLexer) readUint64Field() (i uint64, err error) {
for {
ch, err := l.readByte()
if errors.Is(err, io.EOF) && i > 0 {
break
} else if err != nil {
return i, err
}
if isNewline(ch) {
if err := l.unreadByte(); err != nil {
return i, err
}
break
}
if isWhitespace(ch) {
if err := l.readWhitespace(); err != nil {
return i, err
}
break
}
switch ch {
case '0':
i *= 10
case '1':
i = i*10 + 1
case '2':
i = i*10 + 2
case '3':
i = i*10 + 3
case '4':
i = i*10 + 4
case '5':
i = i*10 + 5
case '6':
i = i*10 + 6
case '7':
i = i*10 + 7
case '8':
i = i*10 + 8
case '9':
i = i*10 + 9
default:
return i, l.syntaxError()
}
}
return i, nil
}
// Returns next field on this line or empty string if no more fields on line
func (l *baseLexer) readField() (string, error) {
start := l.pos
var stop int
for {
stop = l.pos
ch, err := l.readByte()
if errors.Is(err, io.EOF) && stop > start {
break
} else if err != nil {
return "", err
}
if isNewline(ch) {
if err := l.unreadByte(); err != nil {
return "", err
}
break
}
if isWhitespace(ch) {
if err := l.readWhitespace(); err != nil {
return "", err
}
break
}
}
return l.value[start:stop], nil
}
// Returns symbols until line end
func (l *baseLexer) readLine() (string, error) {
start := l.pos
trim := 1
for {
ch, err := l.readByte()
if err != nil {
return "", err
}
if ch == '\r' {
trim++
}
if ch == '\n' {
return l.value[start : l.pos-trim], nil
}
}
}
func (l *baseLexer) readType() (byte, error) {
for {
firstByte, err := l.readByte()
if err != nil {
return 0, err
}
if isNewline(firstByte) {
continue
}
secondByte, err := l.readByte()
if err != nil {
return 0, err
}
if secondByte != '=' {
return firstByte, l.syntaxError()
}
return firstByte, nil
}
}
func isNewline(ch byte) bool { return ch == '\n' || ch == '\r' }
func isWhitespace(ch byte) bool { return ch == ' ' || ch == '\t' }
func anyOf(element string, data ...string) bool {
for _, v := range data {
if element == v {
return true
}
}
return false
}