| 1 | package parser |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "strconv" |
| 7 | |
| 8 | errs "github.com/goccmack/gocc/internal/frontend/errors" |
| 9 | "github.com/goccmack/gocc/internal/frontend/token" |
| 10 | ) |
| 11 | |
| 12 | type ( |
| 13 | ActionTab []*ActionRow |
| 14 | ActionRow struct { |
| 15 | canRecover bool |
| 16 | Actions Actions |
| 17 | } |
| 18 | Actions map[token.Type]Action |
| 19 | ) |
| 20 | |
| 21 | func (R *ActionRow) String() string { |
| 22 | s := fmt.Sprintf("canRecover=%t\n", R.canRecover) |
| 23 | for t, a := range R.Actions { |
| 24 | s += strconv.Itoa(int(t)) + " : " + a.String() + "\n" |
| 25 | } |
| 26 | return s |
| 27 | } |
| 28 | |
| 29 | type ( |
| 30 | Accept int |
| 31 | Shift State |
| 32 | Reduce int |
| 33 | |
| 34 | Action interface { |
| 35 | Act() |
| 36 | String() string |
| 37 | } |
| 38 | ) |
| 39 | |
| 40 | func (this Accept) Act() {} |
| 41 | func (this Shift) Act() {} |
| 42 | func (this Reduce) Act() {} |
| 43 | |
| 44 | func (this Accept) Equal(that Action) bool { |
| 45 | if _, ok := that.(Accept); ok { |
| 46 | return true |
| 47 | } |
| 48 | return false |
| 49 | } |
| 50 | |
| 51 | func (this Reduce) Equal(that Action) bool { |
| 52 | that1, ok := that.(Reduce) |
| 53 | if !ok { |
| 54 | return false |
| 55 | } |
| 56 | return this == that1 |
| 57 | } |
| 58 | |
| 59 | func (this Shift) Equal(that Action) bool { |
| 60 | that1, ok := that.(Shift) |
| 61 | if !ok { |
| 62 | return false |
| 63 | } |
| 64 | return this == that1 |
| 65 | } |
| 66 | |
| 67 | func (this Accept) String() string { return "Accept(0)" } |
| 68 | func (this Shift) String() string { return fmt.Sprintf("Shift:%d", this) } |
| 69 | func (this Reduce) String() string { |
| 70 | return fmt.Sprintf("Reduce:%d(%s)", this, ProductionsTable[this].String) |
| 71 | } |
| 72 | |
| 73 | type ( |
| 74 | GotoTab []GotoRow |
| 75 | GotoRow map[NT]State |
| 76 | State int |
| 77 | NT string |
| 78 | ) |
| 79 | |
| 80 | type ( |
| 81 | ProdTab []ProdTabEntry |
| 82 | ProdTabEntry struct { |
| 83 | String string |
| 84 | Head NT |
| 85 | NumSymbols int |
| 86 | ReduceFunc func([]Attrib) (Attrib, error) |
| 87 | } |
| 88 | Attrib interface { |
| 89 | } |
| 90 | ) |
| 91 | |
| 92 | // Stack |
| 93 | |
| 94 | type stack struct { |
| 95 | state []State |
| 96 | attrib []Attrib |
| 97 | } |
| 98 | |
| 99 | const INITIAL_STACK_SIZE = 100 |
| 100 | |
| 101 | func NewStack() *stack { |
| 102 | return &stack{state: make([]State, 0, INITIAL_STACK_SIZE), |
| 103 | attrib: make([]Attrib, 0, INITIAL_STACK_SIZE), |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func (this *stack) reset() { |
| 108 | this.state = this.state[0:0] |
| 109 | this.attrib = this.attrib[0:0] |
| 110 | } |
| 111 | |
| 112 | func (this *stack) Push(s State, a Attrib) { |
| 113 | this.state = append(this.state, s) |
| 114 | this.attrib = append(this.attrib, a) |
| 115 | } |
| 116 | |
| 117 | func (this *stack) Top() State { |
| 118 | return this.state[len(this.state)-1] |
| 119 | } |
| 120 | |
| 121 | func (this *stack) Peek(pos int) State { |
| 122 | return this.state[pos] |
| 123 | } |
| 124 | |
| 125 | func (this *stack) TopIndex() int { |
| 126 | return len(this.state) - 1 |
| 127 | } |
| 128 | |
| 129 | func (this *stack) PopN(items int) []Attrib { |
| 130 | lo, hi := len(this.state)-items, len(this.state) |
| 131 | |
| 132 | attrib := this.attrib[lo:hi] |
| 133 | |
| 134 | this.state = this.state[:lo] |
| 135 | this.attrib = this.attrib[:lo] |
| 136 | |
| 137 | return attrib |
| 138 | } |
| 139 | |
| 140 | func (S *stack) String() string { |
| 141 | res := "stack:\n" |
| 142 | for i, st := range S.state { |
| 143 | res += "\t" + strconv.Itoa(i) + ": " + strconv.Itoa(int(st)) |
| 144 | res += " , " |
| 145 | if S.attrib[i] == nil { |
| 146 | res += "nil" |
| 147 | } else { |
| 148 | res += fmt.Sprintf("%v", S.attrib[i]) |
| 149 | } |
| 150 | res += "\n" |
| 151 | } |
| 152 | return res |
| 153 | } |
| 154 | |
| 155 | // Parser |
| 156 | |
| 157 | type Parser struct { |
| 158 | actTab ActionTab |
| 159 | gotoTab GotoTab |
| 160 | prodTab ProdTab |
| 161 | stack *stack |
| 162 | nextToken *token.Token |
| 163 | pos token.Position |
| 164 | tokenMap *token.TokenMap |
| 165 | } |
| 166 | |
| 167 | type Scanner interface { |
| 168 | Scan() (*token.Token, token.Position) |
| 169 | } |
| 170 | |
| 171 | func NewParser(act ActionTab, gto GotoTab, prod ProdTab, tm *token.TokenMap) *Parser { |
| 172 | p := &Parser{actTab: act, gotoTab: gto, prodTab: prod, stack: NewStack(), tokenMap: tm} |
| 173 | p.stack.Push(0, nil) |
| 174 | return p |
| 175 | } |
| 176 | |
| 177 | func (P *Parser) Reset() { |
| 178 | P.stack.reset() |
| 179 | P.stack.Push(0, nil) |
| 180 | } |
| 181 | |
| 182 | func Acc() { |
| 183 | fmt.Println("Accept") |
| 184 | } |
| 185 | |
| 186 | func (P *Parser) Error(err error, scanner Scanner) (recovered bool, errorAttrib *errs.Error) { |
| 187 | errorAttrib = &errs.Error{ |
| 188 | Err: err, |
| 189 | ErrorToken: P.nextToken, |
| 190 | ErrorPos: P.pos, |
| 191 | ErrorSymbols: P.popNonRecoveryStates(), |
| 192 | ExpectedTokens: make([]string, 0, 8), |
| 193 | } |
| 194 | for t := range P.actTab[P.stack.Top()].Actions { |
| 195 | errorAttrib.ExpectedTokens = append(errorAttrib.ExpectedTokens, P.tokenMap.TokenString(t)) |
| 196 | } |
| 197 | |
| 198 | action, ok := P.actTab[P.stack.Top()].Actions[P.tokenMap.Type("error")] |
| 199 | if !ok { |
| 200 | return |
| 201 | } |
| 202 | P.stack.Push(State(action.(Shift)), errorAttrib) // action can only be shift |
| 203 | |
| 204 | _, recovered = P.actTab[P.stack.Top()].Actions[P.nextToken.Type] |
| 205 | for !recovered && P.nextToken.Type != token.EOF { |
| 206 | P.nextToken, P.pos = scanner.Scan() |
| 207 | _, recovered = P.actTab[P.stack.Top()].Actions[P.nextToken.Type] |
| 208 | } |
| 209 | |
| 210 | return |
| 211 | } |
| 212 | |
| 213 | func (P *Parser) popNonRecoveryStates() (removedAttribs []errs.ErrorSymbol) { |
| 214 | if rs, ok := P.firstRecoveryState(); ok { |
| 215 | errorSymbols := P.stack.PopN(P.stack.TopIndex() - rs) |
| 216 | removedAttribs = make([]errs.ErrorSymbol, len(errorSymbols)) |
| 217 | for i, e := range errorSymbols { |
| 218 | removedAttribs[i] = e |
| 219 | } |
| 220 | } else { |
| 221 | removedAttribs = []errs.ErrorSymbol{} |
| 222 | } |
| 223 | return |
| 224 | } |
| 225 | |
| 226 | // recoveryState points to the highest state on the stack, which can recover |
| 227 | func (P *Parser) firstRecoveryState() (recoveryState int, canRecover bool) { |
| 228 | recoveryState, canRecover = P.stack.TopIndex(), P.actTab[P.stack.Top()].canRecover |
| 229 | for recoveryState > 0 && !canRecover { |
| 230 | recoveryState-- |
| 231 | canRecover = P.actTab[P.stack.Peek(recoveryState)].canRecover |
| 232 | } |
| 233 | return |
| 234 | } |
| 235 | |
| 236 | func (P *Parser) newError(err error) error { |
| 237 | errmsg := "Error: " + P.TokString(P.nextToken) + " @ " + P.pos.String() |
| 238 | if err != nil { |
| 239 | errmsg += " " + err.Error() |
| 240 | } else { |
| 241 | errmsg += ", expected one of: " |
| 242 | actRow := P.actTab[P.stack.Top()] |
| 243 | i := 0 |
| 244 | for t := range actRow.Actions { |
| 245 | errmsg += P.tokenMap.TokenString(t) |
| 246 | if i < len(actRow.Actions)-1 { |
| 247 | errmsg += " " |
| 248 | } |
| 249 | i++ |
| 250 | } |
| 251 | } |
| 252 | return errors.New(errmsg) |
| 253 | } |
| 254 | |
| 255 | func (P *Parser) TokString(tok *token.Token) string { |
| 256 | msg := P.tokenMap.TokenString(tok.Type) + "(" + strconv.Itoa(int(tok.Type)) + ")" |
| 257 | msg += " " + string(tok.Lit) |
| 258 | return msg |
| 259 | } |
| 260 | |
| 261 | func (this *Parser) Parse(scanner Scanner) (res interface{}, err error) { |
| 262 | this.Reset() |
| 263 | this.nextToken, this.pos = scanner.Scan() |
| 264 | for acc := false; !acc; { |
| 265 | action, ok := this.actTab[this.stack.Top()].Actions[this.nextToken.Type] |
| 266 | if !ok { |
| 267 | if recovered, errAttrib := this.Error(nil, scanner); !recovered { |
| 268 | this.nextToken, this.pos = errAttrib.ErrorToken, errAttrib.ErrorPos |
| 269 | return nil, this.newError(nil) |
| 270 | } |
| 271 | if action, ok = this.actTab[this.stack.Top()].Actions[this.nextToken.Type]; !ok { |
| 272 | panic("Error recover led to invalid action") |
| 273 | } |
| 274 | } |
| 275 | // fmt.Printf("S%d %s %s\n", this.stack.Top(), this.nextToken, action) |
| 276 | switch act := action.(type) { |
| 277 | case Accept: |
| 278 | res = this.stack.PopN(1)[0] |
| 279 | acc = true |
| 280 | case Shift: |
| 281 | this.stack.Push(State(act), this.nextToken) |
| 282 | this.nextToken, this.pos = scanner.Scan() |
| 283 | case Reduce: |
| 284 | prod := this.prodTab[int(act)] |
| 285 | attrib, err := prod.ReduceFunc(this.stack.PopN(prod.NumSymbols)) |
| 286 | if err != nil { |
| 287 | return nil, this.newError(err) |
| 288 | } else { |
| 289 | this.stack.Push(this.gotoTab[this.stack.Top()][prod.Head], attrib) |
| 290 | } |
| 291 | default: |
| 292 | panic("unknown action: " + action.String()) |
| 293 | } |
| 294 | } |
| 295 | return res, nil |
| 296 | } |