This repository has been archived by the owner on Mar 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EventHandler.jl
153 lines (141 loc) · 2.87 KB
/
EventHandler.jl
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
module EventHandler
asts = []
blocks = []
line = 0
function readSourceToAST(file)
global asts
global blocks
line = 0
s = ""
# recording blocks such as a function
block_start = 0
block_end = 0
open(file) do f
for ln in eachline(f)
line += 1
s *= ln
ex = parseInputLine(s)
if (isa(ex, Expr) && ex.head === :incomplete)
s *= "\n"
if block_start == 0
block_start = line
end
continue
else
if block_start != 0
# add block line postion
block_end = line
push!(blocks, (block_start, block_end))
block_start = 0
end
ast = Meta.lower(Main, ex)
push!(asts, ast)
s = ""
end
end
end
end
# run whole program
# return a status contains ast, current_line
function run()
global asts
global line
global blocks
for ast in asts
try
updateLine()
Core.eval(Main, ast)
catch err
# if we run to a breakpoint
# we will catch an exception and collect info
return
end
end
# exit normally
return ([], 0)
end
function next()
global asts
global line
ast_index = getAstIndex()
if ast_index == lastindex(asts)
return false
end
while true
try
updateLine()
ast_index = getAstIndex()
Core.eval(Main, asts[ast_index])
catch err
# if we meet a breakpoint
# we just ignore it
println("Error in next")
return false
#continue
end
break
end
return true
end
function continous(status)
global asts
global line
ast_index = getAstIndex()
for ast in asts[ast_index + 1 : end]
try
updateLine()
Core.eval(Main, ast)
catch err
# catch breakpoint
return
end
end
# exit normally
return
end
# update line for run/next/continous call
function updateLine()
global blocks
global line
for range in blocks
if line == range[1]
line += range[2] - range[1] + 1
return
elseif range[1] > line
break
end
end
line += 1
end
# get AstIndex from current line number
function getAstIndex()
global blocks
global line
global asts
base = line
ofs = 0
for block in blocks
if block[1] <= line <= block[2]
base = block[1]
break
elseif block[1] > line
break
end
ofs += block[2] - block[1]
end
return base - ofs
end
function parseInputLine(s::String; filename::String="none", depwarn=true)
# For now, assume all parser warnings are depwarns
ex = if depwarn
ccall(:jl_parse_input_line, Any, (Ptr{UInt8}, Csize_t, Ptr{UInt8}, Csize_t),
s, sizeof(s), filename, sizeof(filename))
else
with_logger(NullLogger()) do
ccall(:jl_parse_input_line, Any, (Ptr{UInt8}, Csize_t, Ptr{UInt8}, Csize_t),
s, sizeof(s), filename, sizeof(filename))
end
end
return ex
end
end # module EventHandler