Skip to content

Commit 22aeb3a

Browse files
committed
work around CSTParser type piracy
1 parent a1b4cdb commit 22aeb3a

File tree

5 files changed

+27
-26
lines changed

5 files changed

+27
-26
lines changed

Manifest.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
[[Base64]]
44
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
55

6+
[[CSTParser]]
7+
deps = ["Tokenize"]
8+
git-tree-sha1 = "376a39f1862000442011390f1edf5e7f4dcc7142"
9+
uuid = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
10+
version = "0.6.0"
11+
612
[[Compat]]
713
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
814
git-tree-sha1 = "84aa74986c5b9b898b0d1acaf3258741ee64754f"

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name = "MacroTools"
22
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
33

44
[deps]
5+
CSTParser = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
56
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
67
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
78
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

src/MacroTools.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ include("examples/forward.jl")
1818

1919
include("cstparser/CSTParser.jl")
2020

21+
module hack; using CSTParser; end
22+
2123
using .CSTParser
2224

2325
include("patch/diff.jl")

src/cstparser/components/operators.jl

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ precedence(x::AbstractToken) = precedence(x.kind)
4343
precedence(x::OPERATOR) = precedence(x.kind)
4444

4545

46-
isoperator(kind) = Tokens.begin_ops < kind < Tokens.end_ops
47-
isoperator(t::AbstractToken) = isoperator(t.kind)
48-
49-
5046
isunaryop(op) = false
5147
isunaryop(op::OPERATOR) = isunaryop(op.kind)
5248
isunaryop(t::AbstractToken) = isunaryop(t.kind)
@@ -243,7 +239,7 @@ end
243239
# Parse power (special case for preceding unary ops)
244240
function parse_operator_power(ps::ParseState, @nospecialize(ret), op)
245241
nextarg = @precedence ps PowerOp - LtoR(PowerOp) @closer ps inwhere parse_expression(ps)
246-
242+
247243
if ret isa UnaryOpCall
248244
nextarg = BinaryOpCall(ret.arg, op, nextarg)
249245
ret = UnaryOpCall(ret.op, nextarg)
@@ -257,7 +253,7 @@ end
257253
# parse where
258254
function parse_operator_where(ps::ParseState, @nospecialize(ret), op)
259255
nextarg = @precedence ps LazyAndOp @closer ps inwhere parse_expression(ps)
260-
256+
261257
if nextarg isa EXPR{Braces}
262258
args = nextarg.args
263259
else
@@ -286,7 +282,7 @@ function parse_operator_dot(ps::ParseState, @nospecialize(ret), op)
286282
if ps.nt.kind == Tokens.LPAREN
287283
nextarg = @closeparen ps @precedence ps DotOp - LtoR(DotOp) parse_expression(ps)
288284
nextarg = EXPR{Quote}(Any[op2, nextarg])
289-
else
285+
else
290286
nextarg = @precedence ps DotOp - LtoR(DotOp) parse_unary(ps, op2)
291287
end
292288
elseif ps.nt.kind == Tokens.EX_OR && ps.nnt.kind == Tokens.LPAREN
@@ -312,7 +308,7 @@ end
312308

313309
function parse_operator_anon_func(ps::ParseState, @nospecialize(ret), op)
314310
arg = @closer ps comma @precedence ps 0 parse_expression(ps)
315-
311+
316312
if !(arg isa EXPR{Begin} || (arg isa EXPR{InvisBrackets} && arg.args[2] isa EXPR{Block}))
317313
arg = EXPR{Block}(Any[arg])
318314
end
@@ -352,7 +348,7 @@ function parse_operator(ps::ParseState, @nospecialize(ret), op)
352348
else
353349
ltor = K == Tokens.LPIPE ? true : LtoR(P)
354350
nextarg = @precedence ps P - ltor parse_expression(ps)
355-
351+
356352
if issyntaxcall(op)
357353
ret = BinarySyntaxOpCall(ret, op, nextarg)
358354
else

src/cstparser/utils.jl

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function closer(ps::ParseState)
99
ps.nt.kind == Tokens.LBRACE ||
1010
ps.nt.kind == Tokens.LSQUARE ||
1111
(ps.nt.kind == Tokens.STRING && isemptyws(ps.ws)) ||
12-
((ps.nt.kind == Tokens.RPAREN || ps.nt.kind == Tokens.RSQUARE) && isidentifier(ps.nt))
12+
((ps.nt.kind == Tokens.RPAREN || ps.nt.kind == Tokens.RSQUARE) && isidentifier(ps.nt))
1313
)) ||
1414
(ps.nt.kind == Tokens.COMMA && ps.closer.precedence > 0) ||
1515
ps.nt.kind == Tokens.ENDMARKER ||
@@ -20,10 +20,10 @@ function closer(ps::ParseState)
2020
(ps.closer.paren && ps.nt.kind == Tokens.RPAREN) ||
2121
(ps.closer.brace && ps.nt.kind == Tokens.RBRACE) ||
2222
(ps.closer.square && ps.nt.kind == Tokens.RSQUARE) ||
23-
ps.nt.kind == Tokens.ELSEIF ||
23+
ps.nt.kind == Tokens.ELSEIF ||
2424
ps.nt.kind == Tokens.ELSE ||
25-
ps.nt.kind == Tokens.CATCH ||
26-
ps.nt.kind == Tokens.FINALLY ||
25+
ps.nt.kind == Tokens.CATCH ||
26+
ps.nt.kind == Tokens.FINALLY ||
2727
(ps.closer.ifop && isoperator(ps.nt) && (precedence(ps.nt) <= 0 || ps.nt.kind == Tokens.COLON)) ||
2828
(ps.closer.range && (ps.nt.kind == Tokens.FOR || iscomma(ps.nt) || ps.nt.kind == Tokens.IF)) ||
2929
(ps.closer.ws && !isemptyws(ps.ws) &&
@@ -32,7 +32,7 @@ function closer(ps::ParseState)
3232
!(!ps.closer.inmacro && ps.nt.kind == Tokens.FOR) &&
3333
!(ps.nt.kind == Tokens.DO) &&
3434
!(
35-
(isbinaryop(ps.nt) && !(isemptyws(ps.nws) && isunaryop(ps.nt) && ps.closer.wsop)) ||
35+
(isbinaryop(ps.nt) && !(isemptyws(ps.nws) && isunaryop(ps.nt) && ps.closer.wsop)) ||
3636
(isunaryop(ps.t) && ps.ws.kind == WS)
3737
)) ||
3838
(ps.nt.startbyte ps.closer.stop) ||
@@ -209,8 +209,6 @@ end
209209

210210
isidentifier(t::AbstractToken) = t.kind == Tokens.IDENTIFIER
211211

212-
isliteral(t::AbstractToken) = Tokens.begin_literal < t.kind < Tokens.end_literal
213-
214212
isbool(t::AbstractToken) = Tokens.TRUE t.kind Tokens.FALSE
215213
iscomma(t::AbstractToken) = t.kind == Tokens.COMMA
216214

@@ -224,7 +222,7 @@ isinstance(t::AbstractToken) = isidentifier(t) ||
224222

225223
ispunctuation(t::AbstractToken) = t.kind == Tokens.COMMA ||
226224
t.kind == Tokens.END ||
227-
Tokens.LSQUARE t.kind Tokens.RPAREN ||
225+
Tokens.LSQUARE t.kind Tokens.RPAREN ||
228226
t.kind == Tokens.AT_SIGN
229227

230228
isstring(x) = false
@@ -235,7 +233,7 @@ is_float(x) = x isa LITERAL && x.kind == Tokens.FLOAT
235233
is_number(x) = x isa LITERAL && (x.kind == Tokens.INTEGER || x.kind == Tokens.FLOAT)
236234
is_nothing(x) = x isa LITERAL && x.kind == Tokens.NOTHING
237235

238-
isajuxtaposition(ps::ParseState, ret) = ((is_number(ret) && (ps.nt.kind == Tokens.IDENTIFIER || ps.nt.kind == Tokens.LPAREN || ps.nt.kind == Tokens.CMD || ps.nt.kind == Tokens.STRING || ps.nt.kind == Tokens.TRIPLE_STRING)) ||
236+
isajuxtaposition(ps::ParseState, ret) = ((is_number(ret) && (ps.nt.kind == Tokens.IDENTIFIER || ps.nt.kind == Tokens.LPAREN || ps.nt.kind == Tokens.CMD || ps.nt.kind == Tokens.STRING || ps.nt.kind == Tokens.TRIPLE_STRING)) ||
239237
((ret isa UnarySyntaxOpCall && is_prime(ret.arg2) && ps.nt.kind == Tokens.IDENTIFIER) ||
240238
((ps.t.kind == Tokens.RPAREN || ps.t.kind == Tokens.RSQUARE) && (ps.nt.kind == Tokens.IDENTIFIER || ps.nt.kind == Tokens.CMD)) ||
241239
((ps.t.kind == Tokens.STRING || ps.t.kind == Tokens.TRIPLE_STRING) && (ps.nt.kind == Tokens.STRING || ps.nt.kind == Tokens.TRIPLE_STRING)))) || ((ps.t.kind in (Tokens.INTEGER, Tokens.FLOAT) || ps.t.kind in (Tokens.RPAREN,Tokens.RSQUARE,Tokens.RBRACE)) && ps.nt.kind == Tokens.IDENTIFIER)
@@ -386,7 +384,7 @@ function check_file(file, ret, neq)
386384
str = read(file, String)
387385
x0, cstfailed, sp = cst_parsefile(str)
388386
x1, flispfailed = flisp_parsefile(str)
389-
387+
390388
print("\r ")
391389
if !isempty(sp)
392390
printstyled(file, color = :blue)
@@ -408,7 +406,7 @@ function check_file(file, ret, neq)
408406
printstyled(string(" ", c1), bold = true, color = :light_green)
409407
println()
410408
push!(ret, (file, :noteq))
411-
end
409+
end
412410
end
413411

414412
function check_base(dir = dirname(Base.find_source_file("essentials.jl")), display = false)
@@ -428,7 +426,7 @@ function check_base(dir = dirname(Base.find_source_file("essentials.jl")), displ
428426
N += 1
429427
try
430428
print("\r", rpad(string(N), 5), rpad(string(round(fail / N * 100, sigdigits = 3)), 8), rpad(string(round(err / N * 100, sigdigits = 3)), 8), rpad(string(round(neq / N * 100, sigdigits = 3)), 8))
431-
429+
432430
check_file(file, ret, neq)
433431
catch er
434432
isa(er, InterruptException) && rethrow(er)
@@ -495,15 +493,15 @@ of its components. Returns a vector of failing expressions.
495493
function check_span(x::EXPR{StringH}, neq = []) end
496494
function check_span(x::T, neq = []) where T <: Union{IDENTIFIER,LITERAL,OPERATOR,KEYWORD,PUNCTUATION} neq end
497495

498-
function check_span(x::UnaryOpCall, neq = [])
496+
function check_span(x::UnaryOpCall, neq = [])
499497
check_span(x.op)
500498
check_span(x.arg)
501499
if x.op.fullspan + x.arg.fullspan != x.fullspan
502500
push!(neq, x)
503501
end
504502
neq
505503
end
506-
function check_span(x::UnarySyntaxOpCall, neq = [])
504+
function check_span(x::UnarySyntaxOpCall, neq = [])
507505
check_span(x.arg1)
508506
check_span(x.arg2)
509507
if x.arg1.fullspan + x.arg2.fullspan != x.fullspan
@@ -619,7 +617,7 @@ Base.iterate(x::BinaryOpCall, s) = s > 2 ? nothing : (getfield(x, s + 1), s + 1)
619617
Base.length(x::BinaryOpCall) = 3
620618

621619
Base.iterate(x::WhereOpCall) = x.arg1, 1
622-
function Base.iterate(x::WhereOpCall, s)
620+
function Base.iterate(x::WhereOpCall, s)
623621
if s == 1
624622
return x.op, 2
625623
elseif s < length(x)
@@ -703,5 +701,3 @@ function _unescape_string(io, s::AbstractString)
703701
end
704702
end
705703
end
706-
707-

0 commit comments

Comments
 (0)