| 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 | "compress/gzip" |
| 20 | "encoding/gob" |
| 21 | "fmt" |
| 22 | "path" |
| 23 | "text/template" |
| 24 | |
| 25 | "github.com/goccmack/gocc/internal/io" |
| 26 | "github.com/goccmack/gocc/internal/parser/lr1/items" |
| 27 | "github.com/goccmack/gocc/internal/parser/symbols" |
| 28 | ) |
| 29 | |
| 30 | type gotoTableData struct { |
| 31 | NumNTSymbols int |
| 32 | Rows [][]gotoRowElement |
| 33 | } |
| 34 | |
| 35 | type gotoRowElement struct { |
| 36 | NT string |
| 37 | State int |
| 38 | Pad int |
| 39 | } |
| 40 | |
| 41 | func GenGotoTable(outDir string, itemSets *items.ItemSets, sym *symbols.Symbols, zip bool) { |
| 42 | if zip { |
| 43 | GenCompGotoTable(outDir, itemSets, sym) |
| 44 | return |
| 45 | } |
| 46 | tmpl, err := template.New("parser goto table").Parse(gotoTableSrc[1:]) |
| 47 | if err != nil { |
| 48 | panic(err) |
| 49 | } |
| 50 | wr := new(bytes.Buffer) |
| 51 | if err := tmpl.Execute(wr, getGotoTableData(itemSets, sym)); err != nil { |
| 52 | panic(err) |
| 53 | } |
| 54 | io.WriteFile(path.Join(outDir, "parser", "gototable.go"), wr.Bytes()) |
| 55 | } |
| 56 | |
| 57 | func getGotoTableData(itemSets *items.ItemSets, sym *symbols.Symbols) *gotoTableData { |
| 58 | data := &gotoTableData{ |
| 59 | NumNTSymbols: sym.NumNTSymbols(), |
| 60 | Rows: make([][]gotoRowElement, itemSets.Size()), |
| 61 | } |
| 62 | for i, set := range itemSets.List() { |
| 63 | data.Rows[i] = getGotoRowData(set, sym) |
| 64 | } |
| 65 | return data |
| 66 | } |
| 67 | |
| 68 | func getGotoRowData(itemSet *items.ItemSet, sym *symbols.Symbols) []gotoRowElement { |
| 69 | row := make([]gotoRowElement, sym.NumNTSymbols()) |
| 70 | var max int |
| 71 | for i, nt := range sym.NTList() { |
| 72 | row[i].NT = nt |
| 73 | row[i].State = itemSet.NextSetIndex(nt) |
| 74 | n := nbytes(row[i].State) |
| 75 | if n > max { |
| 76 | max = n |
| 77 | } |
| 78 | } |
| 79 | // Calculate padding. |
| 80 | for i := range row { |
| 81 | row[i].Pad = max + 1 - nbytes(row[i].State) |
| 82 | } |
| 83 | return row |
| 84 | } |
| 85 | |
| 86 | const gotoTableSrc = ` |
| 87 | // Code generated by gocc; DO NOT EDIT. |
| 88 | |
| 89 | package parser |
| 90 | |
| 91 | const numNTSymbols = {{.NumNTSymbols}} |
| 92 | |
| 93 | type ( |
| 94 | gotoTable [numStates]gotoRow |
| 95 | gotoRow [numNTSymbols]int |
| 96 | ) |
| 97 | |
| 98 | var gotoTab = gotoTable{ |
| 99 | {{- range $i, $r := .Rows }} |
| 100 | gotoRow{ // S{{$i}} |
| 101 | {{- range $j, $gto := . }} |
| 102 | {{ printf "%d,%*c// %s" $gto.State $gto.Pad ' ' $gto.NT }} |
| 103 | {{- end }} |
| 104 | }, |
| 105 | {{- end }} |
| 106 | } |
| 107 | ` |
| 108 | |
| 109 | func genEnc(v interface{}) string { |
| 110 | buf := bytes.NewBuffer(nil) |
| 111 | z := gzip.NewWriter(buf) |
| 112 | enc := gob.NewEncoder(z) |
| 113 | if err := enc.Encode(v); err != nil { |
| 114 | panic(err) |
| 115 | } |
| 116 | if err := z.Close(); err != nil { |
| 117 | panic(err) |
| 118 | } |
| 119 | strBuf := bytes.NewBuffer(nil) |
| 120 | fmt.Fprintf(strBuf, "[]byte{\n") |
| 121 | b := buf.Bytes() |
| 122 | for len(b) > 0 { |
| 123 | n := 16 |
| 124 | if n > len(b) { |
| 125 | n = len(b) |
| 126 | } |
| 127 | fmt.Fprintf(strBuf, "\t\t") |
| 128 | for i, c := range b[:n] { |
| 129 | if i != 0 { |
| 130 | strBuf.WriteString(" ") |
| 131 | } |
| 132 | fmt.Fprintf(strBuf, "0x%02x,", c) |
| 133 | } |
| 134 | fmt.Fprintf(strBuf, "\n") |
| 135 | b = b[n:] |
| 136 | } |
| 137 | fmt.Fprintf(strBuf, "\t}") |
| 138 | return string(strBuf.Bytes()) |
| 139 | } |
| 140 | |
| 141 | func GenCompGotoTable(outDir string, itemSets *items.ItemSets, sym *symbols.Symbols) { |
| 142 | numNTSymbols := sym.NumNTSymbols() |
| 143 | rows := make([][]int, itemSets.Size()) |
| 144 | for i, set := range itemSets.List() { |
| 145 | rows[i] = make([]int, numNTSymbols) |
| 146 | for j, nt := range sym.NTList() { |
| 147 | rows[i][j] = set.NextSetIndex(nt) |
| 148 | } |
| 149 | } |
| 150 | bytesStr := genEnc(rows) |
| 151 | v := struct { |
| 152 | NumNTSymbols int |
| 153 | Bytes string |
| 154 | }{ |
| 155 | NumNTSymbols: numNTSymbols, |
| 156 | Bytes: bytesStr, |
| 157 | } |
| 158 | tmpl, err := template.New("parser goto table").Parse(gotoTableCompSrc[1:]) |
| 159 | if err != nil { |
| 160 | panic(err) |
| 161 | } |
| 162 | wr := new(bytes.Buffer) |
| 163 | if err := tmpl.Execute(wr, v); err != nil { |
| 164 | panic(err) |
| 165 | } |
| 166 | io.WriteFile(path.Join(outDir, "parser", "gototable.go"), wr.Bytes()) |
| 167 | } |
| 168 | |
| 169 | const gotoTableCompSrc = ` |
| 170 | // Code generated by gocc; DO NOT EDIT. |
| 171 | |
| 172 | package parser |
| 173 | |
| 174 | import ( |
| 175 | "bytes" |
| 176 | "compress/gzip" |
| 177 | "encoding/gob" |
| 178 | ) |
| 179 | |
| 180 | const numNTSymbols = {{.NumNTSymbols}} |
| 181 | |
| 182 | type ( |
| 183 | gotoTable [numStates]gotoRow |
| 184 | gotoRow [numNTSymbols]int |
| 185 | ) |
| 186 | |
| 187 | var gotoTab = gotoTable{} |
| 188 | |
| 189 | func init() { |
| 190 | tab := [][]int{} |
| 191 | data := {{.Bytes}} |
| 192 | buf, err := gzip.NewReader(bytes.NewBuffer(data)) |
| 193 | if err != nil { |
| 194 | panic(err) |
| 195 | } |
| 196 | dec := gob.NewDecoder(buf) |
| 197 | if err := dec.Decode(&tab); err != nil { |
| 198 | panic(err) |
| 199 | } |
| 200 | for i := 0; i < numStates; i++ { |
| 201 | for j := 0; j < numNTSymbols; j++ { |
| 202 | gotoTab[i][j] = tab[i][j] |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | ` |