@@ -22,6 +22,7 @@ function freeze_attrs(graph::SyntaxGraph)
2222 SyntaxGraph (graph. edge_ranges, graph. edges, frozen_attrs)
2323end
2424
25+ # Create a copy of `graph` where the attribute list is mutable
2526function 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)
3940end
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)]
4344end
4445
4546function 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)
4950end
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
6368end
69+
6470function ensure_attributes (graph:: SyntaxGraph{<:Dict} ; kws... )
6571 g = unfreeze_attrs (graph)
6672 ensure_attributes! (g; kws... )
0 commit comments