| 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 | "sort" |
| 20 | "strings" |
| 21 | |
| 22 | "github.com/goccmack/gocc/internal/ast" |
| 23 | "github.com/goccmack/gocc/internal/parser/first" |
| 24 | "github.com/goccmack/gocc/internal/parser/lr1/action" |
| 25 | "github.com/goccmack/gocc/internal/parser/symbols" |
| 26 | ) |
| 27 | |
| 28 | type ItemSet struct { |
| 29 | SetNo int |
| 30 | imap map[string]*Item |
| 31 | Items []*Item |
| 32 | // string: symbol, int: nextState. |
| 33 | Transitions map[string]int |
| 34 | |
| 35 | Symbols *symbols.Symbols |
| 36 | Prods ast.SyntaxProdList |
| 37 | FS *first.FirstSets |
| 38 | } |
| 39 | |
| 40 | // NewItemSet returns a newly cosntructed set of items. |
| 41 | func NewItemSet(symbols *symbols.Symbols, prods ast.SyntaxProdList, fs *first.FirstSets) *ItemSet { |
| 42 | return &ItemSet{ |
| 43 | SetNo: -1, |
| 44 | imap: make(map[string]*Item), |
| 45 | Items: make([]*Item, 0, 16), |
| 46 | Transitions: make(map[string]int), |
| 47 | Symbols: symbols, |
| 48 | Prods: prods, |
| 49 | FS: fs, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func (this *ItemSet) Action(symbol string) (act1 action.Action, conflicts []action.Action) { |
| 54 | conflictMap := make(map[string]action.Action) |
| 55 | act1 = action.ERROR |
| 56 | for _, item := range this.Items { |
| 57 | act2 := item.action(symbol, this.Transitions[symbol]) |
| 58 | switch { |
| 59 | case act2.Equal(action.ERROR): |
| 60 | // ignore |
| 61 | case act1.Equal(action.ERROR): |
| 62 | act1 = act2 |
| 63 | case !act1.Equal(act2): |
| 64 | conflictMap[act1.String()] = act1 |
| 65 | conflictMap[act2.String()] = act2 |
| 66 | act1 = act1.ResolveConflict(act2) |
| 67 | default: |
| 68 | // act1 == act2. Do nothing |
| 69 | } |
| 70 | } |
| 71 | for _, act := range conflictMap { |
| 72 | conflicts = append(conflicts, act) |
| 73 | } |
| 74 | |
| 75 | return |
| 76 | } |
| 77 | |
| 78 | func (this *ItemSet) AddItem(items ...*Item) { |
| 79 | for _, i := range items { |
| 80 | if _, contain := this.imap[i.str]; !contain { |
| 81 | this.imap[i.str] = i |
| 82 | this.Items = append(this.Items, i) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func (this *ItemSet) AddTransition(symbol string, nextSet int) { |
| 88 | if _, exist := this.Transitions[symbol]; exist { |
| 89 | panic(fmt.Sprintf("Transition %s -> %d already exists", symbol, nextSet)) |
| 90 | } |
| 91 | this.Transitions[symbol] = nextSet |
| 92 | } |
| 93 | |
| 94 | func (this *ItemSet) CanRecover() bool { |
| 95 | for _, item := range this.Items { |
| 96 | if item.canRecover() { |
| 97 | return true |
| 98 | } |
| 99 | } |
| 100 | return false |
| 101 | } |
| 102 | |
| 103 | func (this *ItemSet) NextSetIndex(symbol string) int { |
| 104 | if nextSet, exist := this.Transitions[symbol]; exist { |
| 105 | return nextSet |
| 106 | } |
| 107 | return -1 |
| 108 | } |
| 109 | |
| 110 | //TODO: optimise loop |
| 111 | /* |
| 112 | Dragon book, 2nd ed, section 4.7.2, p261 |
| 113 | Closure(I) |
| 114 | repeat |
| 115 | for each item [A->x•By, a] in I |
| 116 | for each production B -> z in G' |
| 117 | for each terminal b in FIRST(ya) |
| 118 | add [B -> •z, b] to I |
| 119 | until no more items are added to I |
| 120 | */ |
| 121 | func (this *ItemSet) Closure() (c *ItemSet) { |
| 122 | if this.Size() == 0 { |
| 123 | return NewItemSet(this.Symbols, this.Prods, this.FS) |
| 124 | } |
| 125 | c = NewItemSet(this.Symbols, this.Prods, this.FS) |
| 126 | c.AddItem(this.Items...) |
| 127 | included := -1 |
| 128 | for again := true; again; { |
| 129 | again = false |
| 130 | for idx, i := range c.Items { |
| 131 | if idx > included { |
| 132 | if i.Pos >= i.Len || this.Symbols.IsTerminal(i.ExpectedSymbol) { |
| 133 | continue |
| 134 | } |
| 135 | for pi, prod := range this.Prods { |
| 136 | if prod.Id == i.ExpectedSymbol { |
| 137 | first := first1(this.FS, i.Body[i.Pos+1:], i.FollowingSymbol) |
| 138 | for _, t := range first { |
| 139 | if item := NewItem(pi, prod, 0, t); !c.Contain(item) { |
| 140 | c.AddItem(item) |
| 141 | again = true |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | included = idx |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | return |
| 152 | } |
| 153 | |
| 154 | func (this *ItemSet) Contain(item *Item) bool { |
| 155 | if _, contain := this.imap[item.str]; contain { |
| 156 | return true |
| 157 | } |
| 158 | return false |
| 159 | } |
| 160 | |
| 161 | func (this *ItemSet) ContainString(item string) bool { |
| 162 | if _, contain := this.imap[item]; contain { |
| 163 | return true |
| 164 | } |
| 165 | return false |
| 166 | } |
| 167 | |
| 168 | // Equal will return whether two lists of Items are equal. |
| 169 | func (this *ItemSet) Equal(that *ItemSet) bool { |
| 170 | if that == nil || len(this.Items) != len(that.Items) { |
| 171 | return false |
| 172 | } |
| 173 | for k := range this.imap { |
| 174 | if _, contain := that.imap[k]; !contain { |
| 175 | return false |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return true |
| 180 | } |
| 181 | |
| 182 | // first1 returns the characters contained within the first set, sorted in |
| 183 | // alphabetical order. |
| 184 | func first1(firstSets *first.FirstSets, symbols []string, following string) []string { |
| 185 | firsts := first.FirstS(firstSets, append(symbols, following)) |
| 186 | keys := make([]string, 0, len(firsts)) |
| 187 | for key := range firsts { |
| 188 | keys = append(keys, key) |
| 189 | } |
| 190 | sort.Strings(keys) |
| 191 | return keys |
| 192 | } |
| 193 | |
| 194 | // Goto implements Dragon book, 2nd ed, section 4.7.2, p261. |
| 195 | func (I *ItemSet) Goto(X string) *ItemSet { |
| 196 | J := NewItemSet(I.Symbols, I.Prods, I.FS) |
| 197 | for _, item := range I.Items { |
| 198 | if item.Pos < item.Len && X == item.ExpectedSymbol { |
| 199 | nextItem := item.Move() |
| 200 | J.AddItem(nextItem) |
| 201 | } |
| 202 | } |
| 203 | if J.Size() > 0 { |
| 204 | J = J.Closure() |
| 205 | } |
| 206 | return J |
| 207 | } |
| 208 | |
| 209 | // Size returns the number of items in the set. |
| 210 | func (this *ItemSet) Size() int { |
| 211 | return len(this.Items) |
| 212 | } |
| 213 | |
| 214 | func (this *ItemSet) String() string { |
| 215 | buf := new(strings.Builder) |
| 216 | fmt.Fprintf(buf, "{\n") |
| 217 | for _, item := range this.Items { |
| 218 | fmt.Fprintf(buf, "\t%s\n", item) |
| 219 | } |
| 220 | fmt.Fprintf(buf, "}\n") |
| 221 | fmt.Fprintf(buf, "Transitions:\n") |
| 222 | var keys transitions |
| 223 | for sym, set := range this.Transitions { |
| 224 | key := transition{symbol: sym, nextState: set} |
| 225 | keys = append(keys, key) |
| 226 | } |
| 227 | sort.Sort(keys) |
| 228 | for _, key := range keys { |
| 229 | sym, set := key.symbol, key.nextState |
| 230 | fmt.Fprintf(buf, "\t%s -> %d\n", sym, set) |
| 231 | } |
| 232 | fmt.Fprintf(buf, "\n") |
| 233 | return buf.String() |
| 234 | } |
| 235 | |
| 236 | // transitions implements the sort.Sort interface, sorting transitions in |
| 237 | // ascending order based on the next state. |
| 238 | type transitions []transition |
| 239 | |
| 240 | func (ts transitions) Len() int { return len(ts) } |
| 241 | func (ts transitions) Swap(i, j int) { ts[i], ts[j] = ts[j], ts[i] } |
| 242 | func (ts transitions) Less(i, j int) bool { return ts[i].nextState < ts[j].nextState } |
| 243 | |
| 244 | // A transition represents a transition from a symbol to a given state. |
| 245 | type transition struct { |
| 246 | symbol string |
| 247 | nextState int |
| 248 | } |