| 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 | // Support for the symbols of the language defined by the input grammar, G. This package supports code generation. |
| 16 | package symbols |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "strings" |
| 21 | |
| 22 | "github.com/goccmack/gocc/internal/ast" |
| 23 | ) |
| 24 | |
| 25 | type Symbols struct { |
| 26 | //key: symbol id |
| 27 | //val: symbol type |
| 28 | idMap map[string]int |
| 29 | |
| 30 | //key: symbol ntTypeMap index |
| 31 | //val: symbol type |
| 32 | ntIdMap map[string]int |
| 33 | ntTypeMap []string |
| 34 | |
| 35 | //key: symbol id |
| 36 | //val: symbol type |
| 37 | stringLitIdMap map[string]int |
| 38 | stringLitList []string |
| 39 | |
| 40 | //key: symbol type |
| 41 | //val: symbol id |
| 42 | typeMap []string |
| 43 | } |
| 44 | |
| 45 | func NewSymbols(grammar *ast.Grammar) *Symbols { |
| 46 | symbols := &Symbols{ |
| 47 | idMap: make(map[string]int), |
| 48 | typeMap: make([]string, 0, 16), |
| 49 | ntIdMap: make(map[string]int), |
| 50 | ntTypeMap: make([]string, 0, 16), |
| 51 | stringLitIdMap: make(map[string]int), |
| 52 | stringLitList: make([]string, 0, 16), |
| 53 | } |
| 54 | |
| 55 | symbols.Add("INVALID") |
| 56 | symbols.Add("␚") |
| 57 | |
| 58 | if grammar.SyntaxPart == nil { |
| 59 | return symbols |
| 60 | } |
| 61 | |
| 62 | for _, p := range grammar.SyntaxPart.ProdList { |
| 63 | if _, exist := symbols.ntIdMap[p.Id]; !exist { |
| 64 | symbols.ntTypeMap = append(symbols.ntTypeMap, p.Id) |
| 65 | symbols.ntIdMap[p.Id] = len(symbols.ntTypeMap) - 1 |
| 66 | } |
| 67 | symbols.Add(p.Id) |
| 68 | for _, sym := range p.Body.Symbols { |
| 69 | symStr := sym.SymbolString() |
| 70 | symbols.Add(symStr) |
| 71 | if _, ok := sym.(ast.SyntaxStringLit); ok { |
| 72 | if _, exist := symbols.ntIdMap[symStr]; exist { |
| 73 | panic(fmt.Sprintf("string_lit \"%s\" conflicts with production name %s", symStr, symStr)) |
| 74 | } |
| 75 | if _, exist := symbols.stringLitIdMap[symStr]; !exist { |
| 76 | symbols.stringLitIdMap[symStr] = symbols.Type(symStr) |
| 77 | symbols.stringLitList = append(symbols.stringLitList, symStr) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | return symbols |
| 83 | } |
| 84 | |
| 85 | func (this *Symbols) Add(symbols ...string) { |
| 86 | for _, sym := range symbols { |
| 87 | if _, exist := this.idMap[sym]; !exist { |
| 88 | this.typeMap = append(this.typeMap, sym) |
| 89 | this.idMap[sym] = len(this.typeMap) - 1 |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func (this *Symbols) Id(typ int) string { |
| 95 | return this.typeMap[typ] |
| 96 | } |
| 97 | |
| 98 | func (this *Symbols) IsTerminal(sym string) bool { |
| 99 | _, nt := this.ntIdMap[sym] |
| 100 | return !nt |
| 101 | } |
| 102 | |
| 103 | func (this *Symbols) List() []string { |
| 104 | return this.typeMap |
| 105 | } |
| 106 | |
| 107 | // ListStringLitSymbols will return a slice containing the ids of |
| 108 | // all symbols declared as string literals in the grammar. |
| 109 | func (this *Symbols) ListStringLitSymbols() []string { |
| 110 | return this.stringLitList |
| 111 | } |
| 112 | |
| 113 | func (this *Symbols) ListTerminals() []string { |
| 114 | terminals := make([]string, 0, 16) |
| 115 | for _, sym := range this.typeMap { |
| 116 | if this.IsTerminal(sym) { |
| 117 | terminals = append(terminals, sym) |
| 118 | } |
| 119 | } |
| 120 | return terminals |
| 121 | } |
| 122 | |
| 123 | func (this *Symbols) StringLitType(id string) int { |
| 124 | if typ, exist := this.stringLitIdMap[id]; exist { |
| 125 | return typ |
| 126 | } |
| 127 | return -1 |
| 128 | } |
| 129 | |
| 130 | // NTId will return the id of the NT with index idx, or "" if |
| 131 | // there is no NT symbol with index, idx. |
| 132 | func (this *Symbols) NTId(idx int) string { |
| 133 | if idx < 0 || idx >= len(this.ntTypeMap) { |
| 134 | return "" |
| 135 | } |
| 136 | return this.ntTypeMap[idx] |
| 137 | } |
| 138 | |
| 139 | // NumNTSymbols will return the number of NT symbols in the grammar. |
| 140 | func (this *Symbols) NumNTSymbols() int { |
| 141 | return len(this.ntTypeMap) |
| 142 | } |
| 143 | |
| 144 | // NTList returns a slice containing all the non-terminal symbols of the grammar. |
| 145 | func (this *Symbols) NTList() []string { |
| 146 | return this.ntTypeMap |
| 147 | } |
| 148 | |
| 149 | // NTType returns the NT index of a symbol (index in 0..|NT|-1) |
| 150 | // or -1 if the symbol is not in NT. |
| 151 | func (this *Symbols) NTType(symbol string) int { |
| 152 | if idx, exist := this.ntIdMap[symbol]; exist { |
| 153 | return idx |
| 154 | } |
| 155 | return -1 |
| 156 | } |
| 157 | |
| 158 | // NumSymbols returns the total number of symbols in grammar: |
| 159 | // the sum of the terminals and non-terminals. |
| 160 | func (this *Symbols) NumSymbols() int { |
| 161 | return len(this.typeMap) |
| 162 | } |
| 163 | |
| 164 | func (this *Symbols) String() string { |
| 165 | w := new(strings.Builder) |
| 166 | for i, sym := range this.typeMap { |
| 167 | fmt.Fprintf(w, "%3d: %s\n", i, sym) |
| 168 | } |
| 169 | return w.String() |
| 170 | } |
| 171 | |
| 172 | func (this *Symbols) Type(id string) int { |
| 173 | if typ, ok := this.idMap[id]; ok { |
| 174 | return typ |
| 175 | } |
| 176 | return -1 |
| 177 | } |