Debian Codegraph @

internal/lexer/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/lexer/symbols"
23 )
24
25 type ItemSets struct {
26 sets []*ItemSet
27 lexPart *ast.LexPart
28 symbols *symbols.Symbols
29 }
30
31 func GetItemSets(lexPart *ast.LexPart) *ItemSets {
32 itemSets := &ItemSets{
33 sets: make([]*ItemSet, 0, 256),
34 lexPart: lexPart,
35 symbols: symbols.NewSymbols(lexPart),
36 }
37
38 itemSets.Add(ItemsSet0(lexPart, itemSets.symbols))
39
40 return itemSets.Closure()
41 }
42
43 func (this *ItemSets) Add(items ItemList) (setNo int) {
44 if yes, setNo := this.Contain(items); yes {
45 return setNo
46 }
47 setNo = this.Size()
48 this.sets = append(this.sets, NewItemSet(setNo, this.lexPart, this.symbols, items))
49 return setNo
50 }
51
52 func (this *ItemSets) Closure() *ItemSets {
53 for i := 0; i < len(this.sets); i++ {
54 for symI, rng := range this.sets[i].SymbolClasses.List() {
55 if items := this.sets[i].Next(rng); len(items) != 0 {
56 setNo, nextState := this.Add(items), this.sets[i].Transitions[symI]
57 if nextState != -1 && nextState != setNo {
58 panic(fmt.Sprintf("Next set conflict in (S%d, %s) -> %d. Existing setNo: %d", i, rng, setNo, nextState))
59 }
60 this.sets[i].Transitions[symI] = setNo
61 }
62 }
63 for impI, imprt := range this.symbols.ImportIdList {
64 if items := this.sets[i].NextImport(imprt); len(items) != 0 {
65 setNo, nextState := this.Add(items), this.sets[i].ImportTransitions[this.symbols.ImportType(imprt)]
66 if nextState != -1 && nextState != setNo {
67 panic(fmt.Sprintf("Next set conflict in (S%d, %s) -> %d. Existing setNo: %d", i, imprt, setNo, nextState))
68 }
69 this.sets[i].ImportTransitions[impI] = setNo
70 }
71 }
72 if items := this.sets[i].NextDot(); len(items) != 0 {
73 setNo := this.Add(items)
74 this.sets[i].DotTransition = setNo
75 }
76 }
77 return this
78 }
79
80 func (this *ItemSets) Contain(items ItemList) (yes bool, index int) {
81 for i, thisSet := range this.sets {
82 if thisSet.Equal(items) {
83 return true, i
84 }
85 }
86 return false, -1
87 }
88
89 func (this *ItemSets) Size() int {
90 return len(this.sets)
91 }
92
93 func (this *ItemSets) List() []*ItemSet {
94 return this.sets
95 }
96
97 func (this *ItemSets) Symbols() *symbols.Symbols {
98 return this.symbols
99 }
100
101 func (this *ItemSets) String() string {
102 buf := new(strings.Builder)
103 fmt.Fprintf(buf, "Item sets:\n")
104 for i, s := range this.sets {
105 fmt.Fprintf(buf, "S%d{\n", i)
106 for _, item := range s.Items {
107 fmt.Fprintf(buf, "\t%s\n", item)
108 }
109 fmt.Fprintf(buf, "}\n")
110 fmt.Fprintf(buf, "Transitions:\n")
111 symClasses := s.SymbolClasses.List()
112 for idx, symClass := range symClasses {
113 fmt.Fprintf(buf, "\t%s -> S%d\n", symClass, s.Transitions[idx])
114 }
115 for idx, imprt := range this.symbols.ImportIdList {
116 fmt.Fprintf(buf, "\t%s -> %d\n", imprt, s.ImportTransitions[idx])
117 }
118 if s.SymbolClasses.MatchAny {
119 fmt.Fprintf(buf, ". -> S%d\n", s.DotTransition)
120 }
121 if s.Action() == nil {
122 fmt.Fprintf(buf, "Action: nil\n")
123 } else {
124 fmt.Fprintf(buf, "Action: %s\n", s.Action())
125 }
126 fmt.Fprintf(buf, "Symbols classes: %s\n\n", s.SymbolClasses)
127 }
128 return buf.String()
129 }