Debian Codegraph @

internal/parser/lr1/items/item.go

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