This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds.go
172 lines (139 loc) · 3.61 KB
/
ds.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
package antlr4512
/*
DITStructureRule implements RFC 4512 Section 4.1.7.1.
*/
type DITStructureRule struct {
ID string
Name []string
Desc string
Obsolete bool
Form string
SuperRules []string
Extensions map[string][]string
}
/*
DITStructureRules is an instance of slices of [DITStructureRule] instances.
*/
type DITStructureRules []DITStructureRule
/*
ParseDITStructureRule processes raw into an instance of [DITStructureRule],
which is returned alongside an error.
*/
func ParseDITStructureRule(raw string) (ds DITStructureRule, err error) {
var i Instance
if i, err = ParseInstance(raw); err != nil {
return
}
// Ensure what went in matches precisely what came out
x := i.P.DITStructureRuleDescription()
if err = ds.process(x); err == nil {
ptext := x.GetText()
if raw != ptext {
err = errorf("Inconsistent %T parse results or bad content", ds)
}
}
return
}
func (r *DITStructureRule) process(ctx IDITStructureRuleDescriptionContext) (err error) {
if err = parenContext(ctx.OpenParen(), ctx.CloseParen()); err != nil {
return
}
for k, ct := 0, ctx.GetChildCount(); k < ct && err == nil; k++ {
switch tv := ctx.GetChild(k).(type) {
case *StructureRuleContext:
err = r.setCritical(tv)
case *NameContext,
*ExtensionsContext,
*DescriptionContext:
err = r.setMisc(tv)
case *ObsolescenceContext:
r.Obsolete = true
case *FormContext:
if o := tv.OID(); o != nil {
r.Form = o.GetText()
}
case *SuperRulesContext:
err = r.superRulesContext(tv)
default:
err = isErrImpl(`dITStructureRule`, r.ID, tv)
}
}
if len(r.Form) == 0 {
err = errorf("No nameForm reference set for %T", r)
}
return
}
func (r *DITStructureRule) setMisc(ctx any) (err error) {
switch tv := ctx.(type) {
case *NameContext:
r.Name, err = nameContext(tv.Names())
case *DescriptionContext:
r.Desc, err = descContext(tv)
case *ExtensionsContext:
r.Extensions, err = extContext(tv)
default:
err = errorf("Unknown miscellaneous context '%T'", ctx)
}
return
}
func (r *DITStructureRule) setCritical(ctx any) (err error) {
switch tv := ctx.(type) {
case *StructureRuleContext:
if m, err := ruleIDContext(tv); err == nil {
r.ID = m
}
default:
err = errorf("Unknown critical context '%T'", ctx)
}
return
}
func (r *DITStructureRule) superRulesContext(ctx *SuperRulesContext) (err error) {
if ctx != nil {
var sids []string
if x := ctx.StructureRule(); x != nil {
var sid string
sid, err = ruleIDContext(x.(*StructureRuleContext))
if err == nil {
sids = append(sids, sid)
}
} else if y := ctx.StructureRules(); y != nil {
sids, err = ruleIDsContext(y.(*StructureRulesContext))
}
if len(sids) > 0 {
r.SuperRules = sids
}
}
return
}
// Use of single-valued IDs applies to attributeType, dITStructureRule
// and nameForm definitions.
func ruleIDContext(ctx *StructureRuleContext) (id string, err error) {
dig := ctx.Number()
if dig == nil {
err = errorf("%T.%T is nil", ctx, dig)
return
}
id = trimS(trim(dig.GetText(), `''`))
return
}
// Use of multi-valued IDs applies solely to subordinate dITStructureRules
// in reference to their respective superior rules.
func ruleIDsContext(ctx *StructureRulesContext) (sids []string, err error) {
ch := ctx.AllStructureRule()
for i := 0; i < len(ch) && err == nil; i++ {
assert, ok := ch[i].(*StructureRuleContext)
if !ok {
err = errorf("%T type assertion failed for %T", ch[i], assert)
break
}
var sid string
if sid, err = ruleIDContext(assert); err != nil {
break
}
sids = append(sids, sid)
}
if len(sids) == 0 {
err = errorf("No rule IDs parsed")
}
return
}