-
Notifications
You must be signed in to change notification settings - Fork 48
/
productionstable.go
123 lines (112 loc) · 3.23 KB
/
productionstable.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
//Copyright 2013 Vastech SA (PTY) LTD
//
// 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 golang
import (
"bytes"
"fmt"
"path"
"text/template"
"github.com/goccmack/gocc/internal/ast"
"github.com/goccmack/gocc/internal/io"
"github.com/goccmack/gocc/internal/parser/lr1/items"
"github.com/goccmack/gocc/internal/parser/symbols"
"github.com/goccmack/gocc/internal/token"
)
func GenProductionsTable(pkg, outDir, header string, prods ast.SyntaxProdList, symbols *symbols.Symbols,
itemsets *items.ItemSets, tokMap *token.TokenMap) {
fname := path.Join(outDir, "parser", "productionstable.go")
tmpl, err := template.New("parser productions table").Parse(prodsTabSrc[1:])
if err != nil {
panic(err)
}
wr := new(bytes.Buffer)
if err := tmpl.Execute(wr, getProdsTab(header, prods, symbols, itemsets, tokMap)); err != nil {
panic(err)
}
io.WriteFile(fname, wr.Bytes())
}
func getProdsTab(header string, prods ast.SyntaxProdList, symbols *symbols.Symbols,
itemsets *items.ItemSets, tokMap *token.TokenMap) *prodsTabData {
data := &prodsTabData{
Header: header,
ProdTab: make([]prodTabEntry, len(prods)),
}
for i, prod := range prods {
data.ProdTab[i].String = fmt.Sprintf("`%s`", prod.String())
data.ProdTab[i].Id = prod.Id
data.ProdTab[i].NTType = symbols.NTType(prod.Id)
isEmpty := prod.Body.Symbols[0].SymbolString() == "empty"
if isEmpty {
data.ProdTab[i].NumSymbols = 0
} else {
data.ProdTab[i].NumSymbols = len(prod.Body.Symbols)
}
switch {
case len(prod.Body.SDT) > 0:
data.ProdTab[i].ReduceFunc = fmt.Sprintf("return %s", prod.Body.SDT)
case isEmpty:
// Empty production with no semantic action.
data.ProdTab[i].ReduceFunc = "return nil, nil"
default:
data.ProdTab[i].ReduceFunc = "return X[0], nil"
}
}
return data
}
type prodsTabData struct {
Header string
ProdTab []prodTabEntry
}
type prodTabEntry struct {
String string
Id string
NTType int
NumSymbols int
ReduceFunc string
}
// TODO(types): change type and variable names to be consistent with other tables
const prodsTabSrc = `
// Code generated by gocc; DO NOT EDIT.
package parser
{{if .Header}}
{{.Header}}
{{end}}
type (
ProdTab [numProductions]ProdTabEntry
ProdTabEntry struct {
String string
Id string
NTType int
Index int
NumSymbols int
ReduceFunc func([]Attrib, interface{}) (Attrib, error)
}
Attrib interface {
}
)
var productionsTable = ProdTab{
{{- range $i, $entry := .ProdTab }}
ProdTabEntry{
String: {{$entry.String}},
Id: "{{$entry.Id}}",
NTType: {{$entry.NTType}},
Index: {{$i}},
NumSymbols: {{$entry.NumSymbols}},
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
{{$entry.ReduceFunc}}
},
},
{{- end }}
}
`