| 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 itemsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24 fmt/" |
| 19 | "stringsscip-go gomod github.com/golang/go/src go1.24 strings/" |
| 20 | |
| 21 | "github.com/goccmack/gocc/internal/astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/" |
| 22 | "github.com/goccmack/gocc/internal/parser/lr1/actionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/action`/" |
| 23 | ) |
| 24 | |
| 25 | // An LR1 Item. |
| 26 | type Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item# struct { |
| 27 | ProdIdxscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#ProdIdx. int // index in list of productions in Grammar.Prod |
| 28 | Prodscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Prod. *astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/.SyntaxProdscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxProd# // The syntax production of this item |
| 29 | Idscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Id. string |
| 30 | Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Body. []string |
| 31 | Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Pos. int // position of • in the item |
| 32 | ExpectedSymbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#ExpectedSymbol. string // the next exptect symbol in the item, if this isn't a reduce item. |
| 33 | FollowingSymbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#FollowingSymbol. string // The next expected symbol after the item has been recognised |
| 34 | Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len. int // the number of symbols making up the body |
| 35 | strscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#str. string |
| 36 | } |
| 37 | |
| 38 | // following symbol: the symbol expected after this item has been reduced. |
| 39 | func NewItemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/NewItem().(prodIdx int, prod *astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/.SyntaxProdscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxProd#, pos int, followingSymbol string) *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item# { |
| 40 | item := &Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#{ |
| 41 | ProdIdxscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#ProdIdx.: prodIdx, |
| 42 | Prodscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Prod.: prod, |
| 43 | Idscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Id.: prod.Idscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxProd#Id., |
| 44 | Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Pos.: pos, |
| 45 | FollowingSymbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#FollowingSymbol.: followingSymbol, |
| 46 | } |
| 47 | if prod.Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxProd#Body..Symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxBody#Symbols.[0].SymbolStringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxSymbol#SymbolString.() == "empty" { |
| 48 | item.Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len. = 0 |
| 49 | } else { |
| 50 | item.Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len. = len(prod.Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxProd#Body..Symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxBody#Symbols.) |
| 51 | } |
| 52 | if pos > item.Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len. { |
| 53 | panic(fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Sprintf().("%s : %s, pos=%d", item.Idscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Id., item.Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Body., item.Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Pos.)) |
| 54 | } |
| 55 | item.Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Body. = make([]string, item.Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len.) |
| 56 | if item.Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len. > 0 { |
| 57 | for i, sym := range prod.Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxProd#Body..Symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxBody#Symbols. { |
| 58 | item.Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Body.[i] = sym.SymbolStringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxSymbol#SymbolString.() |
| 59 | } |
| 60 | } |
| 61 | if item.Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len. > 0 && item.Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Pos. < item.Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len. { |
| 62 | item.ExpectedSymbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#ExpectedSymbol. = item.Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Body.[item.Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Pos.] |
| 63 | } else { |
| 64 | item.ExpectedSymbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#ExpectedSymbol. = "" |
| 65 | } |
| 66 | item.strscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#str. = item.getStringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#getString().() |
| 67 | return item |
| 68 | } |
| 69 | |
| 70 | func (this *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#) acceptscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#accept().(sym string) bool { |
| 71 | return this.ProdIdxscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#ProdIdx. == 0 && |
| 72 | this.Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Pos. >= this.Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len. && |
| 73 | this.FollowingSymbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#FollowingSymbol. == "␚" && |
| 74 | sym == "␚" |
| 75 | } |
| 76 | |
| 77 | // If the action is shift the next state is nextState. |
| 78 | func (this *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#) actionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#action().(sym string, nextState int) actionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/action`/.Actionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/action`/Action# { |
| 79 | switch { |
| 80 | case sym == "INVALID": |
| 81 | return actionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/action`/.ERRORscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/action`/ERROR. |
| 82 | case this.acceptscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#accept().(sym): |
| 83 | return actionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/action`/.ACCEPTscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/action`/ACCEPT. |
| 84 | case this.reducescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#reduce().() && this.FollowingSymbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#FollowingSymbol. == sym: |
| 85 | return actionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/action`/.Reducescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/action`/Reduce#(this.ProdIdxscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#ProdIdx.) |
| 86 | case sym == this.ExpectedSymbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#ExpectedSymbol.: |
| 87 | return actionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/action`/.Shiftscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/action`/Shift#(nextState) |
| 88 | } |
| 89 | return actionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/action`/.ERRORscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/action`/ERROR. |
| 90 | } |
| 91 | |
| 92 | func (this *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#) canRecoverscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#canRecover().() bool { |
| 93 | return this.Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len. > 0 && this.Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Body.[0] == "error" |
| 94 | } |
| 95 | |
| 96 | // Equals weturns whether two Items are equal based on their ProdIdx, Pos and NextToken. |
| 97 | func (this *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#) Equalsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Equals().(that *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#) bool { |
| 98 | if that == nil { |
| 99 | return false |
| 100 | } |
| 101 | |
| 102 | return this.ProdIdxscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#ProdIdx. == that.ProdIdxscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#ProdIdx. && |
| 103 | this.Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Pos. == that.Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Pos. && |
| 104 | this.FollowingSymbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#FollowingSymbol. == that.FollowingSymbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#FollowingSymbol. |
| 105 | } |
| 106 | |
| 107 | func (this *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#) Movescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Move().() (next *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#) { |
| 108 | return NewItemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/NewItem().(this.ProdIdxscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#ProdIdx., this.Prodscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Prod., this.Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Pos.+1, this.FollowingSymbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#FollowingSymbol.) |
| 109 | } |
| 110 | |
| 111 | // reduce returns true if this is a reduce item. |
| 112 | func (this *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#) reducescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#reduce().() bool { |
| 113 | return this.Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len. == 0 || this.Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Pos. >= this.Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len. |
| 114 | } |
| 115 | |
| 116 | func (this *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#) Symbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Symbol().(i int) string { |
| 117 | return this.Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Body.[i] |
| 118 | } |
| 119 | |
| 120 | func (this *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#) getStringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#getString().() string { |
| 121 | buf := new(stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Builderscip-go gomod github.com/golang/go/src go1.24 strings/Builder#) |
| 122 | fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(buf, "%s : ", this.Idscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Id.) |
| 123 | if this.Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len. == 0 { |
| 124 | fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(buf, "empty") |
| 125 | } else { |
| 126 | for i, s := range this.Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Body. { |
| 127 | if this.Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Pos. == i { |
| 128 | fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(buf, "•") |
| 129 | } |
| 130 | fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.24 fmt/Fprint().(buf, s) |
| 131 | if i < this.Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len.-1 { |
| 132 | fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(buf, " ") |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | if this.Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Pos. == this.Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#Len. { |
| 137 | fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(buf, "•") |
| 138 | } |
| 139 | fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(buf, " «%s»", this.FollowingSymbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#FollowingSymbol.) |
| 140 | return buf.Stringscip-go gomod github.com/golang/go/src go1.24 strings/Builder#String().() |
| 141 | } |
| 142 | |
| 143 | func (this *Itemscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#) Stringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#String().() string { |
| 144 | return this.strscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/lr1/items`/Item#str. |
| 145 | } |