| 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 | ) |
| 23 | |
| 24 | type itemPos struct { |
| 25 | stack []stackElement |
| 26 | } |
| 27 | |
| 28 | type stackElement struct { |
| 29 | node ast.LexNTNode |
| 30 | pos int |
| 31 | } |
| 32 | |
| 33 | func newItemPos(lexPattern *ast.LexPattern) (pos *itemPos) { |
| 34 | pos = &itemPos{ |
| 35 | stack: make([]stackElement, 0, 8), |
| 36 | } |
| 37 | pos.push(lexPattern, 0) |
| 38 | return |
| 39 | } |
| 40 | |
| 41 | func (this *itemPos) clone() *itemPos { |
| 42 | clone := &itemPos{ |
| 43 | stack: make([]stackElement, len(this.stack)), |
| 44 | } |
| 45 | copy(clone.stack, this.stack[0:len(this.stack)]) |
| 46 | return clone |
| 47 | } |
| 48 | |
| 49 | func (this *itemPos) inc() { |
| 50 | this.stack[this.level()].pos++ |
| 51 | } |
| 52 | |
| 53 | func (this *itemPos) pop() (ntNode ast.LexNTNode, pos int) { |
| 54 | ntNode, pos = this.top() |
| 55 | this.stack = this.stack[:len(this.stack)-1] |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | func (this *itemPos) push(node ast.LexNTNode, pos int) { |
| 60 | this.stack = append(this.stack, stackElement{node, pos}) |
| 61 | } |
| 62 | |
| 63 | func (this *itemPos) top() (nt ast.LexNTNode, pos int) { |
| 64 | top := len(this.stack) - 1 |
| 65 | nt, pos = this.stack[top].node, this.stack[top].pos |
| 66 | return |
| 67 | } |
| 68 | |
| 69 | // level returns the level of stacking in the item, where the bottom is 0. |
| 70 | func (this *itemPos) level() int { |
| 71 | return len(this.stack) - 1 |
| 72 | } |
| 73 | |
| 74 | // ntNode returns the ast.Node at the top of the stack. |
| 75 | func (this *itemPos) ntNode() ast.LexNTNode { |
| 76 | n, _ := this.top() |
| 77 | return n |
| 78 | } |
| 79 | |
| 80 | // pos returns the position within the top level of the stack. |
| 81 | func (this *itemPos) pos() int { |
| 82 | _, pos := this.top() |
| 83 | return pos |
| 84 | } |
| 85 | |
| 86 | func (this *itemPos) setPos(i int) { |
| 87 | this.stack[this.level()].pos = i |
| 88 | } |
| 89 | |
| 90 | func (this *itemPos) setToEnd() { |
| 91 | node, _ := this.top() |
| 92 | this.stack[this.level()].pos = node.Len() |
| 93 | } |
| 94 | |
| 95 | func (this *itemPos) equal(that *itemPos) bool { |
| 96 | if len(this.stack) != len(that.stack) { |
| 97 | return false |
| 98 | } |
| 99 | |
| 100 | for i := 0; i < len(this.stack); i++ { |
| 101 | if this.stack[i].node != that.stack[i].node || |
| 102 | this.stack[i].pos != that.stack[i].pos { |
| 103 | return false |
| 104 | } |
| 105 | } |
| 106 | return true |
| 107 | } |
| 108 | |
| 109 | func (this *itemPos) String() string { |
| 110 | buf := new(strings.Builder) |
| 111 | for i := 0; i < len(this.stack); i++ { |
| 112 | fmt.Fprintf(buf, "\t%T:%v; pos %d\n", (*this).stack[i].node, (*this).stack[i].node, this.stack[i].pos) |
| 113 | } |
| 114 | return buf.String() |
| 115 | } |