| 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 symbols |
| 16 | |
| 17 | import ( |
| 18 | "github.com/goccmack/gocc/internal/ast" |
| 19 | ) |
| 20 | |
| 21 | // key: string of symbols - string(ast.CharLit.Lit). E.g.: "'a'" |
| 22 | type CharLitSymbols struct { |
| 23 | idMap map[string]int |
| 24 | typeMap []*ast.LexCharLit |
| 25 | } |
| 26 | |
| 27 | func NewCharLitSymbols() *CharLitSymbols { |
| 28 | return &CharLitSymbols{ |
| 29 | idMap: make(map[string]int), |
| 30 | typeMap: make([]*ast.LexCharLit, 0, 16), |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func (this *CharLitSymbols) Add(cl *ast.LexCharLit) { |
| 35 | this.typeMap = append(this.typeMap, cl) |
| 36 | this.idMap[cl.String()] = len(this.typeMap) - 1 |
| 37 | } |
| 38 | |
| 39 | func (this *CharLitSymbols) GetSymbolId(id string) (sym *ast.LexCharLit, exist bool) { |
| 40 | if idx, ok := this.idMap[id]; !ok { |
| 41 | return nil, false |
| 42 | } else { |
| 43 | sym = this.typeMap[idx] |
| 44 | } |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | func (this *CharLitSymbols) Len() int { |
| 49 | return len(this.typeMap) |
| 50 | } |
| 51 | |
| 52 | func (this *CharLitSymbols) List() []*ast.LexCharLit { |
| 53 | return this.typeMap |
| 54 | } |
| 55 | |
| 56 | func (this *CharLitSymbols) StringList() []string { |
| 57 | symbols := make([]string, len(this.typeMap)) |
| 58 | for i, sym := range this.typeMap { |
| 59 | symbols[i] = sym.String() |
| 60 | } |
| 61 | return symbols |
| 62 | } |