| 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 | "math" |
| 21 | "path" |
| 22 | "text/template" |
| 23 | |
| 24 | "github.com/goccmack/gocc/internal/ast" |
| 25 | "github.com/goccmack/gocc/internal/io" |
| 26 | "github.com/goccmack/gocc/internal/parser/lr1/action" |
| 27 | "github.com/goccmack/gocc/internal/parser/lr1/items" |
| 28 | "github.com/goccmack/gocc/internal/token" |
| 29 | ) |
| 30 | |
| 31 | func GenActionTable(outDir string, prods ast.SyntaxProdList, itemSets *items.ItemSets, tokMap *token.TokenMap, zip bool) map[int]items.RowConflicts { |
| 32 | if zip { |
| 33 | return GenCompActionTable(outDir, prods, itemSets, tokMap) |
| 34 | } |
| 35 | tmpl, err := template.New("parser action table").Parse(actionTableSrc[1:]) |
| 36 | if err != nil { |
| 37 | panic(err) |
| 38 | } |
| 39 | wr := new(bytes.Buffer) |
| 40 | data, conflicts := getActionTableData(prods, itemSets, tokMap) |
| 41 | if err := tmpl.Execute(wr, data); err != nil { |
| 42 | panic(err) |
| 43 | } |
| 44 | io.WriteFile(path.Join(outDir, "parser", "actiontable.go"), wr.Bytes()) |
| 45 | return conflicts |
| 46 | } |
| 47 | |
| 48 | type actionTableData struct { |
| 49 | Rows []*actRow |
| 50 | } |
| 51 | |
| 52 | func getActionTableData(prods ast.SyntaxProdList, itemSets *items.ItemSets, |
| 53 | tokMap *token.TokenMap) (actTab *actionTableData, conflicts map[int]items.RowConflicts) { |
| 54 | actTab = &actionTableData{ |
| 55 | Rows: make([]*actRow, itemSets.Size()), |
| 56 | } |
| 57 | conflicts = make(map[int]items.RowConflicts) |
| 58 | var cnflcts items.RowConflicts |
| 59 | var row *actRow |
| 60 | for i := range actTab.Rows { |
| 61 | if row, cnflcts = getActionRowData(prods, itemSets.Set(i), tokMap); len(cnflcts) > 0 { |
| 62 | conflicts[i] = cnflcts |
| 63 | } |
| 64 | actTab.Rows[i] = row |
| 65 | } |
| 66 | return |
| 67 | } |
| 68 | |
| 69 | type actRow struct { |
| 70 | CanRecover bool |
| 71 | Actions []string |
| 72 | } |
| 73 | |
| 74 | func getActionRowData(prods ast.SyntaxProdList, set *items.ItemSet, tokMap *token.TokenMap) (data *actRow, conflicts items.RowConflicts) { |
| 75 | data = &actRow{ |
| 76 | CanRecover: set.CanRecover(), |
| 77 | Actions: make([]string, len(tokMap.TypeMap)), |
| 78 | } |
| 79 | conflicts = make(items.RowConflicts) |
| 80 | var max int |
| 81 | // calculate padding. |
| 82 | for _, sym := range tokMap.TypeMap { |
| 83 | act, _ := set.Action(sym) |
| 84 | switch act1 := act.(type) { |
| 85 | case action.Accept: |
| 86 | n := len("accept(true),") |
| 87 | if n > max { |
| 88 | max = n |
| 89 | } |
| 90 | case action.Error: |
| 91 | n := len("nil,") |
| 92 | if n > max { |
| 93 | max = n |
| 94 | } |
| 95 | case action.Reduce: |
| 96 | n := len("reduce(") + nbytes(int(act1)) + len("),") |
| 97 | if n > max { |
| 98 | max = n |
| 99 | } |
| 100 | case action.Shift: |
| 101 | n := len("shift(") + nbytes(int(act1)) + len("),") |
| 102 | if n > max { |
| 103 | max = n |
| 104 | } |
| 105 | default: |
| 106 | panic(fmt.Sprintf("Unknown action type: %T", act1)) |
| 107 | } |
| 108 | } |
| 109 | for i, sym := range tokMap.TypeMap { |
| 110 | act, symConflicts := set.Action(sym) |
| 111 | if len(symConflicts) > 0 { |
| 112 | conflicts[sym] = symConflicts |
| 113 | } |
| 114 | switch act1 := act.(type) { |
| 115 | case action.Accept: |
| 116 | pad := max + 1 - len("accept(true),") |
| 117 | data.Actions[i] = fmt.Sprintf("accept(true),%*c// %s", pad, ' ', sym) |
| 118 | case action.Error: |
| 119 | pad := max + 1 - len("nil,") |
| 120 | data.Actions[i] = fmt.Sprintf("nil,%*c// %s", pad, ' ', sym) |
| 121 | case action.Reduce: |
| 122 | pad := max + 1 - (len("reduce(") + nbytes(int(act1)) + len("),")) |
| 123 | data.Actions[i] = fmt.Sprintf("reduce(%d),%*c// %s, reduce: %s", int(act1), pad, ' ', sym, prods[int(act1)].Id) |
| 124 | case action.Shift: |
| 125 | pad := max + 1 - (len("shift(") + nbytes(int(act1)) + len("),")) |
| 126 | data.Actions[i] = fmt.Sprintf("shift(%d),%*c// %s", int(act1), pad, ' ', sym) |
| 127 | default: |
| 128 | panic(fmt.Sprintf("Unknown action type: %T", act1)) |
| 129 | } |
| 130 | } |
| 131 | return |
| 132 | } |
| 133 | |
| 134 | const actionTableSrc = ` |
| 135 | // Code generated by gocc; DO NOT EDIT. |
| 136 | |
| 137 | package parser |
| 138 | |
| 139 | type ( |
| 140 | actionTable [numStates]actionRow |
| 141 | actionRow struct { |
| 142 | canRecover bool |
| 143 | actions [numSymbols]action |
| 144 | } |
| 145 | ) |
| 146 | |
| 147 | var actionTab = actionTable{ |
| 148 | {{- range $i, $r := .Rows }} |
| 149 | actionRow{ // S{{$i}} |
| 150 | canRecover: {{printf "%t" .CanRecover}}, |
| 151 | actions: [numSymbols]action{ |
| 152 | {{- range $a := .Actions }} |
| 153 | {{$a}} |
| 154 | {{- end }} |
| 155 | }, |
| 156 | }, |
| 157 | {{- end }} |
| 158 | } |
| 159 | ` |
| 160 | |
| 161 | func GenCompActionTable(outDir string, prods ast.SyntaxProdList, itemSets *items.ItemSets, tokMap *token.TokenMap) map[int]items.RowConflicts { |
| 162 | tab := make([]struct { |
| 163 | CanRecover bool |
| 164 | Actions []struct { |
| 165 | Index int |
| 166 | Action int |
| 167 | Amount int |
| 168 | } |
| 169 | }, itemSets.Size()) |
| 170 | conflictss := make(map[int]items.RowConflicts) |
| 171 | for i := range tab { |
| 172 | set := itemSets.Set(i) |
| 173 | tab[i].CanRecover = set.CanRecover() |
| 174 | conflicts := make(items.RowConflicts) |
| 175 | for j, sym := range tokMap.TypeMap { |
| 176 | act, symConflicts := set.Action(sym) |
| 177 | if len(symConflicts) > 0 { |
| 178 | conflicts[sym] = symConflicts |
| 179 | } |
| 180 | switch act1 := act.(type) { |
| 181 | case action.Accept: |
| 182 | tab[i].Actions = append(tab[i].Actions, struct { |
| 183 | Index int |
| 184 | Action int |
| 185 | Amount int |
| 186 | }{Index: j, Action: 0, Amount: 0}) |
| 187 | case action.Error: |
| 188 | // skip |
| 189 | case action.Reduce: |
| 190 | tab[i].Actions = append(tab[i].Actions, struct { |
| 191 | Index int |
| 192 | Action int |
| 193 | Amount int |
| 194 | }{Index: j, Action: 1, Amount: int(act1)}) |
| 195 | case action.Shift: |
| 196 | tab[i].Actions = append(tab[i].Actions, struct { |
| 197 | Index int |
| 198 | Action int |
| 199 | Amount int |
| 200 | }{Index: j, Action: 2, Amount: int(act1)}) |
| 201 | default: |
| 202 | panic(fmt.Sprintf("Unknown action type: %T", act1)) |
| 203 | } |
| 204 | } |
| 205 | if len(conflicts) > 0 { |
| 206 | conflictss[i] = conflicts |
| 207 | } |
| 208 | } |
| 209 | bytesStr := genEnc(tab) |
| 210 | tmpl, err := template.New("parser action table").Parse(actionCompTableSrc[1:]) |
| 211 | if err != nil { |
| 212 | panic(err) |
| 213 | } |
| 214 | wr := new(bytes.Buffer) |
| 215 | if err := tmpl.Execute(wr, bytesStr); err != nil { |
| 216 | panic(err) |
| 217 | } |
| 218 | io.WriteFile(path.Join(outDir, "parser", "actiontable.go"), wr.Bytes()) |
| 219 | return conflictss |
| 220 | } |
| 221 | |
| 222 | const actionCompTableSrc = ` |
| 223 | // Code generated by gocc; DO NOT EDIT. |
| 224 | |
| 225 | package parser |
| 226 | |
| 227 | import ( |
| 228 | "bytes" |
| 229 | "compress/gzip" |
| 230 | "encoding/gob" |
| 231 | ) |
| 232 | |
| 233 | type ( |
| 234 | actionTable [numStates]actionRow |
| 235 | actionRow struct { |
| 236 | canRecover bool |
| 237 | actions [numSymbols]action |
| 238 | } |
| 239 | ) |
| 240 | |
| 241 | var actionTab = actionTable{} |
| 242 | |
| 243 | func init() { |
| 244 | tab := []struct { |
| 245 | CanRecover bool |
| 246 | Actions []struct { |
| 247 | Index int |
| 248 | Action int |
| 249 | Amount int |
| 250 | } |
| 251 | }{} |
| 252 | data := {{.}} |
| 253 | buf, err := gzip.NewReader(bytes.NewBuffer(data)) |
| 254 | if err != nil { |
| 255 | panic(err) |
| 256 | } |
| 257 | dec := gob.NewDecoder(buf) |
| 258 | if err := dec.Decode(&tab); err != nil { |
| 259 | panic(err) |
| 260 | } |
| 261 | |
| 262 | for i, row := range tab { |
| 263 | actionTab[i].canRecover = row.CanRecover |
| 264 | for _, a := range row.Actions { |
| 265 | switch a.Action { |
| 266 | case 0: |
| 267 | actionTab[i].actions[a.Index] = accept(true) |
| 268 | case 1: |
| 269 | actionTab[i].actions[a.Index] = reduce(a.Amount) |
| 270 | case 2: |
| 271 | actionTab[i].actions[a.Index] = shift(a.Amount) |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | ` |
| 277 | |
| 278 | // nbytes returns the number of bytes required to output the integer x. |
| 279 | func nbytes(x int) int { |
| 280 | if x == 0 { |
| 281 | return 1 |
| 282 | } |
| 283 | n := 0 |
| 284 | if x < 0 { |
| 285 | x = -x |
| 286 | n++ |
| 287 | } |
| 288 | n += int(math.Log10(float64(x))) + 1 |
| 289 | return n |
| 290 | } |