| 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 | "path" |
| 20 | "text/template" |
| 21 | |
| 22 | "github.com/goccmack/gocc/internal/io" |
| 23 | "github.com/goccmack/gocc/internal/lexer/items" |
| 24 | "github.com/goccmack/gocc/internal/token" |
| 25 | ) |
| 26 | |
| 27 | func genActionTable(pkg, outDir string, itemsets *items.ItemSets, tokMap *token.TokenMap) { |
| 28 | fname := path.Join(outDir, "lexer", "acttab.go") |
| 29 | tmpl, err := template.New("action table").Parse(actionTableSrc[1:]) |
| 30 | if err != nil { |
| 31 | panic(err) |
| 32 | } |
| 33 | wr := new(bytes.Buffer) |
| 34 | if err := tmpl.Execute(wr, getActTab(pkg, itemsets, tokMap)); err != nil { |
| 35 | panic(err) |
| 36 | } |
| 37 | io.WriteFile(fname, wr.Bytes()) |
| 38 | } |
| 39 | |
| 40 | func getActTab(pkg string, itemsets *items.ItemSets, tokMap *token.TokenMap) *actTab { |
| 41 | actab := &actTab{ |
| 42 | TokenImport: path.Join(pkg, "token"), |
| 43 | Actions: make([]action, itemsets.Size()), |
| 44 | } |
| 45 | for sno, set := range itemsets.List() { |
| 46 | if act := set.Action(); act != nil { |
| 47 | switch act1 := act.(type) { |
| 48 | case items.Accept: |
| 49 | actab.Actions[sno].Accept = tokMap.IdMap[string(act1)] |
| 50 | actab.Actions[sno].Ignore = "" |
| 51 | case items.Ignore: |
| 52 | actab.Actions[sno].Accept = -1 |
| 53 | actab.Actions[sno].Ignore = string(act1) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | return actab |
| 58 | } |
| 59 | |
| 60 | type actTab struct { |
| 61 | TokenImport string |
| 62 | Actions []action |
| 63 | } |
| 64 | |
| 65 | type action struct { |
| 66 | Accept int |
| 67 | Ignore string |
| 68 | } |
| 69 | |
| 70 | const actionTableSrc = ` |
| 71 | // Code generated by gocc; DO NOT EDIT. |
| 72 | |
| 73 | package lexer |
| 74 | |
| 75 | import ( |
| 76 | "fmt" |
| 77 | |
| 78 | "{{.TokenImport}}" |
| 79 | ) |
| 80 | |
| 81 | type ActionTable [NumStates]ActionRow |
| 82 | |
| 83 | type ActionRow struct { |
| 84 | Accept token.Type |
| 85 | Ignore string |
| 86 | } |
| 87 | |
| 88 | func (a ActionRow) String() string { |
| 89 | return fmt.Sprintf("Accept=%d, Ignore=%s", a.Accept, a.Ignore) |
| 90 | } |
| 91 | |
| 92 | var ActTab = ActionTable{ |
| 93 | {{- range $s, $act := .Actions}} |
| 94 | ActionRow{ // S{{$s}} |
| 95 | Accept: {{$act.Accept}}, |
| 96 | Ignore: "{{$act.Ignore}}", |
| 97 | }, |
| 98 | {{- end}} |
| 99 | } |
| 100 | ` |