Debian Codegraph @

internal/parser/lr1/action/action.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 action
16
17 import (
18 "fmt"
19 )
20
21 type Action interface {
22 Equal(Action) bool
23 ResolveConflict(that Action) Action
24 String() string
25 }
26
27 type (
28 Accept bool
29 Error bool
30 Reduce int
31 Shift int
32 )
33
34 const (
35 ACCEPT = Accept(true)
36 ERROR = Error(true)
37 )
38
39 func (Accept) Equal(act Action) bool {
40 if _, ok := act.(Accept); ok {
41 return true
42 }
43 return false
44 }
45
46 func (Error) Equal(act Action) bool {
47 if _, ok := act.(Error); ok {
48 return true
49 }
50 return false
51 }
52
53 func (this Reduce) Equal(act Action) bool {
54 if that, ok := act.(Reduce); ok {
55 return this == that
56 }
57 return false
58 }
59
60 func (this Shift) Equal(act Action) bool {
61 if that, ok := act.(Shift); ok {
62 return this == that
63 }
64 return false
65 }
66
67 func (this Accept) ResolveConflict(that Action) Action {
68 if _, ok := that.(Error); ok {
69 return this
70 }
71 panic("Cannot have LR1 conflict with Accept.")
72 }
73
74 func (Error) ResolveConflict(that Action) Action {
75 return that
76 }
77
78 func (this Shift) ResolveConflict(that Action) Action {
79 switch that := that.(type) {
80 case Accept:
81 panic(fmt.Sprintf("Impossible conflict: Shift(%d)/Accept", int(this)))
82 case Shift:
83 panic(fmt.Sprintf("Cannot have Shift(%d)/Shift(%d)", int(this), int(that)))
84 case Error:
85 return this
86 case Reduce:
87 return this
88 }
89 panic(fmt.Sprintf("Conflict with unknown type of action: %T", that))
90 }
91
92 func (this Reduce) ResolveConflict(that Action) Action {
93 switch that := that.(type) {
94 case Accept:
95 panic(fmt.Sprintf("Impossible conflict: Shift(%d)/Accept", int(this)))
96 case Shift:
97 return that
98 case Error:
99 return this
100 case Reduce:
101 if this < that {
102 return this
103 }
104 return that
105 }
106 panic(fmt.Sprintf("Conflict with unknown type of action: %T", that))
107 }
108
109 func (this Accept) String() string {
110 return "accept"
111 }
112
113 func (this Error) String() string {
114 return "error"
115 }
116
117 func (this Reduce) String() string {
118 return fmt.Sprintf("Reduce(%d)", this)
119 }
120
121 func (this Shift) String() string {
122 return fmt.Sprintf("Shift(%d)", this)
123 }