| 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 | "io" |
| 20 | "strings" |
| 21 | |
| 22 | "github.com/goccmack/gocc/internal/ast" |
| 23 | "github.com/goccmack/gocc/internal/lexer/symbols" |
| 24 | "github.com/goccmack/gocc/internal/util" |
| 25 | ) |
| 26 | |
| 27 | type Item struct { |
| 28 | Id string |
| 29 | Prod ast.LexProduction |
| 30 | pos *itemPos |
| 31 | ProdIndex ast.LexProdIndex |
| 32 | hashKey string |
| 33 | str string |
| 34 | *symbols.Symbols |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | NewItem will return |
| 39 | |
| 40 | T : s |
| 41 | |
| 42 | for a lex production, |
| 43 | |
| 44 | T : •s |
| 45 | |
| 46 | without executing the ℇ-moves for s. |
| 47 | |
| 48 | Func *Item.Emoves must be called to return the set of basic items for T : •s. |
| 49 | */ |
| 50 | func NewItem(prodId string, lexPart *ast.LexPart, symbols *symbols.Symbols) *Item { |
| 51 | prod := lexPart.Production(prodId) |
| 52 | item := &Item{ |
| 53 | Id: prodId, |
| 54 | Prod: prod, |
| 55 | pos: newItemPos(prod.LexPattern()), |
| 56 | ProdIndex: lexPart.ProdIndex(prodId), |
| 57 | Symbols: symbols, |
| 58 | } |
| 59 | item.getHashKey() |
| 60 | return item |
| 61 | } |
| 62 | |
| 63 | func (this *Item) Clone() *Item { |
| 64 | return &Item{ |
| 65 | Id: this.Id, |
| 66 | Prod: this.Prod, |
| 67 | pos: this.pos.clone(), |
| 68 | ProdIndex: this.ProdIndex, |
| 69 | Symbols: this.Symbols, |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | "this" may be a non-basic item. The function returns the set of basic items after ℇ-moves have be executed on this. |
| 75 | |
| 76 | For a general description of dotted items (items) and ℇ-moves of items, see: |
| 77 | |
| 78 | Modern Compiler Design. Dick Grune, et al. Second Edition. Springer 2012. |
| 79 | |
| 80 | ℇ-moves for lex items: |
| 81 | |
| 82 | Lex T be any production. |
| 83 | Let w,x,y,z be any strings of lexical symbols. |
| 84 | Let s = x|...|y have one or more alternatives. |
| 85 | |
| 86 | Then |
| 87 | |
| 88 | T : •s => T : •x|...|z |
| 89 | ... |
| 90 | T : x|...|•z |
| 91 | |
| 92 | T : x•[y]z => T : x[•y]z |
| 93 | T : x[y]•z |
| 94 | |
| 95 | T : x[y•]z => T : x[y]•z |
| 96 | |
| 97 | T : x•{y}z => T : x{•y}z |
| 98 | T : x{y}•z |
| 99 | |
| 100 | T : x{y•}z => T : x{•y}z |
| 101 | T : x{y}•z |
| 102 | |
| 103 | T : w•(x|...|y)z => T : w(•x|...|y)z |
| 104 | ... |
| 105 | T : w( x|...|•y)z |
| 106 | |
| 107 | T : x(...|y•|...)z => T : x(...|y|...)•z |
| 108 | */ |
| 109 | func (this *Item) Emoves() (items []*Item) { |
| 110 | newItems := util.NewStack(8).Push(this) |
| 111 | for newItems.Len() > 0 { |
| 112 | item := newItems.Pop().(*Item) |
| 113 | |
| 114 | if item.Reduce() || item.nextIsTerminal() { |
| 115 | items = append(items, item) |
| 116 | continue |
| 117 | } |
| 118 | |
| 119 | nt, pos := item.pos.top() |
| 120 | |
| 121 | switch node := nt.(type) { |
| 122 | case *ast.LexPattern: |
| 123 | newItems.Push(item.eMovesLexPattern(node, pos)...) |
| 124 | case *ast.LexGroupPattern: |
| 125 | newItems.Push(item.eMovesGroupPattern(node, pos)...) |
| 126 | case *ast.LexOptPattern: |
| 127 | newItems.Push(item.eMovesOptPattern(node, pos)...) |
| 128 | case *ast.LexRepPattern: |
| 129 | newItems.Push(item.eMovesRepPattern(node, pos)...) |
| 130 | case *ast.LexAlt: |
| 131 | if newItem := item.eMovesLexAlt(node, pos); newItem != item { |
| 132 | newItems.Push(newItem) |
| 133 | } else { |
| 134 | items = append(items, item) |
| 135 | } |
| 136 | default: |
| 137 | panic(fmt.Sprintf("Unexpected type in items.Emoves(): %T", nt)) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return |
| 142 | } |
| 143 | |
| 144 | func (this *Item) eMovesLexPattern(nt *ast.LexPattern, pos int) []interface{} { |
| 145 | if pos == 0 { |
| 146 | return this.newLexPatternBasicItems(nt, pos) |
| 147 | } |
| 148 | |
| 149 | postItem := this.Clone() |
| 150 | if postItem.pos.level() == 0 { |
| 151 | postItem.pos.stack[0].pos = nt.Len() |
| 152 | } else { |
| 153 | postItem.pos.pop() |
| 154 | postItem.pos.inc() |
| 155 | } |
| 156 | postItem.getHashKey() |
| 157 | |
| 158 | return []interface{}{postItem} |
| 159 | } |
| 160 | |
| 161 | func (this *Item) eMovesGroupPattern(nt *ast.LexGroupPattern, pos int) []interface{} { |
| 162 | if pos == 0 { |
| 163 | return this.newLexPatternBasicItems(nt.LexPattern, pos) |
| 164 | } |
| 165 | |
| 166 | postItem := this.Clone() |
| 167 | postItem.pos.pop() |
| 168 | postItem.pos.inc() |
| 169 | postItem.getHashKey() |
| 170 | |
| 171 | return []interface{}{postItem} |
| 172 | } |
| 173 | |
| 174 | func (this *Item) eMovesLexAlt(nt *ast.LexAlt, pos int) *Item { |
| 175 | if pos >= len(nt.Terms) { |
| 176 | postItem := this.Clone() |
| 177 | postItem.pos.pop() |
| 178 | postItem.pos.setToEnd() |
| 179 | postItem.getHashKey() |
| 180 | return postItem |
| 181 | } |
| 182 | |
| 183 | nextTerm := nt.Terms[pos] |
| 184 | |
| 185 | if nextTerm.LexTerminal() { |
| 186 | return this |
| 187 | } |
| 188 | |
| 189 | newItem := this.Clone() |
| 190 | newItem.pos.push(nextTerm.(ast.LexNTNode), 0) |
| 191 | newItem.getHashKey() |
| 192 | |
| 193 | return newItem |
| 194 | } |
| 195 | |
| 196 | func (this *Item) eMovesOptPattern(nt *ast.LexOptPattern, pos int) (items []interface{}) { |
| 197 | if pos == 0 { |
| 198 | items = append(items, this.newLexPatternBasicItems(nt.LexPattern, pos)...) |
| 199 | } |
| 200 | |
| 201 | postItem := this.Clone() |
| 202 | postItem.pos.pop() |
| 203 | postItem.pos.inc() |
| 204 | postItem.getHashKey() |
| 205 | items = append(items, postItem) |
| 206 | |
| 207 | return |
| 208 | } |
| 209 | |
| 210 | func (this *Item) eMovesRepPattern(nt *ast.LexRepPattern, pos int) (items []interface{}) { |
| 211 | items = append(items, this.newLexPatternBasicItems(nt.LexPattern, pos)...) |
| 212 | |
| 213 | postItem := this.Clone() |
| 214 | postItem.pos.pop() |
| 215 | postItem.pos.inc() |
| 216 | postItem.getHashKey() |
| 217 | items = append(items, postItem) |
| 218 | |
| 219 | return |
| 220 | } |
| 221 | |
| 222 | func (this *Item) getHashKey() { |
| 223 | w := new(strings.Builder) |
| 224 | fmt.Fprintf(w, "%d:", this.ProdIndex) |
| 225 | for i, stackElem := range this.pos.stack { |
| 226 | if i < len(this.pos.stack)-1 { |
| 227 | fmt.Fprintf(w, "%d:", stackElem.pos) |
| 228 | } else { |
| 229 | fmt.Fprintf(w, "%d", stackElem.pos) |
| 230 | } |
| 231 | } |
| 232 | this.hashKey = w.String() |
| 233 | } |
| 234 | |
| 235 | func (this *Item) HashKey() string { |
| 236 | if this.hashKey == "" { |
| 237 | panic("Uninitialised hash key") |
| 238 | } |
| 239 | return this.hashKey |
| 240 | } |
| 241 | |
| 242 | func (this *Item) newLexPatternBasicItems(nt *ast.LexPattern, pos int) []interface{} { |
| 243 | numAlts := len(nt.Alternatives) |
| 244 | items := make([]interface{}, numAlts) |
| 245 | for i, alt := range nt.Alternatives { |
| 246 | altItem := this.Clone() |
| 247 | altItem.pos.setPos(i) |
| 248 | altItem.pos.push(alt, 0) |
| 249 | altItem.getHashKey() |
| 250 | items[i] = altItem |
| 251 | } |
| 252 | return items |
| 253 | } |
| 254 | |
| 255 | func (this *Item) nextIsTerminal() bool { |
| 256 | nt, pos := this.pos.top() |
| 257 | if pos >= nt.Len() { |
| 258 | return false |
| 259 | } |
| 260 | return nt.Element(pos).LexTerminal() |
| 261 | } |
| 262 | |
| 263 | func (this *Item) Equal(that *Item) bool { |
| 264 | if this.hashKey == "" || that.hashKey == "" { |
| 265 | panic("nil hashkey") |
| 266 | } |
| 267 | return this.hashKey == that.hashKey |
| 268 | } |
| 269 | |
| 270 | // ExpectedSymbol returns the expected symbol for shift items and nil for reduce |
| 271 | // items. This is the position of a basic item -- no ℇ-moves are possible. |
| 272 | func (this *Item) ExpectedSymbol() (node ast.LexTNode) { |
| 273 | nt, pos := this.pos.top() |
| 274 | if pos >= nt.Len() { |
| 275 | return nil |
| 276 | } |
| 277 | return nt.Element(pos).(ast.LexTNode) |
| 278 | } |
| 279 | |
| 280 | func (this *Item) match(rng CharRange) bool { |
| 281 | lexTerm := this.ExpectedSymbol() |
| 282 | if lexTerm == nil { |
| 283 | return false |
| 284 | } |
| 285 | switch t := lexTerm.(type) { |
| 286 | case *ast.LexDot: |
| 287 | return false |
| 288 | case *ast.LexCharLit: |
| 289 | return rng.From == t.Val && rng.To == t.Val |
| 290 | case *ast.LexCharRange: |
| 291 | return rng.From >= t.From.Val && rng.From <= t.To.Val && |
| 292 | rng.To <= t.To.Val |
| 293 | case *ast.LexRegDefId: |
| 294 | return false |
| 295 | } |
| 296 | panic(fmt.Sprintf("Unexpected lexTerm type: %T", lexTerm)) |
| 297 | } |
| 298 | |
| 299 | func (this *Item) Move(rng CharRange) []*Item { |
| 300 | if !this.match(rng) { |
| 301 | return nil |
| 302 | } |
| 303 | movedItem := this.Clone() |
| 304 | movedItem.pos.inc() |
| 305 | movedItem.getHashKey() |
| 306 | |
| 307 | items := movedItem.Emoves() |
| 308 | |
| 309 | return items |
| 310 | } |
| 311 | |
| 312 | func (this *Item) MoveDot() []*Item { |
| 313 | if this.ExpectedSymbol() != ast.LexDOT { |
| 314 | return nil |
| 315 | } |
| 316 | movedItem := this.Clone() |
| 317 | movedItem.pos.inc() |
| 318 | movedItem.getHashKey() |
| 319 | items := movedItem.Emoves() |
| 320 | |
| 321 | return items |
| 322 | } |
| 323 | |
| 324 | func (this *Item) MoveRegDefId(id string) []*Item { |
| 325 | if rid, ok := this.ExpectedSymbol().(*ast.LexRegDefId); ok && id == rid.Id { |
| 326 | movedItem := this.Clone() |
| 327 | movedItem.pos.inc() |
| 328 | movedItem.getHashKey() |
| 329 | items := movedItem.Emoves() |
| 330 | return items |
| 331 | } |
| 332 | return nil |
| 333 | } |
| 334 | |
| 335 | // Reduce returns true for return items, such as `T : xyz •`, otherwise returns false. |
| 336 | func (this *Item) Reduce() bool { |
| 337 | if this.pos.level() != 0 { |
| 338 | return false |
| 339 | } |
| 340 | ntNode, pos := this.pos.top() |
| 341 | return pos >= ntNode.Len() |
| 342 | } |
| 343 | |
| 344 | func (this *Item) getString() { |
| 345 | w := new(strings.Builder) |
| 346 | fmt.Fprintf(w, "%s : ", this.Prod.Id()) |
| 347 | if this.Reduce() { |
| 348 | le, _ := this.pos.top() |
| 349 | if le.Len() > 1 { |
| 350 | fmt.Fprintf(w, "(") |
| 351 | } |
| 352 | WriteStringNode(w, this.Prod.LexPattern(), this.pos) |
| 353 | if le.Len() > 1 { |
| 354 | fmt.Fprintf(w, ") •") |
| 355 | } else { |
| 356 | fmt.Fprintf(w, " •") |
| 357 | } |
| 358 | } else { |
| 359 | WriteStringNode(w, this.Prod.LexPattern(), this.pos) |
| 360 | } |
| 361 | this.str = w.String() |
| 362 | } |
| 363 | |
| 364 | func (this *Item) String() string { |
| 365 | if this.str == "" { |
| 366 | this.getString() |
| 367 | } |
| 368 | return this.str |
| 369 | } |
| 370 | |
| 371 | func WriteStringNode(w io.Writer, node ast.LexNode, pos *itemPos) { |
| 372 | topNode, idx := pos.top() |
| 373 | |
| 374 | switch n := node.(type) { |
| 375 | case *ast.LexPattern: |
| 376 | writeStringPattern(w, n, pos) |
| 377 | case *ast.LexDot: |
| 378 | fmt.Fprintf(w, ".") |
| 379 | case *ast.LexCharLit: |
| 380 | fmt.Fprintf(w, "%s", n.String()) |
| 381 | case *ast.LexCharRange: |
| 382 | fmt.Fprintf(w, " %s-%s", n.From.String(), n.To.String()) |
| 383 | case *ast.LexGroupPattern: |
| 384 | fmt.Fprintf(w, "(") |
| 385 | writeStringPattern(w, n.LexPattern, pos) |
| 386 | fmt.Fprintf(w, ")") |
| 387 | case *ast.LexAlt: |
| 388 | for i, term := range n.Terms { |
| 389 | if n == topNode && i == idx { |
| 390 | fmt.Fprintf(w, "• ") |
| 391 | } |
| 392 | WriteStringNode(w, term, pos) |
| 393 | if i < len(n.Terms)-1 { |
| 394 | fmt.Fprintf(w, " ") |
| 395 | } |
| 396 | } |
| 397 | case *ast.LexOptPattern: |
| 398 | fmt.Fprintf(w, "[") |
| 399 | writeStringPattern(w, n.LexPattern, pos) |
| 400 | fmt.Fprintf(w, "]") |
| 401 | case *ast.LexRegDefId: |
| 402 | fmt.Fprintf(w, "%s", n.String()) |
| 403 | case *ast.LexRepPattern: |
| 404 | fmt.Fprintf(w, "{") |
| 405 | writeStringPattern(w, n.LexPattern, pos) |
| 406 | fmt.Fprintf(w, "}") |
| 407 | default: |
| 408 | panic(fmt.Sprintf("Unexpected type of node, %T", node)) |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | func writeStringPattern(w io.Writer, pattern *ast.LexPattern, pos *itemPos) { |
| 413 | for i, alt := range pattern.Alternatives { |
| 414 | WriteStringNode(w, alt, pos) |
| 415 | if i < len(pattern.Alternatives)-1 { |
| 416 | fmt.Fprintf(w, " | ") |
| 417 | } |
| 418 | } |
| 419 | } |