-
Notifications
You must be signed in to change notification settings - Fork 0
/
abnf.go
245 lines (212 loc) · 6.23 KB
/
abnf.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
package goabnf
import (
"fmt"
"strconv"
"strings"
)
// ElemItf defines the interface of all the element alternations:
// - ElemRulename
// - ElemGroup
// - ElemOption
// - ElemProseVal
// - ElemNumVal
// - ElemCharVal
//
// This is exposed for custom lexing purposes, please don't use it else.
type ElemItf interface {
fmt.Stringer
regex(*Grammar) (string, error)
}
// Rule represents an ABNF rule, with its name and underlying alternation.
//
// This is exposed for custom lexing purposes, please don't use it else.
type Rule struct {
// Name is the unique rule name.
// Notice it is case-insensitive according to RFC 5234 Section 2.1.
Name string
Alternation Alternation
}
func (rl Rule) String() string {
return fmt.Sprintf("%s = %s", rl.Name, rl.Alternation)
}
// Alternation represents an ABNF alternation object.
//
// This is exposed for custom lexing purposes, please don't use it else.
type Alternation struct {
// Concatenations contains the variants that validate the upper
// object (i.e. rule, group or option).
Concatenations []Concatenation
}
func (alt Alternation) String() string {
str := ""
for _, concat := range alt.Concatenations {
str += fmt.Sprintf("%s / ", concat)
}
return strings.TrimSuffix(str, " / ")
}
// Concatenation represents an ABNF concatenation object.
//
// This is exposed for custom lexing purposes, please don't use it else.
type Concatenation struct {
// Repetitions contains the following repetitions, order matter.
Repetitions []Repetition
}
func (cnt Concatenation) String() string {
str := ""
for _, rep := range cnt.Repetitions {
str += fmt.Sprintf("%s ", rep)
}
return strings.TrimSuffix(str, " ")
}
// Repetition represents an ABNF repetition object.
//
// This is exposed for custom lexing purposes, please don't use it else.
type Repetition struct {
Min, Max int
Element ElemItf
}
func (rep Repetition) String() string {
if rep.Min == rep.Max {
if rep.Min == 1 {
return rep.Element.String()
}
return strconv.Itoa(rep.Min) + rep.Element.String()
}
str := ""
if rep.Min != 0 {
str += strconv.Itoa(rep.Min)
}
str += "*"
if rep.Max != inf {
str += strconv.Itoa(rep.Max)
}
return str + rep.Element.String()
}
// ElemRulename represents an ABNF rulename object.
//
// This is exposed for custom lexing purposes, please don't use it else.
type ElemRulename struct {
// Name is the unique rule name.
// Notice it is case-insensitive according to RFC 5234 Section 2.1.
Name string
}
func (erln ElemRulename) String() string {
return erln.Name
}
var _ ElemItf = (*ElemRulename)(nil)
// ElemGroup represents an ABNF group object.
// It will be straightly passed through with its underlying alternation.
//
// This is exposed for custom lexing purposes, please don't use it else.
type ElemGroup struct {
Alternation Alternation
}
func (egrp ElemGroup) String() string {
return "(" + egrp.Alternation.String() + ")"
}
var _ ElemItf = (*ElemGroup)(nil)
// ElemOption represents an ABNF option object.
// It will be straightly converted to a 0*1 repetition.
//
// This is exposed for custom lexing purposes, please don't use it else.
type ElemOption struct {
Alternation Alternation
}
func (eopt ElemOption) String() string {
return "[" + eopt.Alternation.String() + "]"
}
var _ ElemItf = (*ElemOption)(nil)
// ElemCharVal represents an ABNF char-val object.
//
// This is exposed for custom lexing purposes, please don't use it else.
type ElemCharVal struct {
// Sensitive is by default false, support clarrified by RFC 7405.
Sensitive bool
Values []byte
}
func (ecvl ElemCharVal) String() string {
str := ""
for _, val := range ecvl.Values {
str += string(val)
}
return `"` + str + `"`
}
var _ ElemItf = (*ElemCharVal)(nil)
// ElemNumVal represents an ABNF num-val object.
//
// This is exposed for custom lexing purposes, please don't use it else.
type ElemNumVal struct {
Base string
// Status could be:
// - `statSeries`: `elems` contains all the expected
// values in the order of the grammar defined them ;
// - `statRange`: `elems` contains the start and end
// bounds (so no more than two).
Status Status
Elems []string
}
func (envl ElemNumVal) String() string {
str := "%" + envl.Base
spl := "."
if envl.Status == StatRange {
spl = "-"
}
for _, val := range envl.Elems {
str += val + spl
}
return strings.TrimSuffix(str, spl)
}
var _ ElemItf = (*ElemNumVal)(nil)
// ElemProseVal represents an ABNF prose-val object.
//
// This is exposed for custom lexing purposes, please don't use it else.
type ElemProseVal struct {
values []string
}
func (epvl ElemProseVal) String() string {
str := ""
for _, val := range epvl.values {
str += val
}
return "<" + str + ">"
}
var _ ElemItf = (*ElemProseVal)(nil)
// Status defines the type of a num-val, rather StatSeries or StatRange.
//
// This is exposed for custom lexing purposes, please don't use it else.
type Status int
const (
// StatSeries represents a serie of unique byte value.
StatSeries Status = iota
// StatRange represents an interval of possible bytes.
StatRange
)
// ABNF is the manually parsed+lexed+validated ABNF grammar.
var ABNF = &Grammar{
Rulemap: map[string]*Rule{
abnfRulelist.Name: abnfRulelist,
abnfRule.Name: abnfRule,
abnfRulename.Name: abnfRulename,
abnfDefinedAs.Name: abnfDefinedAs,
abnfElements.Name: abnfElements,
abnfCWsp.Name: abnfCWsp,
abnfCNl.Name: abnfCNl,
abnfComment.Name: abnfComment,
abnfAlternation.Name: abnfAlternation,
abnfConcatenation.Name: abnfConcatenation,
abnfRepetition.Name: abnfRepetition,
abnfRepeat.Name: abnfRepeat,
abnfElement.Name: abnfElement,
abnfGroup.Name: abnfGroup,
abnfOption.Name: abnfOption,
abnfCharVal.Name: abnfCharVal,
abnfCaseInsensitiveString.Name: abnfCaseInsensitiveString,
abnfCaseSensitiveString.Name: abnfCaseSensitiveString,
abnfQuotedString.Name: abnfQuotedString,
abnfNumVal.Name: abnfNumVal,
abnfBinVal.Name: abnfBinVal,
abnfDecVal.Name: abnfDecVal,
abnfHexVal.Name: abnfHexVal,
abnfProseVal.Name: abnfProseVal,
},
}