/
slots.go
290 lines (248 loc) · 6.1 KB
/
slots.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright 2021 Aaron Moss
// Copyright 2019 Marius Ackerman
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package slots generates a Go parser slots package
package slots
import (
"bytes"
"text/template"
"github.com/bruceiv/pegll/ast"
"github.com/bruceiv/pegll/frstflw"
"github.com/bruceiv/pegll/gslot"
gsyms "github.com/bruceiv/pegll/symbols"
"github.com/goccmack/goutil/ioutil"
)
func Gen(slotFile string, g *ast.GoGLL, gs *gslot.GSlot, ff *frstflw.FF) {
tmpl, err := template.New("Slot").Parse(slotTmpl)
if err != nil {
panic(err)
}
buf, data := new(bytes.Buffer), getData(g, gs, ff)
if err = tmpl.Execute(buf, data); err != nil {
panic(err)
}
if err := ioutil.WriteFile(slotFile, buf.Bytes()); err != nil {
panic(err)
}
}
type Data struct {
Package string
Slots []*SlotData
Alts []*AltData
}
type AltData struct {
NT string
Labels string
}
type SlotData struct {
Label string
NT string
Alt int
Pos int
Nullable bool
Symbols []string
FirstT []string
}
func getData(g *ast.GoGLL, gs *gslot.GSlot, ff *frstflw.FF) *Data {
return &Data{
Package: g.Package.GetString(),
Slots: getSlotData(gs, ff),
Alts: getAltData(g, gs, ff),
}
}
func getAltData(g *ast.GoGLL, gs *gslot.GSlot, ff *frstflw.FF) (data []*AltData) {
for _, r := range g.SyntaxRules {
d := &AltData{r.Head.ID(), getLabelList(r, g, gs, ff)}
data = append(data, d)
}
return
}
func getLabelList(rule *ast.SyntaxRule, g *ast.GoGLL, gs *gslot.GSlot, ff *frstflw.FF) string {
buf := new(bytes.Buffer)
for i := range rule.Alternates {
if i > 0 {
buf.WriteString(",")
}
buf.WriteString(gslot.NewLabel(rule.Head.ID(), i, 0, gslot.Unknown, gs, ff).Label())
}
return buf.String()
}
func getSlotData(gs *gslot.GSlot, ff *frstflw.FF) (data []*SlotData) {
for _, s := range gs.Slots() {
d := &SlotData{
Label: s.Label(),
NT: s.Head,
Alt: s.Alternate,
Pos: s.Pos,
Nullable: isNullable(s, ff),
Symbols: s.Symbols().GoStrings(),
FirstT: firstT(s, ff),
}
data = append(data, d)
}
return
}
// true if the remainder of the symbols in the sequence represented by this slot can
// match while consuming no input
func isNullable(s gslot.Label, ff *frstflw.FF) bool {
if s.Mode == gslot.Fail {
return false
}
syms := s.Symbols()
for i := s.Pos; i < len(syms); i++ {
if !ff.IsNullable(syms[i].String()) {
return false
}
}
return true
}
// gets FIRST set for a slot
func firstT(s gslot.Label, ff *frstflw.FF) []string {
if s.Mode == gslot.Fail {
return make([]string, 0)
}
fsts := ff.FirstOfString(s.Symbols().Strings()[s.Pos:])
fsts.Remove(frstflw.Empty)
r := make([]string, 0, fsts.Len())
for _, lit := range fsts.Elements() {
t := gsyms.TerminalLiteralToType(lit)
r = append(r, t.GoString())
}
return r
}
const slotTmpl = `
// Package slot is generated by gogll. Do not edit.
package slot
import(
"bytes"
"fmt"
"{{.Package}}/parser/symbols"
"{{.Package}}/token"
)
type Label int
const({{range $i, $l := .Slots}}
{{$l.Label}}{{if not $i}} Label = iota{{end}}{{end}}
)
type Slot struct {
NT symbols.NT
Alt int
Pos int
Symbols symbols.Symbols
Label Label
}
type Index struct {
NT symbols.NT
Alt int
Pos int
}
func GetAlternates(nt symbols.NT) []Label {
alts, exist := alternates[nt]
if !exist {
panic(fmt.Sprintf("Invalid NT %s", nt))
}
return alts
}
func GetLabel(nt symbols.NT, alt, pos int) Label {
l, exist := slotIndex[Index{nt,alt,pos}]
if exist {
return l
}
panic(fmt.Sprintf("Error: no slot label for NT=%s, alt=%d, pos=%d", nt, alt, pos))
}
func (l Label) EoR() bool {
return l.Slot().EoR()
}
func (l Label) Head() symbols.NT {
return l.Slot().NT
}
func (l Label) Index() Index {
s := l.Slot()
return Index{s.NT, s.Alt, s.Pos}
}
func (l Label) Alternate() int {
return l.Slot().Alt
}
func (l Label) Pos() int {
return l.Slot().Pos
}
func (l Label) Slot() *Slot {
s, exist := slots[l]
if !exist {
panic(fmt.Sprintf("Invalid slot label %d", l))
}
return s
}
func (l Label) String() string {
return l.Slot().String()
}
func (l Label) Symbols() symbols.Symbols {
return l.Slot().Symbols
}
func (l Label) IsNullable() bool {
return nullable[l]
}
func (l Label) FirstContains(typ token.Type) bool {
return firstT[l][typ]
}
func (l Label) IsLookahead() bool {
s := l.Slot()
return s.Pos > 0 && s.Symbols[s.Pos-1].IsLookahead()
}
func (s *Slot) EoR() bool {
return s.Pos >= len(s.Symbols)
}
func (s *Slot) Successor() *Slot {
if s.EoR() {
return nil
} else {
// TODO try slots[s.Label + 1]
return slots[slotIndex[Index{s.NT,s.Alt,s.Pos+1}]]
}
}
func (s *Slot) String() string {
buf := new(bytes.Buffer)
fmt.Fprintf(buf, "%s : ", s.NT)
for i, sym := range s.Symbols {
if i == s.Pos {
fmt.Fprintf(buf, "∙")
}
fmt.Fprintf(buf, "%s ", sym)
}
if s.Pos >= len(s.Symbols) {
fmt.Fprintf(buf, "∙")
}
return buf.String()
}
var slots = map[Label]*Slot{ {{range $i, $s := .Slots}}
{{$s.Label}}: {
symbols.NT_{{$s.NT}}, {{$s.Alt}}, {{$s.Pos}},
symbols.Symbols{ {{range $sym := $s.Symbols}}
symbols.{{$sym}},{{end}}
},
{{$s.Label}},
},{{end}}
}
var slotIndex = map[Index]Label { {{range $i, $s := .Slots}}
Index{ symbols.NT_{{$s.NT}},{{$s.Alt}},{{$s.Pos}} }: {{$s.Label}},{{end}}
}
var alternates = map[symbols.NT][]Label{ {{range $i, $a := .Alts}}
symbols.NT_{{$a.NT}}:[]Label{ {{$a.Labels}} },{{end}}
}
var nullable = []bool { {{range $i, $s := .Slots}}
{{$s.Nullable}}, // {{$s.Label}} {{end}}
}
var firstT = []map[token.Type]bool { {{range $i, $s := .Slots}}
{ {{range $j, $t := $s.FirstT}} token.{{$t}}: true, {{end}} }, // {{$s.Label}} {{end}}
}
`