-
Notifications
You must be signed in to change notification settings - Fork 0
/
table-item.go
62 lines (56 loc) · 1.39 KB
/
table-item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package lr0
import (
"github.com/pkg/errors"
)
// Create tableItem from a Rule with position pointing to first symbol
//
// input rule:
// Sum : Sum "+" Product
// output tableItem:
// Sum : > Sum "+" Product
func newTableItem(r Rule) tableItem {
return tableItem{Rule: r}
}
// Item of parser state items set
//
// Item is a rule-like object, where definition body is spited by Current
// Position into passed and further symbols. So each Rule can produce exactly
// N+1 Items where N is count of symbols in Rule definition.
//
// rule:
// Sum : Sum "+" Product
// items possible:
// Sum : > Sum "+" Product
// Sum : Sum > "+" Product
// Sum : Sum "+" > Product
// Sum : Sum "+" Product >
type tableItem struct {
Rule
nextIndex int
}
// Expected returns next expected Id or InvalidId in the last
// position
func (i tableItem) Expected() Id {
if !i.HasFurther() {
return InvalidId
}
return i.Definition()[i.nextIndex]
}
// HasFurther returns whether the tableItem has further expected symbols
func (i tableItem) HasFurther() bool {
return i.nextIndex < len(i.Definition())
}
// Shift creates a new tableItem by shifting current position to the next symbol
//
// in
// Sum : Sum > "+" Product
// out
// Sum : Sum "+" > Product
func (i tableItem) Shift() tableItem {
if !i.HasFurther() {
panic(errors.Wrap(ErrInternal, "bad usage"))
}
next := i
next.nextIndex++
return next
}