| 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 ast |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "sort" |
| 20 | "strings" |
| 21 | ) |
| 22 | |
| 23 | // All maps are indexed by production id |
| 24 | type LexPart struct { |
| 25 | Header *FileHeader |
| 26 | *LexImports |
| 27 | TokDefsList []*LexTokDef |
| 28 | TokDefs map[string]*LexTokDef |
| 29 | stringLitToks map[string]*LexTokDef |
| 30 | RegDefsList []*LexRegDef |
| 31 | RegDefs map[string]*LexRegDef |
| 32 | IgnoredTokDefsList []*LexIgnoredTokDef |
| 33 | IgnoredTokDefs map[string]*LexIgnoredTokDef |
| 34 | ProdList *LexProductions |
| 35 | ProdMap *LexProdMap |
| 36 | } |
| 37 | |
| 38 | func NewLexPart(header, imports, prodList interface{}) (*LexPart, error) { |
| 39 | lexPart := &LexPart{ |
| 40 | TokDefsList: make([]*LexTokDef, 0, 16), |
| 41 | TokDefs: make(map[string]*LexTokDef, 16), |
| 42 | stringLitToks: make(map[string]*LexTokDef, 16), |
| 43 | RegDefsList: make([]*LexRegDef, 0, 16), |
| 44 | RegDefs: make(map[string]*LexRegDef, 16), |
| 45 | IgnoredTokDefsList: make([]*LexIgnoredTokDef, 0, 16), |
| 46 | IgnoredTokDefs: make(map[string]*LexIgnoredTokDef, 16), |
| 47 | } |
| 48 | if header != nil { |
| 49 | lexPart.Header = header.(*FileHeader) |
| 50 | } else { |
| 51 | lexPart.Header = new(FileHeader) |
| 52 | } |
| 53 | if imports != nil { |
| 54 | lexPart.LexImports = imports.(*LexImports) |
| 55 | } else { |
| 56 | lexPart.LexImports = newLexImports() |
| 57 | } |
| 58 | if prodList != nil { |
| 59 | lexPart.ProdList = prodList.(*LexProductions) |
| 60 | lexPart.ProdMap = NewLexProdMap(prodList.(*LexProductions)) |
| 61 | for _, p := range prodList.(*LexProductions).Productions { |
| 62 | pid := p.Id() |
| 63 | |
| 64 | switch p1 := p.(type) { |
| 65 | case *LexTokDef: |
| 66 | //TODO: decide whether to handle in separate semantic check |
| 67 | if _, exist := lexPart.TokDefs[pid]; exist { |
| 68 | return nil, fmt.Errorf("duplicate token def: %s", pid) |
| 69 | } |
| 70 | lexPart.TokDefs[pid] = p1 |
| 71 | lexPart.TokDefsList = append(lexPart.TokDefsList, p1) |
| 72 | case *LexRegDef: |
| 73 | //TODO: decide whether to handle in separate semantic check |
| 74 | if _, exist := lexPart.RegDefs[pid]; exist { |
| 75 | return nil, fmt.Errorf("duplicate token def: %s", pid) |
| 76 | } |
| 77 | lexPart.RegDefs[pid] = p1 |
| 78 | lexPart.RegDefsList = append(lexPart.RegDefsList, p1) |
| 79 | case *LexIgnoredTokDef: |
| 80 | if _, exist := lexPart.IgnoredTokDefs[pid]; exist { |
| 81 | return nil, fmt.Errorf("duplicate ignored token def: %s", pid) |
| 82 | } |
| 83 | lexPart.IgnoredTokDefs[pid] = p1 |
| 84 | lexPart.IgnoredTokDefsList = append(lexPart.IgnoredTokDefsList, p1) |
| 85 | } |
| 86 | } |
| 87 | } else { |
| 88 | lexPart.ProdList = newLexProductions() |
| 89 | lexPart.ProdMap = newLexProdMap() |
| 90 | } |
| 91 | return lexPart, nil |
| 92 | } |
| 93 | |
| 94 | func (this *LexPart) StringLitTokDef(id string) *LexTokDef { |
| 95 | tokDef := this.stringLitToks[id] |
| 96 | return tokDef |
| 97 | } |
| 98 | |
| 99 | func (this *LexPart) Production(id string) LexProduction { |
| 100 | idx, exist := this.ProdMap.idMap[id] |
| 101 | if !exist { |
| 102 | panic(fmt.Sprintf("Unknown production: %s", id)) |
| 103 | } |
| 104 | return this.ProdList.Productions[idx] |
| 105 | } |
| 106 | |
| 107 | func (this *LexPart) ProdIndex(id string) LexProdIndex { |
| 108 | idx, exist := this.ProdMap.idMap[id] |
| 109 | if !exist { |
| 110 | panic(fmt.Sprintf("Unknown production %s", id)) |
| 111 | } |
| 112 | return idx |
| 113 | } |
| 114 | |
| 115 | func (this *LexPart) TokenIds() []string { |
| 116 | tids := make([]string, 0, len(this.TokDefs)) |
| 117 | for tid := range this.TokDefs { |
| 118 | tids = append(tids, tid) |
| 119 | } |
| 120 | sort.Strings(tids) |
| 121 | return tids |
| 122 | } |
| 123 | |
| 124 | func (this *LexPart) UpdateStringLitTokens(tokens []string) { |
| 125 | for _, strLit := range tokens { |
| 126 | tokDef := NewLexStringLitTokDef(strLit) |
| 127 | this.ProdMap.Add(tokDef) |
| 128 | this.TokDefsList = append(this.TokDefsList, tokDef) |
| 129 | this.TokDefs[strLit] = tokDef |
| 130 | this.stringLitToks[strLit] = tokDef |
| 131 | this.ProdList.Productions = append(this.ProdList.Productions, tokDef) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func (this *LexPart) String() string { |
| 136 | buf := new(strings.Builder) |
| 137 | fmt.Fprintf(buf, "LexPart.ProdMap:\n") |
| 138 | if this.ProdList != nil { |
| 139 | for i, p := range this.ProdList.Productions { |
| 140 | fmt.Fprintf(buf, "\t%d: %s\n", i, p) |
| 141 | } |
| 142 | } |
| 143 | fmt.Fprintf(buf, "LexPart.TokDefs:\n") |
| 144 | for t := range this.TokDefs { |
| 145 | fmt.Fprintf(buf, "\t%s\n", t) |
| 146 | } |
| 147 | return buf.String() |
| 148 | } |