Debian Codegraph @

internal/ast/lexprodmap.go

raw symbols SCIP
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 )
20
21 type LexProdMap struct {
22 //key: production id
23 idMap map[string]LexProdIndex
24
25 //value: lex production id
26 idxMap map[LexProdIndex]string
27 }
28
29 type LexProdIndex int
30
31 func NewLexProdMap(prodList *LexProductions) *LexProdMap {
32 lpm := &LexProdMap{
33 idMap: make(map[string]LexProdIndex),
34 idxMap: make(map[LexProdIndex]string),
35 }
36 lpm.Add(prodList.Productions...)
37
38 return lpm
39 }
40
41 func newLexProdMap() *LexProdMap {
42 return &LexProdMap{
43 idMap: make(map[string]LexProdIndex),
44 idxMap: make(map[LexProdIndex]string),
45 }
46 }
47
48 func (this *LexProdMap) Index(id string) LexProdIndex {
49 idx, exist := this.idMap[id]
50 if exist {
51 return idx
52 }
53 return -1
54 }
55
56 func (this *LexProdMap) Id(index LexProdIndex) string {
57 id, exist := this.idxMap[index]
58 if exist {
59 return id
60 }
61 return ""
62 }
63
64 func (this *LexProdMap) Add(prods ...LexProduction) {
65 for _, prod := range prods {
66 if _, exist := this.idMap[prod.Id()]; exist {
67 panic(fmt.Sprintf("Production %s already exists", prod.Id()))
68 }
69 idx := len(this.idxMap)
70 this.idMap[prod.Id()] = LexProdIndex(idx)
71 this.idxMap[LexProdIndex(idx)] = prod.Id()
72 }
73 }