| 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 itemsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24 fmt/" |
| 19 | "stringsscip-go gomod github.com/golang/go/src go1.24 strings/" |
| 20 | |
| 21 | "github.com/goccmack/gocc/internal/astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/" |
| 22 | "github.com/goccmack/gocc/internal/lexer/symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/symbols`/" |
| 23 | ) |
| 24 | |
| 25 | // Each Itemset element is a ItemList. |
| 26 | type ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList# []*Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/Item# |
| 27 | |
| 28 | func NewItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/NewItemList().(len int) ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList# { |
| 29 | if len < 8 { |
| 30 | len = 8 |
| 31 | } |
| 32 | return make(ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#, 0, len) |
| 33 | } |
| 34 | |
| 35 | func (this ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#) AddExclusivescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#AddExclusive().(item *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/Item#) (ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#, error) { |
| 36 | if this.Containscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#Contain().(item) { |
| 37 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24 fmt/Errorf().("duplicate item: %s in set:\n%s", item.Stringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/Item#String().(), this.PrefixStringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#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 ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#) AddNoDuplicatescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#AddNoDuplicate().(items ...*Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/Item#) ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList# { |
| 44 | newList := this |
| 45 | for _, item := range items { |
| 46 | if !newList.Containscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#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 ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#) Closurescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#Closure().(lexPart *astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/.LexPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#, symbols *symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/symbols`/.Symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/symbols`/Symbols#) ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList# { |
| 55 | closure := this |
| 56 | for i := 0; i < len(closure); i++ { |
| 57 | expSym := closure[i].ExpectedSymbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/Item#ExpectedSymbol().() |
| 58 | if regDefId, isRegDefId := expSym.(*astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/.LexRegDefIdscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexRegDefId#); isRegDefId { |
| 59 | if !this.ContainShiftscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#ContainShift().(expSym.Stringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexNode#String.()) && !symbols.IsImportscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/symbols`/Symbols#IsImport().(regDefId.Idscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexRegDefId#Id.) { |
| 60 | closure = closure.AddNoDuplicatescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#AddNoDuplicate().(NewItemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/NewItem().(regDefId.Idscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexRegDefId#Id., lexPart, symbols).Emovesscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/Item#Emoves().()...) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | return closure |
| 65 | } |
| 66 | |
| 67 | func (this ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#) Containscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#Contain().(that *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/Item#) bool { |
| 68 | for _, item := range this { |
| 69 | if item.Equalscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/Item#Equal().(that) { |
| 70 | return true |
| 71 | } |
| 72 | } |
| 73 | return false |
| 74 | } |
| 75 | |
| 76 | func (this ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#) ContainShiftscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#ContainShift().(id string) bool { |
| 77 | for _, item := range this { |
| 78 | if item.Idscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/Item#Id. == id && !item.Reducescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/Item#Reduce().() { |
| 79 | return true |
| 80 | } |
| 81 | } |
| 82 | return false |
| 83 | } |
| 84 | |
| 85 | func (this ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#) Equalscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#Equal().(that ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#) bool { |
| 86 | if len(this) != len(that) { |
| 87 | return false |
| 88 | } |
| 89 | for _, item := range this { |
| 90 | if !that.Containscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#Contain().(item) { |
| 91 | return false |
| 92 | } |
| 93 | } |
| 94 | return true |
| 95 | } |
| 96 | |
| 97 | func (this ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#) indexOfscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#indexOf().(that *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/Item#) int { |
| 98 | for i, item := range this { |
| 99 | if item.Equalscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/Item#Equal().(that) { |
| 100 | return i |
| 101 | } |
| 102 | } |
| 103 | return -1 |
| 104 | } |
| 105 | |
| 106 | func (this ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#) Removescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#Remove().(item *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/Item#) ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList# { |
| 107 | idx := this.indexOfscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#indexOf().(item) |
| 108 | if idx == -1 { |
| 109 | panic(fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Sprintf().("Cannot find item: %s", item.Stringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/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 ItemListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#) PrefixStringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/ItemList#PrefixString().(prefix string) string { |
| 118 | buf := new(stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Builderscip-go gomod github.com/golang/go/src go1.24 strings/Builder#) |
| 119 | for _, item := range this { |
| 120 | fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(buf, "%s%s\n", prefix, item.Stringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/lexer/items`/Item#String().()) |
| 121 | } |
| 122 | return buf.Stringscip-go gomod github.com/golang/go/src go1.24 strings/Builder#String().() |
| 123 | } |