Debian Codegraph @

internal/parser/lr1/items/itemsets.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 items
16
17 import (
18 "fmt"
19 "strings"
20
21 "github.com/goccmack/gocc/internal/ast"
22 "github.com/goccmack/gocc/internal/parser/first"
23 "github.com/goccmack/gocc/internal/parser/symbols"
24 )
25
26 // A list of a list of Items.
27 type ItemSets struct {
28 sets []*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 GetItemSets(g *ast.Grammar, s *symbols.Symbols, firstSets *first.FirstSets) *ItemSets {
34 S := &ItemSets{
35 sets: []*ItemSet{InitialItemSet(g, s, firstSets).Closure()},
36 }
37 symbols := s.List()
38 included := -1
39 for again := true; again; {
40 again = false
41 for i, I := range S.sets {
42 if i > included {
43 for _, X := range symbols {
44 gto := I.Goto(X)
45 if gto.Size() > 0 {
46 idx := S.GetIndex(gto)
47 if idx == -1 {
48 S.sets, again = append(S.sets, gto), true
49 idx = len(S.sets) - 1
50 gto.SetNo = idx
51 }
52 I.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 *ItemSets) Contains(I *ItemSet) bool {
64 for _, i := range this.sets {
65 if i.Equal(I) {
66 return true
67 }
68 }
69 return false
70 }
71
72 // Returns the index of the list of items.
73 func (this *ItemSets) GetIndex(I *ItemSet) int {
74 if I == nil || I.Size() == 0 {
75 return -1
76 }
77
78 for i, items := range this.sets {
79 if items.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 *ItemSets) List() []*ItemSet {
90 return this.sets
91 }
92
93 /*
94 return set[SetNo]
95 */
96 func (this *ItemSets) Set(SetNo int) *ItemSet {
97 return this.sets[SetNo]
98 }
99
100 /*
101 Returns the number of item sets
102 */
103 func (this *ItemSets) Size() int {
104 return len(this.sets)
105 }
106
107 // Returns a string representing the list of the list of items.
108 func (this *ItemSets) String() string {
109 buf := new(strings.Builder)
110 for i, is := range this.sets {
111 fmt.Fprintf(buf, "S%d%s\n", i, is.String())
112 }
113 return buf.String()
114 }
115
116 // Returns the initial Item of a Grammar.
117 func InitialItemSet(g *ast.Grammar, symbols *symbols.Symbols, fs *first.FirstSets) *ItemSet {
118 set := NewItemSet(symbols, g.SyntaxPart.ProdList, fs)
119 set.SetNo = 0
120 prod := g.SyntaxPart.ProdList[0]
121 set.AddItem(NewItem(0, prod, 0, "␚"))
122 return set
123 }