| 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 | /* |
| 26 | Set of basic lexical items |
| 27 | Each entry of Transitions is the next set for the corresponding symbo class. |
| 28 | len(ItemSet.Transitions) == ItemSet.SymbolClasses.Size() |
| 29 | */ |
| 30 | type ItemSet struct { |
| 31 | setNo int |
| 32 | Items ItemList |
| 33 | lexPart *ast.LexPart |
| 34 | *symbols.Symbols |
| 35 | SymbolClasses *DisjunctRangeSet |
| 36 | Transitions []int |
| 37 | ImportTransitions []int |
| 38 | DotTransition int |
| 39 | } |
| 40 | |
| 41 | func NewItemSet(setNo int, lexPart *ast.LexPart, symbols *symbols.Symbols, items ItemList) *ItemSet { |
| 42 | set := &ItemSet{ |
| 43 | setNo: setNo, |
| 44 | Items: items.Closure(lexPart, symbols), |
| 45 | lexPart: lexPart, |
| 46 | Symbols: symbols, |
| 47 | DotTransition: -1, |
| 48 | } |
| 49 | set.getSymbolClasses() |
| 50 | set.newTransitions() |
| 51 | return set |
| 52 | } |
| 53 | |
| 54 | func ItemsSet0(lexPart *ast.LexPart, symbols *symbols.Symbols) ItemList { |
| 55 | items := NewItemList(16) |
| 56 | for _, p := range lexPart.ProdList.Productions { |
| 57 | switch p.(type) { |
| 58 | case *ast.LexTokDef, *ast.LexIgnoredTokDef: |
| 59 | //TODO: shouldn't .Emoves() be inside NewItem? |
| 60 | items = items.AddNoDuplicate(NewItem(p.Id(), lexPart, symbols).Emoves()...) |
| 61 | } |
| 62 | } |
| 63 | return items |
| 64 | } |
| 65 | |
| 66 | func (this *ItemSet) Action() Action { |
| 67 | actionItem := (*Item)(nil) |
| 68 | for _, item := range this.Items { |
| 69 | if !item.Prod.RegDef() && item.Reduce() { |
| 70 | if actionItem == nil || |
| 71 | this.lexPart.StringLitTokDef(item.Id) != nil || |
| 72 | (this.lexPart.StringLitTokDef(actionItem.Id) == nil && item.ProdIndex < actionItem.ProdIndex) { |
| 73 | actionItem = item |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | if actionItem == nil { |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | switch actionItem.Prod.(type) { |
| 82 | case *ast.LexTokDef: |
| 83 | return Accept(actionItem.Id) |
| 84 | case *ast.LexIgnoredTokDef: |
| 85 | return Ignore(actionItem.Id) |
| 86 | } |
| 87 | |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | // Add will return the number of deduplicated items added to the ItemSet. |
| 92 | func (this *ItemSet) Add(items ...*Item) { |
| 93 | this.Items.AddNoDuplicate(items...) |
| 94 | } |
| 95 | |
| 96 | func (this *ItemSet) Contain(that *Item) bool { |
| 97 | return this.Items.Contain(that) |
| 98 | } |
| 99 | |
| 100 | func (this *ItemSet) Equal(items ItemList) bool { |
| 101 | return this.Items.Equal(items) |
| 102 | } |
| 103 | |
| 104 | func (this *ItemSet) Empty() bool { |
| 105 | return len(this.Items) == 0 |
| 106 | } |
| 107 | |
| 108 | func (this *ItemSet) getSymbolClasses() { |
| 109 | this.SymbolClasses = NewDisjunctRangeSet() |
| 110 | for _, item := range this.Items { |
| 111 | if !item.Reduce() { |
| 112 | this.SymbolClasses.AddLexTNode(item.ExpectedSymbol()) |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func (this *ItemSet) newTransitions() { |
| 118 | this.Transitions = make([]int, this.SymbolClasses.Size()) |
| 119 | for i := range this.Transitions { |
| 120 | this.Transitions[i] = -1 |
| 121 | } |
| 122 | this.ImportTransitions = make([]int, len(this.Symbols.ImportIdList)) |
| 123 | for i := range this.ImportTransitions { |
| 124 | this.ImportTransitions[i] = -1 |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // See algorithm: set.Next() in package doc. |
| 129 | func (this *ItemSet) Next(rng CharRange) ItemList { |
| 130 | // fmt.Printf("S%d%s\n", this.setNo, this) |
| 131 | nextItems := NewItemList(16) |
| 132 | for _, item := range this.Items { |
| 133 | nextItems = nextItems.AddNoDuplicate(item.Move(rng)...) |
| 134 | } |
| 135 | nextItems = this.dependentsClosure(nextItems) |
| 136 | return nextItems.Closure(this.lexPart, this.Symbols) |
| 137 | } |
| 138 | |
| 139 | func (this *ItemSet) NextDot() ItemList { |
| 140 | nextItems := NewItemList(16) |
| 141 | for _, item := range this.Items { |
| 142 | nextItems = nextItems.AddNoDuplicate(item.MoveDot()...) |
| 143 | } |
| 144 | nextItems = this.dependentsClosure(nextItems) |
| 145 | return nextItems.Closure(this.lexPart, this.Symbols) |
| 146 | } |
| 147 | |
| 148 | func (this *ItemSet) NextImport(imprt string) ItemList { |
| 149 | nextItems := NewItemList(16) |
| 150 | for _, item := range this.Items { |
| 151 | nextItems = nextItems.AddNoDuplicate(item.MoveRegDefId(imprt)...) |
| 152 | } |
| 153 | nextItems = this.dependentsClosure(nextItems) |
| 154 | return nextItems.Closure(this.lexPart, this.Symbols) |
| 155 | } |
| 156 | |
| 157 | func (this *ItemSet) dependentsClosure(items ItemList) ItemList { |
| 158 | if len(items) == 0 { |
| 159 | return items |
| 160 | } |
| 161 | // fmt.Printf("dependentsClosure S%d, %s\n", this.setNo, items) |
| 162 | for i := 0; i < len(items); i++ { |
| 163 | for _, thisItem := range this.Items { |
| 164 | if expSym := thisItem.ExpectedSymbol(); expSym != nil && expSym.String() == items[i].Id { |
| 165 | if items[i].Reduce() { |
| 166 | // mv := thisItem.MoveRegDefId(items[i].Id) |
| 167 | // for _, mvi := range mv { |
| 168 | // fmt.Printf("\t%s\n", mvi) |
| 169 | // } |
| 170 | items = items.AddNoDuplicate(thisItem.MoveRegDefId(items[i].Id)...) |
| 171 | } else { |
| 172 | // fmt.Printf("\t%s\n", thisItem) |
| 173 | items = items.AddNoDuplicate(thisItem) |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | return items |
| 179 | } |
| 180 | |
| 181 | func (this *ItemSet) Size() int { |
| 182 | return len(this.Items) |
| 183 | } |
| 184 | |
| 185 | func (this *ItemSet) String() string { |
| 186 | buf := new(strings.Builder) |
| 187 | fmt.Fprintf(buf, "{\n") |
| 188 | for _, i := range this.Items { |
| 189 | fmt.Fprintf(buf, "\t%s\n", i) |
| 190 | } |
| 191 | fmt.Fprintf(buf, "}\n") |
| 192 | fmt.Fprintf(buf, "Transitions:\n") |
| 193 | for rng, setNo := range this.Transitions { |
| 194 | fmt.Fprintf(buf, "\t%s -> S%d\n", this.SymbolClasses.Range(rng), setNo) |
| 195 | } |
| 196 | fmt.Fprintf(buf, "Symbols classes: %s\n", this.SymbolClasses) |
| 197 | return buf.String() |
| 198 | } |
| 199 | |
| 200 | func (this *ItemSet) StringItems() string { |
| 201 | buf := new(strings.Builder) |
| 202 | fmt.Fprintf(buf, "{\n") |
| 203 | for _, i := range this.Items { |
| 204 | fmt.Fprintf(buf, "\t%s\n", i) |
| 205 | } |
| 206 | fmt.Fprintf(buf, "}\n") |
| 207 | return buf.String() |
| 208 | } |