-
Notifications
You must be signed in to change notification settings - Fork 45
/
debugger.go
248 lines (222 loc) · 5.84 KB
/
debugger.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package eval
import (
"bufio"
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
"github.com/sirupsen/logrus"
"github.com/antlr/antlr4/runtime/Go/antlr"
parser "github.com/anz-bank/sysl/pkg/grammar"
"github.com/anz-bank/sysl/pkg/parse"
sysl "github.com/anz-bank/sysl/pkg/sysl"
)
type DebugFunc func(scope *Scope, app *sysl.Application, expr *sysl.Expr) error
type repl struct {
input *bufio.Scanner
output io.Writer
breakpoints []*sysl.SourceContext
step bool
lastCommand string
}
func (r *repl) run(scope *Scope, app *sysl.Application, expr *sysl.Expr) error {
if expr == nil || r.step || r.isBreakpoint(expr) {
return r.handleInput(scope, app, expr)
}
return nil
}
func (r *repl) handleInput(scope *Scope, app *sysl.Application, expr *sysl.Expr) error {
for {
if expr != nil && expr.SourceContext.Text != "" {
writeOutput(expr.SourceContext.Text+"\n", r.output)
}
writeOutput("sysl> ", r.output)
if !r.input.Scan() {
r.step = false
return fmt.Errorf("input closed")
}
text := r.input.Text()
if len(text) == 0 {
text = r.lastCommand
}
switch len(text) {
case 0:
printUsage(r.output)
case 1:
r.lastCommand = text
switch text[0] {
case 's':
writeOutput("(step)\n", r.output)
r.step = true
return nil
case 'c':
writeOutput("(continue)\n", r.output)
r.step = false
return nil
case 'd':
writeOutput("(dump scope)\n", r.output)
dumpScope(r.output, *scope)
case 'l':
writeOutput("(list)\n", r.output)
if app != nil {
dumpApp(r.output, app)
}
case 'q':
writeOutput("(quit)\n", r.output)
os.Exit(0)
case '?', 'h':
printUsage(r.output)
default:
writeOutput(parseExpression(text, app, scope), r.output)
}
default:
writeOutput(parseExpression(text, app, scope), r.output)
}
}
}
func (r *repl) isBreakpoint(expr *sysl.Expr) bool {
if expr.SourceContext == nil {
return false
}
sc := expr.SourceContext
for _, bsc := range r.breakpoints {
if strings.HasSuffix(sc.File, bsc.File) &&
sc.Start.Line == bsc.Start.Line &&
(sc.Start.Col == bsc.Start.Col || bsc.Start.Col < 0) {
return true
}
}
return false
}
// Breakpoint format:
// filename:line[:col]
func makeBreakpoint(from string) *sysl.SourceContext {
parts := strings.Split(from, ":")
if len(parts) >= 2 {
line, err := strconv.ParseInt(parts[1], 10, 32)
if err != nil {
return nil
}
res := &sysl.SourceContext{
File: parts[0],
Start: &sysl.SourceContext_Location{
Line: int32(line),
Col: -1,
},
}
if len(parts) >= 3 {
col, err := strconv.ParseInt(parts[2], 10, 32)
if err != nil {
return nil
}
res.Start.Col = int32(col)
}
return res
}
return nil
}
func printUsage(out io.Writer) {
text := `
?, h This help text
d Dump the current scope
c Continue execution until the next breakpoint
s Step execution
q quit
EXPRESSION Print the value of the supplied EXPRESSION
`
writeOutput(text, out)
}
func dumpScope(out io.Writer, scope Scope) {
names := make([]string, 0, len(scope))
for k := range scope {
names = append(names, k)
}
sort.Strings(names)
lines := make([]string, 0, len(scope))
for _, k := range names {
l := fmt.Sprintf(" %s: %s", k, UnaryString(scope[k]).GetS())
lines = append(lines, l)
}
writeOutput(strings.Join(lines, "\n")+"\n", out)
}
func dumpApp(out io.Writer, app *sysl.Application) {
writeOutput(" App -> "+app.Name.String()+"\n", out)
writeOutput(" -- views --\n", out)
for _, view := range app.Views {
writeOutput(strings.Split(view.SourceContext.Text, "\n")[0]+"\n", out)
}
}
func parseExpression(text string, app *sysl.Application, scope *Scope) string {
errorListener := parse.SyslParserErrorListener{}
lexer := parser.NewThreadSafeSyslLexer(antlr.NewInputStream(text))
lexer.SetMode(parser.SyslLexerVIEW_TRANSFORM)
defer parser.DeleteLexerState(lexer)
stream := antlr.NewCommonTokenStream(lexer, 0)
listener := parse.NewTreeShapeListener()
p := parser.NewThreadSafeSyslParser(stream)
p.AddErrorListener(&errorListener)
p.BuildParseTrees = true
defer func() {
_ = recover() //nolint:errcheck
}()
var res *sysl.Expr
ee := exprEval{
txApp: app,
exprStack: exprStack{[]*exprData{{e: &sysl.Expr{}}}},
logger: logrus.StandardLogger(),
// needed so the eval doesnt os.Exit()
dbg: func(scope *Scope, app *sysl.Application, expr *sysl.Expr) error { return nil },
}
// Need to figure out what sort of expression this is going to be
lookAhead := []int{stream.LA(1), stream.LA(2)}
if lookAhead[0] == parser.SyslLexerE_LET || lookAhead[1] == parser.SyslLexerE_EQ {
// Need to push a transform onto the expression stack
transform := &sysl.Expr_Transform{}
listener.PushExpr(&sysl.Expr{Expr: &sysl.Expr_Transform_{Transform: transform}})
var tree antlr.Tree
if lookAhead[0] == parser.SyslLexerE_LET {
tree = p.Expr_let_statement()
} else {
tree = p.Expr_simple_assign()
}
antlr.NewParseTreeWalker().Walk(listener, tree)
xformres := evalTransformStmts(&ee, *scope, transform)
for k, v := range xformres.GetMap().Items {
(*scope)[k] = v
}
return ""
}
antlr.NewParseTreeWalker().Walk(listener, p.Expr())
res = listener.TopExpr()
// There should be a single Expression in the listener,
// Take it and force it to a string for output
expr := &sysl.Expr{
Expr: &sysl.Expr_Unexpr{
Unexpr: &sysl.Expr_UnExpr{
Op: sysl.Expr_UnExpr_STRING,
Arg: res,
},
},
}
val, err := ee.eval(*scope, expr)
if err != nil {
return err.Error()
}
return val.GetS() + "\n"
}
func NewREPL(input io.Reader, output io.Writer) DebugFunc {
r := repl{
input: bufio.NewScanner(input),
output: output,
breakpoints: []*sysl.SourceContext{},
step: true,
}
return r.run
}
func writeOutput(text string, to io.Writer) {
if _, err := io.WriteString(to, text); err != nil {
logrus.Errorf("Failed to write to output. %s", err.Error())
}
}