@@ -33,33 +33,33 @@ func FromParserNode(g parser.Grammar, e parser.TreeElement) Branch {
33
33
return result .collapse (0 ).(Branch )
34
34
}
35
35
36
- func (n Branch ) collapse (level int ) Node {
36
+ func (b Branch ) collapse (level int ) Node {
37
37
if false && level > 0 {
38
- switch oneChild := n .oneChild ().(type ) {
38
+ switch oneChild := b .oneChild ().(type ) {
39
39
case Branch :
40
40
oneBranch := oneChild
41
41
oneBranch .inc (SkipTag )
42
- if choice , has := n [ChoiceTag ]; has {
42
+ if choice , has := b [ChoiceTag ]; has {
43
43
if oChoice , has := oneBranch [ChoiceTag ]; has {
44
44
oneBranch [ChoiceTag ] = append (choice .(Many ), oChoice .(Many )... )
45
45
} else {
46
46
oneBranch [ChoiceTag ] = choice
47
47
}
48
48
}
49
- if rule , has := n [RuleTag ]; has {
49
+ if rule , has := b [RuleTag ]; has {
50
50
oneBranch [RuleTag ] = rule
51
51
}
52
52
return oneBranch
53
53
// case Leaf:
54
54
// return oneChild
55
55
}
56
56
}
57
- return n
57
+ return b
58
58
}
59
59
60
- func (n Branch ) oneChild () Node {
60
+ func (b Branch ) oneChild () Node {
61
61
var oneChildren Children
62
- for childrenName , children := range n {
62
+ for childrenName , children := range b {
63
63
if ! strings .HasPrefix (childrenName , "@" ) {
64
64
if oneChildren != nil {
65
65
return nil
@@ -80,135 +80,132 @@ func (n Branch) oneChild() Node {
80
80
return nil
81
81
}
82
82
83
- func (n Branch ) inc (name string ) int {
83
+ func (b Branch ) inc (name string ) int {
84
84
i := 0
85
- if child , has := n [name ]; has {
85
+ if child , has := b [name ]; has {
86
86
i = child .(One ).Node .(Extra ).Data .(int )
87
87
}
88
- n [name ] = One {Node : Extra {Data : i + 1 }}
88
+ b [name ] = One {Node : Extra {Data : i + 1 }}
89
89
return i
90
90
}
91
91
92
- func (n Branch ) add (name string , node Node , ctr counter ) {
92
+ func (b Branch ) add (name string , node Node , ctr counter ) {
93
93
switch ctr {
94
94
case counter {}:
95
95
panic (errors .Inconceivable )
96
96
case zeroOrOne , oneOne :
97
- n .one (name , node )
97
+ b .one (name , node )
98
98
default :
99
- n .many (name , node )
99
+ b .many (name , node )
100
100
}
101
101
}
102
102
103
- func (n Branch ) one (name string , node Node ) {
104
- if _ , has := n [name ]; has {
103
+ func (b Branch ) one (name string , node Node ) {
104
+ if _ , has := b [name ]; has {
105
105
panic (errors .Inconceivable )
106
106
}
107
- n [name ] = One {Node : node }
107
+ b [name ] = One {Node : node }
108
108
}
109
109
110
- func (n Branch ) many (name string , node Node ) {
111
- if many , has := n [name ]; has {
112
- n [name ] = append (many .(Many ), node )
110
+ func (b Branch ) many (name string , node Node ) {
111
+ if many , has := b [name ]; has {
112
+ b [name ] = append (many .(Many ), node )
113
113
} else {
114
- n [name ] = Many ([]Node {node })
114
+ b [name ] = Many ([]Node {node })
115
115
}
116
116
}
117
117
118
- func (n Branch ) fromParserNode (g parser.Grammar , term parser.Term , ctrs counters , e parser.TreeElement ) {
118
+ func (b Branch ) fromParserNode (g parser.Grammar , term parser.Term , ctrs counters , e parser.TreeElement ) {
119
119
var tag string
120
120
// defer enterf("fromParserNode(term=%T(%[1]v), ctrs=%v, v=%v)", term, ctrs, e).exitf("tag=%q, n=%v", &tag, &n)
121
121
switch t := term .(type ) {
122
122
case parser.S , parser.RE :
123
- n .add ("" , Leaf (e .(parser.Scanner )), ctrs ["" ])
123
+ b .add ("" , Leaf (e .(parser.Scanner )), ctrs ["" ])
124
124
case parser.Rule :
125
125
term := g [t ]
126
126
childCtrs := newCounters (term )
127
- b := Branch {}
127
+ b2 := Branch {}
128
128
unleveled , level := unlevel (string (t ), g )
129
- b .fromParserNode (g , term , childCtrs , e )
130
- var node Node = b
129
+ b2 .fromParserNode (g , term , childCtrs , e )
130
+ var node Node = b2
131
131
// if name := childCtrs.singular(); name != nil {
132
- // node = b [*name].(One).Node
132
+ // node = b2 [*name].(One).Node
133
133
// // TODO: zeroOrOne
134
134
// }
135
135
node = node .collapse (level )
136
- n .add (unleveled , node , ctrs [string (t )])
136
+ b .add (unleveled , node , ctrs [string (t )])
137
137
case parser.ScopedGrammar :
138
138
gcopy := g
139
139
for rule , terms := range t .Grammar {
140
140
gcopy [rule ] = terms
141
141
}
142
- n .fromParserNode (gcopy , t .Term , ctrs , e )
142
+ b .fromParserNode (gcopy , t .Term , ctrs , e )
143
143
case parser.Seq :
144
144
node := e .(parser.Node )
145
- tag = node .Tag
146
145
for i , child := range node .Children {
147
- n .fromParserNode (g , t [i ], ctrs , child )
146
+ b .fromParserNode (g , t [i ], ctrs , child )
148
147
}
149
148
case parser.Oneof :
150
149
node := e .(parser.Node )
151
- tag = node .Tag
152
- n .many (ChoiceTag , Extra {Data : node .Extra .(parser.Choice )})
153
- n .fromParserNode (g , t [node .Extra .(parser.Choice )], ctrs , node .Children [0 ])
150
+ b .many (ChoiceTag , Extra {Data : node .Extra .(parser.Choice )})
151
+ b .fromParserNode (g , t [node .Extra .(parser.Choice )], ctrs , node .Children [0 ])
154
152
case parser.Delim :
155
153
node := e .(parser.Node )
156
154
tag = node .Tag
157
155
tgen := t .LRTerms (node )
158
156
for i , child := range node .Children {
159
157
term := tgen .Next ()
160
158
if _ , ok := child .(parser.Empty ); ok {
161
- n .many ("@empty" , Extra {map [bool ]string {true : "@prefix" , false : "@suffix" }[i == 0 ]})
159
+ b .many ("@empty" , Extra {map [bool ]string {true : "@prefix" , false : "@suffix" }[i == 0 ]})
162
160
} else {
163
161
if term == t {
164
162
if _ , ok := child .(parser.Node ); ok {
165
163
childCtrs := newCounters (term )
166
- b := Branch {}
164
+ b2 := Branch {}
167
165
childCtrs .termCountChildren (t , ctrs ["" ])
168
- b .fromParserNode (g , term , childCtrs , child )
169
- n .one (tag , b )
166
+ b2 .fromParserNode (g , term , childCtrs , child )
167
+ b .one (tag , b2 )
170
168
} else {
171
- n .fromParserNode (g , t .Term , ctrs , child )
169
+ b .fromParserNode (g , t .Term , ctrs , child )
172
170
}
173
171
} else {
174
- n .fromParserNode (g , term , ctrs , child )
172
+ b .fromParserNode (g , term , ctrs , child )
175
173
}
176
174
}
177
175
}
178
176
case parser.Quant :
179
177
node := e .(parser.Node )
180
- tag = node .Tag
181
178
for _ , child := range node .Children {
182
- n .fromParserNode (g , t .Term , ctrs , child )
179
+ b .fromParserNode (g , t .Term , ctrs , child )
183
180
}
184
181
case parser.Named :
185
182
childCtrs := newCounters (t .Term )
186
- b := Branch {}
187
- b .fromParserNode (g , t .Term , childCtrs , e )
188
- var node Node = b
183
+ b2 := Branch {}
184
+ b2 .fromParserNode (g , t .Term , childCtrs , e )
185
+ var node Node = b2
189
186
// if name := childCtrs.singular(); name != nil {
190
- // node = b [*name].(One).Node
187
+ // node = b2 [*name].(One).Node
191
188
// // TODO: zeroOrOne
192
189
// }
193
- n .add (t .Name , node , ctrs [t .Name ])
190
+ b .add (t .Name , node , ctrs [t .Name ])
194
191
case parser.REF :
195
192
switch e := e .(type ) {
196
193
case parser.Scanner :
197
- n .add (t .Ident , Leaf (e ), ctrs [t .Ident ])
194
+ b .add (t .Ident , Leaf (e ), ctrs [t .Ident ])
198
195
case parser.Node :
199
- b := Branch {}
196
+ b2 := Branch {}
200
197
for _ , child := range e .Children {
201
- b .fromParserNode (g , term , ctrs , child )
198
+ b2 .fromParserNode (g , term , ctrs , child )
202
199
}
203
- n .add (t .Ident , b , ctrs [t .Ident ])
200
+ b .add (t .Ident , b2 , ctrs [t .Ident ])
204
201
}
205
202
case parser.CutPoint :
206
- n .fromParserNode (g , t .Term , ctrs , e )
203
+ b .fromParserNode (g , t .Term , ctrs , e )
207
204
case parser.ExtRef :
208
205
if node , ok := e .(parser.Node ); ok {
209
- if b , ok := node .Extra .(Branch ); ok {
206
+ if b2 , ok := node .Extra .(Branch ); ok {
210
207
ident := t .String ()
211
- n .add (ident , b , ctrs [ident ])
208
+ b .add (ident , b2 , ctrs [ident ])
212
209
}
213
210
}
214
211
default :
0 commit comments