Debian Codegraph @

internal/parser/gen/golang/productionstable.go

raw symbols SCIP
1 //Copyright 2013 Vastech SA (PTY) LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package golang
16
17 import (
18 "bytes"
19 "fmt"
20 "path"
21 "text/template"
22
23 "github.com/goccmack/gocc/internal/ast"
24 "github.com/goccmack/gocc/internal/io"
25 "github.com/goccmack/gocc/internal/parser/lr1/items"
26 "github.com/goccmack/gocc/internal/parser/symbols"
27 "github.com/goccmack/gocc/internal/token"
28 )
29
30 func GenProductionsTable(pkg, outDir, header string, prods ast.SyntaxProdList, symbols *symbols.Symbols,
31 itemsets *items.ItemSets, tokMap *token.TokenMap) {
32 fname := path.Join(outDir, "parser", "productionstable.go")
33 tmpl, err := template.New("parser productions table").Parse(prodsTabSrc[1:])
34 if err != nil {
35 panic(err)
36 }
37 wr := new(bytes.Buffer)
38 if err := tmpl.Execute(wr, getProdsTab(header, prods, symbols, itemsets, tokMap)); err != nil {
39 panic(err)
40 }
41 io.WriteFile(fname, wr.Bytes())
42 }
43
44 func getProdsTab(header string, prods ast.SyntaxProdList, symbols *symbols.Symbols,
45 itemsets *items.ItemSets, tokMap *token.TokenMap) *prodsTabData {
46 data := &prodsTabData{
47 Header: header,
48 ProdTab: make([]prodTabEntry, len(prods)),
49 }
50 for i, prod := range prods {
51 data.ProdTab[i].String = fmt.Sprintf("`%s`", prod.String())
52 data.ProdTab[i].Id = prod.Id
53 data.ProdTab[i].NTType = symbols.NTType(prod.Id)
54 isEmpty := prod.Body.Symbols[0].SymbolString() == "empty"
55 if isEmpty {
56 data.ProdTab[i].NumSymbols = 0
57 } else {
58 data.ProdTab[i].NumSymbols = len(prod.Body.Symbols)
59 }
60 switch {
61 case len(prod.Body.SDT) > 0:
62 data.ProdTab[i].ReduceFunc = fmt.Sprintf("return %s", prod.Body.SDT)
63 case isEmpty:
64 // Empty production with no semantic action.
65 data.ProdTab[i].ReduceFunc = "return nil, nil"
66 default:
67 data.ProdTab[i].ReduceFunc = "return X[0], nil"
68 }
69 }
70
71 return data
72 }
73
74 type prodsTabData struct {
75 Header string
76 ProdTab []prodTabEntry
77 }
78
79 type prodTabEntry struct {
80 String string
81 Id string
82 NTType int
83 NumSymbols int
84 ReduceFunc string
85 }
86
87 // TODO(types): change type and variable names to be consistent with other tables
88 const prodsTabSrc = `
89 // Code generated by gocc; DO NOT EDIT.
90
91 package parser
92 {{if .Header}}
93 {{.Header}}
94 {{end}}
95 type (
96 ProdTab [numProductions]ProdTabEntry
97 ProdTabEntry struct {
98 String string
99 Id string
100 NTType int
101 Index int
102 NumSymbols int
103 ReduceFunc func([]Attrib, interface{}) (Attrib, error)
104 }
105 Attrib interface {
106 }
107 )
108
109 var productionsTable = ProdTab{
110 {{- range $i, $entry := .ProdTab }}
111 ProdTabEntry{
112 String: {{$entry.String}},
113 Id: "{{$entry.Id}}",
114 NTType: {{$entry.NTType}},
115 Index: {{$i}},
116 NumSymbols: {{$entry.NumSymbols}},
117 ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
118 {{$entry.ReduceFunc}}
119 },
120 },
121 {{- end }}
122 }
123 `