Skip to content

Commit 7e28e09

Browse files
mlechuc42f
andcommitted
Apply suggestions from code review
Co-authored-by: Claire Foster <[email protected]>
1 parent 668021c commit 7e28e09

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/syntax_graph.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function freeze_attrs(graph::SyntaxGraph)
2222
SyntaxGraph(graph.edge_ranges, graph.edges, frozen_attrs)
2323
end
2424

25+
# Create a copy of `graph` where the attribute list is mutable
2526
function unfreeze_attrs(graph::SyntaxGraph)
2627
unfrozen_attrs = Dict{Symbol,Any}(pairs(graph.attributes)...)
2728
SyntaxGraph(graph.edge_ranges, graph.edges, unfrozen_attrs)
@@ -38,8 +39,8 @@ function attrnames(graph::SyntaxGraph)
3839
keys(graph.attributes)
3940
end
4041

41-
function attrtypes(graph::SyntaxGraph)
42-
[(k, typeof(v).parameters[2]) for (k, v) in pairs(graph.attributes)]
42+
function attrdefs(graph::SyntaxGraph)
43+
[(k=>typeof(v).parameters[2]) for (k, v) in pairs(graph.attributes)]
4344
end
4445

4546
function Base.show(io::IO, ::MIME"text/plain", graph::SyntaxGraph)
@@ -48,19 +49,24 @@ function Base.show(io::IO, ::MIME"text/plain", graph::SyntaxGraph)
4849
_show_attrs(io, graph.attributes)
4950
end
5051

51-
function ensure_attributes!(graph::SyntaxGraph{<:Dict}; kws...)
52+
function ensure_attributes!(graph::SyntaxGraph; kws...)
5253
for (k,v) in pairs(kws)
5354
@assert k isa Symbol
5455
@assert v isa Type
5556
if haskey(graph.attributes, k)
5657
v0 = valtype(graph.attributes[k])
5758
v == v0 || throw(ErrorException("Attribute type mismatch $v != $v0"))
59+
elseif graph.attributes isa NamedTuple
60+
throw(ErrorException("""
61+
ensure_attributes!: $k is not an existing attribute, and the graph's attributes are frozen. \
62+
Consider calling non-mutating `ensure_attributes` instead."""))
5863
else
5964
graph.attributes[k] = Dict{NodeId,v}()
6065
end
6166
end
6267
graph
6368
end
69+
6470
function ensure_attributes(graph::SyntaxGraph{<:Dict}; kws...)
6571
g = unfreeze_attrs(graph)
6672
ensure_attributes!(g; kws...)
@@ -435,7 +441,7 @@ end
435441

436442
const SourceAttrType = Union{SourceRef,LineNumberNode,NodeId,Tuple}
437443

438-
function SyntaxTree(graph::SyntaxGraph{<:Dict}, node::SyntaxNode)
444+
function SyntaxTree(graph::SyntaxGraph, node::SyntaxNode)
439445
ensure_attributes!(graph, kind=Kind, syntax_flags=UInt16, source=SourceAttrType,
440446
value=Any, name_val=String)
441447
id = _convert_nodes(graph, node)

test/syntax_graph.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
@test gf2.attributes isa NamedTuple
1717
@test gu2.attributes isa Dict
1818
# does its job
19-
@test (:test_attr, Symbol) in JuliaLowering.attrtypes(gf2)
20-
@test (:foo, Type) in JuliaLowering.attrtypes(gf2)
19+
@test (:test_attr=>Symbol) in JuliaLowering.attrdefs(gf2)
20+
@test (:foo=>Type) in JuliaLowering.attrdefs(gf2)
2121
@test Set(keys(gf2.attributes)) == Set(keys(gu2.attributes))
2222
# no mutation
23-
@test !((:test_attr, Symbol) in JuliaLowering.attrtypes(gf1))
24-
@test !((:foo, Type) in JuliaLowering.attrtypes(gf1))
23+
@test !((:test_attr=>Symbol) in JuliaLowering.attrdefs(gf1))
24+
@test !((:foo=>Type) in JuliaLowering.attrdefs(gf1))
2525
@test Set(keys(gf1.attributes)) == Set(keys(gu1.attributes))
2626

2727
# delete_attributes
@@ -31,12 +31,12 @@
3131
@test gf3.attributes isa NamedTuple
3232
@test gu3.attributes isa Dict
3333
# does its job
34-
@test !((:test_attr, Symbol) in JuliaLowering.attrtypes(gf3))
35-
@test !((:foo, Type) in JuliaLowering.attrtypes(gf3))
34+
@test !((:test_attr=>Symbol) in JuliaLowering.attrdefs(gf3))
35+
@test !((:foo=>Type) in JuliaLowering.attrdefs(gf3))
3636
@test Set(keys(gf3.attributes)) == Set(keys(gu3.attributes))
3737
# no mutation
38-
@test (:test_attr, Symbol) in JuliaLowering.attrtypes(gf2)
39-
@test (:foo, Type) in JuliaLowering.attrtypes(gf2)
38+
@test (:test_attr=>Symbol) in JuliaLowering.attrdefs(gf2)
39+
@test (:foo=>Type) in JuliaLowering.attrdefs(gf2)
4040
@test Set(keys(gf2.attributes)) == Set(keys(gu2.attributes))
4141
end
4242

0 commit comments

Comments
 (0)