Skip to content

Commit c89356c

Browse files
authored
Upgrade to JuliaSyntax version 1 (#186)
1 parent 5254f90 commit c89356c

6 files changed

Lines changed: 180 additions & 210 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Preferences = "21216c6a-2e73-6563-6e65-726566657250"
1010
runic = {}
1111

1212
[compat]
13-
JuliaSyntax = "0.4.10"
13+
JuliaSyntax = "1"
1414
Preferences = "1"
1515
julia = "1.10"
1616

src/Runic.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ struct Node
3535
# and not `Vector{GreenNode}`.
3636
head::JuliaSyntax.SyntaxHead
3737
span::UInt32
38-
kids::Union{Tuple{}, Vector{Node}}
38+
kids::Union{Vector{Node}, Nothing}
3939
# Metadata for the formatter
4040
tags::TagType
4141
end
4242

4343
function Node(head::JuliaSyntax.SyntaxHead, span::Integer, tags::Integer = 0)
44-
return Node(head, span % UInt32, (), tags % TagType)
44+
return Node(head, span % UInt32, nothing, tags % TagType)
4545
end
4646

4747
function Node(head::JuliaSyntax.SyntaxHead, kids::Vector{Node})
@@ -55,15 +55,15 @@ function Node(node::JuliaSyntax.GreenNode)
5555
# juliac: this is `kids = map(Node, JuliaSyntax.children(node))` but written out like
5656
# this in order to help inference.
5757
children = JuliaSyntax.children(node)
58-
if children isa Tuple{}
59-
kids = ()
58+
if children === nothing
59+
return Node(JuliaSyntax.head(node), JuliaSyntax.span(node), nothing, tags)
6060
else
6161
kids = Vector{Node}(undef, length(children))
6262
for (i, child) in pairs(children)
6363
kids[i] = Node(child)
6464
end
65+
return Node(JuliaSyntax.head(node), JuliaSyntax.span(node), kids, tags)
6566
end
66-
return Node(JuliaSyntax.head(node), JuliaSyntax.span(node), kids, tags)
6767
end
6868

6969
function Base.show(io::IO, ::MIME"text/plain", node::Node)
@@ -94,11 +94,11 @@ kind(node::Node) = JuliaSyntax.kind(node)
9494

9595
# Inverse of JuliaSyntax.haschildren
9696
function is_leaf(node::Node)
97-
return node.kids === ()
97+
return node.kids === nothing
9898
end
9999

100100
# This function must only be be called after verifying that the node is not a leaf. We can
101-
# then type-assert the return value to narrow it down from `Union{Tuple{}, Vector{Node}}` to
101+
# then type-assert the return value to narrow it down from `Union{Vector{Node}, Nothing}` to
102102
# `Vector{Node}`.
103103
function verified_kids(node::Node)
104104
@assert !is_leaf(node)

src/chisels.jl

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ end
419419

420420
function make_node(node::Node, span′::Integer, tags = node.tags)
421421
@assert is_leaf(node)
422-
return Node(head(node), span′, (), tags)
422+
return Node(head(node), span′, nothing, tags)
423423
end
424424

425425
# TODO: Remove?
@@ -540,7 +540,7 @@ end
540540
# Assignment node but exclude loop variable assignment
541541
function is_variable_assignment(ctx, node::Node)
542542
return !is_leaf(node) && is_assignment(node) &&
543-
!(ctx.lineage_kinds[end] in KSet"for generator cartesian_iterator filter")
543+
!(ctx.lineage_kinds[end] in KSet"for generator filter")
544544
end
545545

546546
# K"global" and K"local" nodes can be either `global a, b, c` or `global a = 1`. This method
@@ -587,29 +587,24 @@ function is_infix_op_call(node::Node)
587587
return kind(node) in KSet"call dotcall" && JuliaSyntax.is_infix_op_call(node)
588588
end
589589

590-
# Extract the operator of an infix op call node
591-
function infix_op_call_op(node::Node)
592-
@assert is_infix_op_call(node) || kind(node) === K"||"
590+
# Extract the specific operator kind by reading the source bytes. In JuliaSyntax version 1
591+
# operators are just K"Identifier" so we can't look at the kind directly.
592+
# Note: ctx.fmt_io must be positioned at the start of node when this is called.
593+
function infix_op_call_op(ctx, node::Node)
593594
kids = verified_kids(node)
594595
first_operand_index = findfirst(!JuliaSyntax.is_whitespace, kids)::Int
595-
op_index = findnext(JuliaSyntax.is_operator, kids, first_operand_index + 1)::Int
596-
return kids[op_index]
597-
end
598-
599-
# Comparison leaf or a dotted comparison leaf (.<)
600-
function is_comparison_leaf(node::Node)
601-
if is_leaf(node) && JuliaSyntax.is_prec_comparison(node)
602-
return true
603-
elseif !is_leaf(node) && kind(node) === K"." &&
604-
meta_nargs(node) == 2 && is_comparison_leaf(verified_kids(node)[2])
605-
return true
606-
else
607-
return false
596+
op_index = findnext(!JuliaSyntax.is_whitespace, kids, first_operand_index + 1)::Int
597+
off = position(ctx.fmt_io)
598+
for i in 1:(op_index - 1)
599+
off += span(kids[i])
608600
end
609-
end
610-
611-
function is_operator_leaf(node::Node)
612-
return is_leaf(node) && JuliaSyntax.is_operator(node)
601+
pos = position(ctx.fmt_io)
602+
seek(ctx.fmt_io, off)
603+
bytes = read(ctx.fmt_io, span(kids[op_index]))
604+
seek(ctx.fmt_io, pos)
605+
str = String(bytes)
606+
i = get(JuliaSyntax._kind_str_to_int, str, nothing)
607+
return i === nothing ? K"Identifier" : JuliaSyntax.Kind(i)
613608
end
614609

615610
function first_non_whitespace_kid(node::Node)
@@ -783,7 +778,7 @@ function is_string_macro(node)
783778
@assert !is_leaf(node)
784779
kids = verified_kids(node)
785780
return length(kids) >= 2 &&
786-
kind(kids[1]) in KSet"StringMacroName CmdMacroName core_@cmd" &&
781+
kind(kids[1]) in KSet"StringMacroName CmdMacroName" &&
787782
kind(kids[2]) in KSet"string cmdstring"
788783
end
789784

@@ -796,7 +791,7 @@ function is_triple_string_macro(node)
796791
if kind(node) === K"macrocall"
797792
kids = verified_kids(node)
798793
if length(kids) >= 2 &&
799-
kind(kids[1]) in KSet"StringMacroName CmdMacroName core_@cmd" &&
794+
kind(kids[1]) in KSet"StringMacroName CmdMacroName" &&
800795
is_triple_string(kids[2])
801796
return true
802797
end
@@ -806,7 +801,17 @@ end
806801

807802
function is_triple_thing(node)
808803
return is_triple_string(node) || is_triple_string_macro(node) ||
809-
(kind(node) === K"juxtapose" && is_triple_string_macro(verified_kids(node)[1]))
804+
(kind(node) === K"juxtapose" && is_triple_string(verified_kids(node)[1]))
805+
end
806+
807+
function is_any_op_call(node)
808+
return JuliaSyntax.is_infix_op_call(node) || JuliaSyntax.is_prefix_op_call(node) ||
809+
JuliaSyntax.is_postfix_op_call(node)
810+
end
811+
812+
function is_short_form_function_definition(node)
813+
return kind(node) === K"function" &&
814+
JuliaSyntax.has_flags(node, JuliaSyntax.SHORT_FORM_FUNCTION_FLAG)
810815
end
811816

812817
# Check whether the sequence of kinds in `kinds` exist in `kids` starting at index `i`.
@@ -826,7 +831,7 @@ end
826831
function macrocall_name(ctx, node)
827832
@assert kind(node) === K"macrocall"
828833
kids = verified_kids(node)
829-
pred = x -> kind(x) in KSet"MacroName StringMacroName CmdMacroName core_@cmd"
834+
pred = x -> kind(x) in KSet"MacroName StringMacroName CmdMacroName"
830835
mkind = kind(first_leaf_predicate(node, pred)::Node)
831836
if kmatch(kids, KSet"@ MacroName")
832837
p = position(ctx.fmt_io)
@@ -842,10 +847,6 @@ function macrocall_name(ctx, node)
842847
append!(bytes, "_str")
843848
end
844849
return String(bytes)
845-
elseif kmatch(kids, KSet"core_@cmd")
846-
bytes = read_bytes(ctx, kids[1])
847-
@assert length(bytes) == 0
848-
return "core_@cmd"
849850
else
850851
# Don't bother looking in more complex expressions, just return unknown
851852
return "<unknown macro>"

src/juliac.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ function printstyled_juliac(io::RawIO, str::String; bold = false, color::Symbol
5252
end
5353

5454
function isatty(io::RawIO)
55-
return (@ccall isatty(io.fd::Cint)::Cint) == 1
55+
@static if Sys.iswindows()
56+
return (@ccall _isatty(io.fd::Cint)::Cint) == 1
57+
else
58+
return (@ccall isatty(io.fd::Cint)::Cint) == 1
59+
end
5660
end
5761
supports_color(io::RawIO) = isatty(io)
5862

0 commit comments

Comments
 (0)