| 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 items |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | |
| 21 | "github.com/goccmack/gocc/internal/ast" |
| 22 | "github.com/goccmack/gocc/internal/lexer/symbols" |
| 23 | ) |
| 24 | |
| 25 | // Each Itemset element is a ItemList. |
| 26 | type ItemList []*Item |
| 27 | |
| 28 | func NewItemList(len int) ItemList { |
| 29 | if len < 8 { |
| 30 | len = 8 |
| 31 | } |
| 32 | return make(ItemList, 0, len) |
| 33 | } |
| 34 | |
| 35 | func (this ItemList) AddExclusive(item *Item) (ItemList, error) { |
| 36 | if this.Contain(item) { |
| 37 | return nil, fmt.Errorf("duplicate item: %s in set:\n%s", item.String(), this.PrefixString("\t")) |
| 38 | } |
| 39 | return append(this, item), nil |
| 40 | } |
| 41 | |
| 42 | // AddNoDuplicate will add any of the listed items that are not already in the list. |
| 43 | func (this ItemList) AddNoDuplicate(items ...*Item) ItemList { |
| 44 | newList := this |
| 45 | for _, item := range items { |
| 46 | if !newList.Contain(item) { |
| 47 | newList = append(newList, item) |
| 48 | } |
| 49 | } |
| 50 | return newList |
| 51 | } |
| 52 | |
| 53 | // See Algorithm: set.Closure() in package doc. |
| 54 | func (this ItemList) Closure(lexPart *ast.LexPart, symbols *symbols.Symbols) ItemList { |
| 55 | closure := this |
| 56 | for i := 0; i < len(closure); i++ { |
| 57 | expSym := closure[i].ExpectedSymbol() |
| 58 | if regDefId, isRegDefId := expSym.(*ast.LexRegDefId); isRegDefId { |
| 59 | if !this.ContainShift(expSym.String()) && !symbols.IsImport(regDefId.Id) { |
| 60 | closure = closure.AddNoDuplicate(NewItem(regDefId.Id, lexPart, symbols).Emoves()...) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | return closure |
| 65 | } |
| 66 | |
| 67 | func (this ItemList) Contain(that *Item) bool { |
| 68 | for _, item := range this { |
| 69 | if item.Equal(that) { |
| 70 | return true |
| 71 | } |
| 72 | } |
| 73 | return false |
| 74 | } |
| 75 | |
| 76 | func (this ItemList) ContainShift(id string) bool { |
| 77 | for _, item := range this { |
| 78 | if item.Id == id && !item.Reduce() { |
| 79 | return true |
| 80 | } |
| 81 | } |
| 82 | return false |
| 83 | } |
| 84 | |
| 85 | func (this ItemList) Equal(that ItemList) bool { |
| 86 | if len(this) != len(that) { |
| 87 | return false |
| 88 | } |
| 89 | for _, item := range this { |
| 90 | if !that.Contain(item) { |
| 91 | return false |
| 92 | } |
| 93 | } |
| 94 | return true |
| 95 | } |
| 96 | |
| 97 | func (this ItemList) indexOf(that *Item) int { |
| 98 | for i, item := range this { |
| 99 | if item.Equal(that) { |
| 100 | return i |
| 101 | } |
| 102 | } |
| 103 | return -1 |
| 104 | } |
| 105 | |
| 106 | func (this ItemList) Remove(item *Item) ItemList { |
| 107 | idx := this.indexOf(item) |
| 108 | if idx == -1 { |
| 109 | panic(fmt.Sprintf("Cannot find item: %s", item.String())) |
| 110 | } |
| 111 | if idx < len(this)-1 { |
| 112 | copy(this[idx:], this[idx+1:]) |
| 113 | } |
| 114 | return this[:len(this)-1] |
| 115 | } |
| 116 | |
| 117 | func (this ItemList) PrefixString(prefix string) string { |
| 118 | buf := new(strings.Builder) |
| 119 | for _, item := range this { |
| 120 | fmt.Fprintf(buf, "%s%s\n", prefix, item.String()) |
| 121 | } |
| 122 | return buf.String() |
| 123 | } |