Debian Codegraph @

internal/frontend/parser/parser_ut.go

raw symbols SCIP
1 package parser
2
3 import (
4 "errors"
5 "strconv"
6
7 errs "github.com/goccmack/gocc/internal/frontend/errors"
8 "github.com/goccmack/gocc/internal/frontend/token"
9 )
10
11 // ParserUTab
12
13 type ParserUTab struct {
14 actTab *ActionTabU
15 canRecover *CanRecover
16 gotoTab *GotoTabU
17 prodTab *ProdTabU
18 stack *stack
19 nextToken *token.Token
20 pos token.Position
21 tokenMap *token.TokenMap
22 }
23
24 func NewParserUTab(tm *token.TokenMap) *ParserUTab {
25 p := &ParserUTab{
26 actTab: getActionTableUncompressed(),
27 canRecover: getCanRecoverTableUncompressed(),
28 gotoTab: getGotoTableUncompressed(),
29 prodTab: getProductionsTableUncompressed(),
30 stack: NewStack(),
31 tokenMap: tm,
32 }
33 p.stack.Push(0, nil) //TODO: which attribute should be pushed here?
34 return p
35 }
36
37 func (P *ParserUTab) Reset() {
38 P.stack.reset()
39 P.stack.Push(0, nil) //TODO: which attribute should be pushed here?
40 }
41
42 func (P *ParserUTab) Error(err error, scanner Scanner) (recovered bool, errorAttrib *errs.Error) {
43 errorAttrib = &errs.Error{
44 Err: err,
45 ErrorToken: P.nextToken,
46 ErrorPos: P.pos,
47 ErrorSymbols: P.popNonRecoveryStates(),
48 ExpectedTokens: make([]string, 0, 8),
49 }
50 for t, act := range P.actTab[P.stack.Top()] {
51 if act != nil {
52 errorAttrib.ExpectedTokens = append(errorAttrib.ExpectedTokens, P.tokenMap.TokenString(token.Type(t)))
53 }
54 }
55
56 action := P.actTab[P.stack.Top()][P.tokenMap.Type("error")]
57 if action == nil {
58 return
59 }
60 P.stack.Push(State(action.(Shift)), errorAttrib) // action can only be shift
61
62 for P.actTab[P.stack.Top()][P.nextToken.Type] == nil && P.nextToken.Type != token.EOF {
63 P.nextToken, P.pos = scanner.Scan()
64 }
65
66 return
67 }
68
69 func (P *ParserUTab) popNonRecoveryStates() (removedAttribs []errs.ErrorSymbol) {
70 if rs, ok := P.firstRecoveryState(); ok {
71 errorSymbols := P.stack.PopN(P.stack.TopIndex() - rs)
72 removedAttribs = make([]errs.ErrorSymbol, len(errorSymbols))
73 for i, e := range errorSymbols {
74 removedAttribs[i] = e
75 }
76 } else {
77 removedAttribs = []errs.ErrorSymbol{}
78 }
79 return
80 }
81
82 // recoveryState points to the highest state on the stack, which can recover
83 func (P *ParserUTab) firstRecoveryState() (recoveryState int, canRecover bool) {
84 recoveryState, canRecover = P.stack.TopIndex(), P.canRecover[P.stack.Top()]
85 for recoveryState > 0 && !canRecover {
86 recoveryState--
87 canRecover = P.canRecover[P.stack.Peek(recoveryState)]
88 }
89 return
90 }
91
92 func (P *ParserUTab) newError(err error) error {
93 errmsg := "Error: " + P.TokString(P.nextToken) + " @ " + P.pos.String()
94 if err != nil {
95 errmsg += " " + err.Error()
96 } else {
97 errmsg += ", expected one of: "
98 actRow := P.actTab[P.stack.Top()]
99 i := 0
100 for t, act := range actRow {
101 if act != nil {
102 errmsg += P.tokenMap.TokenString(token.Type(t))
103 if i < len(actRow)-1 {
104 errmsg += " "
105 }
106 i++
107 }
108 }
109 }
110 return errors.New(errmsg)
111 }
112
113 func (P *ParserUTab) TokString(tok *token.Token) string {
114 msg := P.tokenMap.TokenString(tok.Type) + "(" + strconv.Itoa(int(tok.Type)) + ")"
115 msg += " " + string(tok.Lit)
116 return msg
117 }
118
119 func (this *ParserUTab) Parse(scanner Scanner) (res interface{}, err error) {
120 this.Reset()
121 this.nextToken, this.pos = scanner.Scan()
122 for acc := false; !acc; {
123 action := this.actTab[this.stack.Top()][this.nextToken.Type]
124 if action == nil {
125 if recovered, errAttrib := this.Error(nil, scanner); !recovered {
126 this.nextToken, this.pos = errAttrib.ErrorToken, errAttrib.ErrorPos
127 return nil, this.newError(nil)
128 }
129 if action = this.actTab[this.stack.Top()][this.nextToken.Type]; action == nil {
130 panic("Error recover led to invalid action")
131 }
132 }
133 // fmt.Printf("S%d %s %s\n", this.stack.Top(), this.nextToken, action)
134 switch act := action.(type) {
135 case Accept:
136 res = this.stack.PopN(1)[0]
137 acc = true
138 case Shift:
139 this.stack.Push(State(act), this.nextToken)
140 this.nextToken, this.pos = scanner.Scan()
141 case Reduce:
142 prod := this.prodTab[int(act)]
143 attrib, err := prod.ReduceFunc(this.stack.PopN(prod.NumSymbols))
144 if err != nil {
145 return nil, this.newError(err)
146 } else {
147 this.stack.Push(this.gotoTab[this.stack.Top()][prod.HeadIndex], attrib)
148 }
149 default:
150 panic("unknown action: " + action.String())
151 }
152 }
153 return res, nil
154 }