| 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 | "fmt" |
| 19 | "strings" |
| 20 | |
| 21 | "github.com/goccmack/gocc/internal/ast" |
| 22 | ) |
| 23 | |
| 24 | type Symbols struct { |
| 25 | *CharLitSymbols |
| 26 | *CharRangeSymbols |
| 27 | ImportIdList []string |
| 28 | importIdMap map[string]int |
| 29 | //key: import Id, value: external function |
| 30 | importFuncMap map[string]string |
| 31 | typeMap []string |
| 32 | idMap map[string]int // key: symbol id, val: symbol type |
| 33 | } |
| 34 | |
| 35 | func NewSymbols(lexpart *ast.LexPart) (sym *Symbols) { |
| 36 | sym = &Symbols{ |
| 37 | CharLitSymbols: NewCharLitSymbols(), |
| 38 | CharRangeSymbols: NewCharRangeSymbols(), |
| 39 | ImportIdList: make([]string, 0, len(lexpart.Imports)+1), |
| 40 | importIdMap: make(map[string]int), |
| 41 | importFuncMap: make(map[string]string), |
| 42 | } |
| 43 | for _, tokDef := range lexpart.TokDefsList { |
| 44 | tokDef.LexPattern().Walk(sym) |
| 45 | } |
| 46 | for _, regDef := range lexpart.RegDefsList { |
| 47 | regDef.LexPattern().Walk(sym) |
| 48 | } |
| 49 | for _, igDef := range lexpart.IgnoredTokDefsList { |
| 50 | igDef.LexPattern().Walk(sym) |
| 51 | } |
| 52 | for id, extFunc := range lexpart.Imports { |
| 53 | sym.ImportIdList = append(sym.ImportIdList, id) |
| 54 | sym.importIdMap[id] = len(sym.ImportIdList) - 1 |
| 55 | sym.importFuncMap[id] = extFunc.ExtFunc |
| 56 | } |
| 57 | sym.makeMaps() |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | func (this *Symbols) makeMaps() { |
| 62 | this.typeMap = make([]string, 0, this.CharLitSymbols.Len()+this.CharRangeSymbols.Len()+len(this.ImportIdList)) |
| 63 | this.typeMap = append(this.typeMap, this.CharLitSymbols.StringList()...) |
| 64 | this.typeMap = append(this.typeMap, this.CharRangeSymbols.StringList()...) |
| 65 | this.typeMap = append(this.typeMap, this.ImportIdList...) |
| 66 | this.typeMap = append(this.typeMap, ".") |
| 67 | this.idMap = make(map[string]int) |
| 68 | for i, sym := range this.typeMap { |
| 69 | this.idMap[sym] = i |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // ExternalFunction returns the external function associated with the import id |
| 74 | // or "" if there is none. |
| 75 | func (this *Symbols) ExternalFunction(id string) string { |
| 76 | if extFunc, exist := this.importFuncMap[id]; exist { |
| 77 | return extFunc |
| 78 | } |
| 79 | return "" |
| 80 | } |
| 81 | |
| 82 | func (this *Symbols) ImportType(id string) int { |
| 83 | if typ, exist := this.importIdMap[id]; exist { |
| 84 | return typ |
| 85 | } |
| 86 | return -1 |
| 87 | } |
| 88 | |
| 89 | func (this *Symbols) List() []string { |
| 90 | return this.typeMap |
| 91 | } |
| 92 | |
| 93 | func (this *Symbols) NumSymbols() int { |
| 94 | return len(this.typeMap) |
| 95 | } |
| 96 | |
| 97 | func (this *Symbols) Type(id string) int { |
| 98 | return this.idMap[id] |
| 99 | } |
| 100 | |
| 101 | func (this *Symbols) IsImport(id string) bool { |
| 102 | if _, isImport := this.importFuncMap[id]; isImport { |
| 103 | return true |
| 104 | } |
| 105 | return false |
| 106 | } |
| 107 | |
| 108 | func (this *Symbols) Visit(n ast.LexNode) ast.LexNodeVisitor { |
| 109 | switch node := n.(type) { |
| 110 | case *ast.LexCharLit: |
| 111 | this.CharLitSymbols.Add(node) |
| 112 | case *ast.LexCharRange: |
| 113 | this.CharRangeSymbols.Add(node) |
| 114 | } |
| 115 | return this |
| 116 | } |
| 117 | |
| 118 | func (this *Symbols) String() string { |
| 119 | buf := new(strings.Builder) |
| 120 | fmt.Fprintf(buf, "Lexical Symbols = { ") |
| 121 | for i, sym := range this.List() { |
| 122 | if i > 0 { |
| 123 | fmt.Fprintf(buf, ", %s", sym) |
| 124 | } else { |
| 125 | fmt.Fprintf(buf, "%s", sym) |
| 126 | } |
| 127 | } |
| 128 | fmt.Fprintf(buf, " }") |
| 129 | return buf.String() |
| 130 | } |