| 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/parser/lr1/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/parser/firstscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/" |
| 23 | "github.com/goccmack/gocc/internal/parser/symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/symbols`/" |
| 24 | ) |
| 25 | |
| 26 | // A list of a list of Items. |
| 27 | type ItemSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets# struct { |
| 28 | setsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#sets. []*ItemSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet# |
| 29 | } |
| 30 | |
| 31 | // TODO: optimise loop |
| 32 | // g is a BNF grammar. Items returns the sets of Items of the grammar g. |
| 33 | func GetItemSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/GetItemSets().(g *astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/.Grammarscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/Grammar#, s *symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/symbols`/.Symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/symbols`/Symbols#, firstSets *firstscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/.FirstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#) *ItemSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets# { |
| 34 | S := &ItemSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#{ |
| 35 | setsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#sets.: []*ItemSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet#{InitialItemSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/InitialItemSet().(g, s, firstSets).Closurescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet#Closure().()}, |
| 36 | } |
| 37 | symbols := s.Listscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/symbols`/Symbols#List().() |
| 38 | included := -1 |
| 39 | for again := true; again; { |
| 40 | again = false |
| 41 | for i, I := range S.setsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#sets. { |
| 42 | if i > included { |
| 43 | for _, X := range symbols { |
| 44 | gto := I.Gotoscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet#Goto().(X) |
| 45 | if gto.Sizescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet#Size().() > 0 { |
| 46 | idx := S.GetIndexscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#GetIndex().(gto) |
| 47 | if idx == -1 { |
| 48 | S.setsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#sets., again = append(S.setsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#sets., gto), true |
| 49 | idx = len(S.setsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#sets.) - 1 |
| 50 | gto.SetNoscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet#SetNo. = idx |
| 51 | } |
| 52 | I.AddTransitionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet#AddTransition().(X, idx) |
| 53 | } |
| 54 | } |
| 55 | included = i |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | return S |
| 60 | } |
| 61 | |
| 62 | // Returns whether the list of a list of items contains the list of items. |
| 63 | func (this *ItemSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#) Containsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#Contains().(I *ItemSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet#) bool { |
| 64 | for _, i := range this.setsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#sets. { |
| 65 | if i.Equalscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet#Equal().(I) { |
| 66 | return true |
| 67 | } |
| 68 | } |
| 69 | return false |
| 70 | } |
| 71 | |
| 72 | // Returns the index of the list of items. |
| 73 | func (this *ItemSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#) GetIndexscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#GetIndex().(I *ItemSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet#) int { |
| 74 | if I == nil || I.Sizescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet#Size().() == 0 { |
| 75 | return -1 |
| 76 | } |
| 77 | |
| 78 | for i, items := range this.setsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#sets. { |
| 79 | if items.Equalscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet#Equal().(I) { |
| 80 | return i |
| 81 | } |
| 82 | } |
| 83 | return -1 |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | Return a slice containing all the item sets in increasing order of index |
| 88 | */ |
| 89 | func (this *ItemSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#) Listscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#List().() []*ItemSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet# { |
| 90 | return this.setsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#sets. |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | return set[SetNo] |
| 95 | */ |
| 96 | func (this *ItemSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#) Setscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#Set().(SetNo int) *ItemSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet# { |
| 97 | return this.setsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#sets.[SetNo] |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | Returns the number of item sets |
| 102 | */ |
| 103 | func (this *ItemSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#) Sizescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#Size().() int { |
| 104 | return len(this.setsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#sets.) |
| 105 | } |
| 106 | |
| 107 | // Returns a string representing the list of the list of items. |
| 108 | func (this *ItemSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#) Stringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#String().() string { |
| 109 | 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#) |
| 110 | for i, is := range this.setsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSets#sets. { |
| 111 | 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%d%s\n", i, is.Stringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet#String().()) |
| 112 | } |
| 113 | return buf.Stringscip-go gomod github.com/golang/go/src go1.24 strings/Builder#String().() |
| 114 | } |
| 115 | |
| 116 | // Returns the initial Item of a Grammar. |
| 117 | func InitialItemSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/InitialItemSet().(g *astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/.Grammarscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/Grammar#, symbols *symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/symbols`/.Symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/symbols`/Symbols#, fs *firstscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/.FirstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#) *ItemSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet# { |
| 118 | set := NewItemSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/NewItemSet().(symbols, g.SyntaxPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/Grammar#SyntaxPart..ProdListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxPart#ProdList., fs) |
| 119 | set.SetNoscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet#SetNo. = 0 |
| 120 | prod := g.SyntaxPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/Grammar#SyntaxPart..ProdListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxPart#ProdList.[0] |
| 121 | set.AddItemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ItemSet#AddItem().(NewItemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/NewItem().(0, prod, 0, "␚")) |
| 122 | return set |
| 123 | } |