Skip to content

Commit 05a1fec

Browse files
committed
style: apply runic
1 parent ca7a41f commit 05a1fec

File tree

6 files changed

+221
-149
lines changed

6 files changed

+221
-149
lines changed

src/HerbCore.jl

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,35 @@ include("constraint.jl")
88
include("indexing.jl")
99

1010
export
11-
AbstractRuleNode,
12-
RuleNode,
13-
@rulenode,
14-
AbstractHole,
15-
AbstractUniformHole,
16-
UniformHole,
17-
Hole,
18-
HoleReference, depth,
19-
node_depth,
20-
rulesoftype,
21-
contains_index,
22-
swap_node,
23-
get_rulesequence,
24-
rulesonleft,
25-
get_node_at_location,
26-
get_path,
27-
number_of_holes,
28-
contains_hole,
29-
contains_nonuniform_hole,
30-
get_children,
31-
get_rule,
32-
isuniform,
33-
isfilled,
34-
hasdynamicvalue,
35-
have_same_shape, AbstractConstraint,
36-
AbstractGrammar,
37-
print_tree,
38-
update_rule_indices!,
39-
is_domain_valid,
40-
issame
11+
AbstractRuleNode,
12+
RuleNode,
13+
@rulenode,
14+
AbstractHole,
15+
AbstractUniformHole,
16+
UniformHole,
17+
Hole,
18+
HoleReference, depth,
19+
node_depth,
20+
rulesoftype,
21+
contains_index,
22+
swap_node,
23+
get_rulesequence,
24+
rulesonleft,
25+
get_node_at_location,
26+
get_path,
27+
number_of_holes,
28+
contains_hole,
29+
contains_nonuniform_hole,
30+
get_children,
31+
get_rule,
32+
isuniform,
33+
isfilled,
34+
hasdynamicvalue,
35+
have_same_shape, AbstractConstraint,
36+
AbstractGrammar,
37+
print_tree,
38+
update_rule_indices!,
39+
is_domain_valid,
40+
issame
4141

4242
end # module HerbCore

src/grammar.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function Base.show(io::IO, grammar::AbstractGrammar)
2424
for i in eachindex(grammar.rules)
2525
println(io, i, ": ", grammar.types[i], " = ", grammar.rules[i])
2626
end
27+
return
2728
end
2829

2930
Base.getindex(grammar::AbstractGrammar, typ::Symbol) = grammar.bytype[typ]

src/indexing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function is_domain_valid end
2626
2727
"""
2828
function issame(a, b)
29-
a == b
29+
return a == b
3030
end
3131

3232
Base.@deprecate issame Base.:(==) false

src/rulenode.jl

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ function update_rule_indices!(node::RuleNode, n_rules::Integer)
6060
for child in children
6161
update_rule_indices!(child, n_rules)
6262
end
63+
return
6364
end
6465

6566
"""
@@ -76,7 +77,7 @@ function update_rule_indices!(
7677
node::RuleNode,
7778
n_rules::Integer,
7879
mapping::AbstractDict{<:Integer, <:Integer}
79-
)
80+
)
8081
rule_ind = get_rule(node)
8182
if rule_ind > n_rules
8283
error("Rule index $(rule_ind) exceeds the number of grammar rules ($n_rules).")
@@ -88,14 +89,15 @@ function update_rule_indices!(
8889
for child in children
8990
update_rule_indices!(child, n_rules, mapping)
9091
end
92+
return
9193
end
9294

9395
# Check whether the `node`'s rule index exceeds the number of rules `n_rules.`
9496
function is_domain_valid(node::RuleNode, n_rules::Integer)
9597
if get_rule(node) > n_rules
9698
return false
9799
end
98-
all(child -> is_domain_valid(child, n_rules), get_children(node))
100+
return all(child -> is_domain_valid(child, n_rules), get_children(node))
99101
end
100102

101103
"""
@@ -133,7 +135,7 @@ function is_domain_valid(hole::AbstractHole, n_rules::Integer)
133135
if length(hole.domain) != n_rules
134136
return false
135137
end
136-
all(child -> is_domain_valid(child, n_rules), get_children(hole))
138+
return all(child -> is_domain_valid(child, n_rules), get_children(hole))
137139
end
138140

139141
"""
@@ -154,6 +156,7 @@ function update_rule_indices!(hole::AbstractHole, n_rules::Integer)
154156
for child in children
155157
update_rule_indices!(child, n_rules)
156158
end
159+
return
157160
end
158161

159162
"""
@@ -168,10 +171,11 @@ Errors if the length of the domain vector exceeds new `n_rules`.
168171
- `n_rules`: The new number of rules in the grammar
169172
- `mapping`: A dictionary mapping the old rule indices to new ones
170173
"""
171-
function update_rule_indices!(hole::AbstractHole,
174+
function update_rule_indices!(
175+
hole::AbstractHole,
172176
n_rules::Integer,
173177
mapping::AbstractDict{<:Integer, <:Integer}
174-
)
178+
)
175179
update_rule_indices!(hole, n_rules) # resize (also checks n_rules)
176180
# update domain BV according to mapping
177181
rule_indices = findall(hole.domain)
@@ -185,6 +189,7 @@ function update_rule_indices!(hole::AbstractHole,
185189
for child in children
186190
update_rule_indices!(child, n_rules, mapping)
187191
end
192+
return
188193
end
189194

190195
"""
@@ -221,16 +226,16 @@ function _get_hole_name(holetype)
221226
else
222227
throw(
223228
ArgumentError(
224-
styled"""
225-
Input to the {code:@rulenode} macro appears to be a hole, but the macro does not support the type: {code:{red:$holetype}}. The macro currently supports concrete subtypes of {code:AbstractHole}s {bold:that are defined in {code:HerbCore.jl}}.
226-
""",
227-
),
229+
styled"""
230+
Input to the {code:@rulenode} macro appears to be a hole, but the macro does not support the type: {code:{red:$holetype}}. The macro currently supports concrete subtypes of {code:AbstractHole}s {bold:that are defined in {code:HerbCore.jl}}.
231+
""",
232+
),
228233
)
229234
end
230235
end
231236

232237
function _shorthand2rulenode(i::Integer)
233-
:(RuleNode($i))
238+
return :(RuleNode($i))
234239
end
235240

236241
function _shorthand2rulenode(ex::Expr)
@@ -261,8 +266,10 @@ function _shorthand2rulenode(ex::Expr)
261266
# rulenodes without children
262267
ex = postwalk(ex) do x
263268
@capture(x, {children__}) || return x
264-
children = [isexpr(child, Int, Integer) ? :(RuleNode($child)) : child
265-
for child in children]
269+
children = [
270+
isexpr(child, Int, Integer) ? :(RuleNode($child)) : child
271+
for child in children
272+
]
266273
return :([$(children...)])
267274
end
268275

@@ -303,7 +310,7 @@ Hole[Bool[1, 1, 0, 0]]
303310
```
304311
"""
305312
macro rulenode(ex::Union{Integer, Expr})
306-
_shorthand2rulenode(ex)
313+
return _shorthand2rulenode(ex)
307314
end
308315

309316
"""
@@ -325,16 +332,16 @@ RuleNode(ind::Int, _val::Any) = RuleNode(ind, _val, AbstractRuleNode[])
325332
Base.:(==)(::RuleNode, ::AbstractHole) = false
326333
Base.:(==)(::AbstractHole, ::RuleNode) = false
327334
function Base.:(==)(A::RuleNode, B::RuleNode)
328-
(A.ind == B.ind) &&
335+
return (A.ind == B.ind) &&
329336
length(A.children) == length(B.children) && #required because zip doesn't check lengths
330337
all(isequal(a, b) for (a, b) in zip(A.children, B.children))
331338
end
332339
function Base.:(==)(a::UniformHole, b::UniformHole)
333-
a.domain == b.domain && length(a.children) == length(b.children) &&
340+
return a.domain == b.domain && length(a.children) == length(b.children) &&
334341
all(isequal(a, b) for (a, b) in zip(a.children, b.children))
335342
end
336-
function Base.:(==)(a::H, b::H) where H <: AbstractHole
337-
a.domain == b.domain
343+
function Base.:(==)(a::H, b::H) where {H <: AbstractHole}
344+
return a.domain == b.domain
338345
end
339346

340347

@@ -356,18 +363,18 @@ end
356363

357364
function Base.show(io::IO, node::RuleNode; separator = ",")
358365
print(io, node.ind)
359-
if !isempty(children(node))
366+
return if !isempty(children(node))
360367
Base.show_enclosed_list(io, "{", children(node), separator, "}", 0)
361368
end
362369
end
363370

364371
function Base.show(io::IO, node::AbstractHole; _...)
365-
print(io, "Hole[$(node.domain)]")
372+
return print(io, "Hole[$(node.domain)]")
366373
end
367374

368375
function Base.show(io::IO, node::UniformHole; separator = ",")
369376
print(io, "UniformHole[$(node.domain)]")
370-
if !isempty(node.children)
377+
return if !isempty(node.children)
371378
Base.show_enclosed_list(io, "{", children(node), separator, "}", 0)
372379
end
373380
end
@@ -396,8 +403,10 @@ performed in both [`RuleNode`](@ref)s until nodes with a different index
396403
are found.
397404
"""
398405
Base.isless(
399-
rn₁::AbstractRuleNode, rn₂::AbstractRuleNode)::Bool = _rulenode_compare(
400-
rn₁, rn₂) == -1
406+
rn₁::AbstractRuleNode, rn₂::AbstractRuleNode
407+
)::Bool = _rulenode_compare(
408+
rn₁, rn₂
409+
) == -1
401410

402411
function _rulenode_compare(rn₁::AbstractRuleNode, rn₂::AbstractRuleNode)::Int
403412
# Helper function for Base.isless
@@ -461,8 +470,10 @@ Returns every rule in the ruleset that is also used in the [`AbstractRuleNode`](
461470
!!! warning
462471
The `ignoreNode` must be a subtree of `node` for it to have an effect.
463472
"""
464-
function rulesoftype(node::RuleNode, ruleset::Set{Int},
465-
ignoreNode::Union{Nothing, AbstractRuleNode} = nothing)::Set{Int}
473+
function rulesoftype(
474+
node::RuleNode, ruleset::Set{Int},
475+
ignoreNode::Union{Nothing, AbstractRuleNode} = nothing
476+
)::Set{Int}
466477
retval = Set{Int}()
467478

468479
if !isnothing(ignoreNode) && node == ignoreNode
@@ -483,9 +494,11 @@ function rulesoftype(node::RuleNode, ruleset::Set{Int},
483494
return retval
484495
end
485496
end
486-
function rulesoftype(node::RuleNode, rule_index::Int,
487-
ignoreNode::Union{Nothing, AbstractRuleNode} = nothing)
488-
rulesoftype(node, Set{Int}(rule_index), ignoreNode)
497+
function rulesoftype(
498+
node::RuleNode, rule_index::Int,
499+
ignoreNode::Union{Nothing, AbstractRuleNode} = nothing
500+
)
501+
return rulesoftype(node, Set{Int}(rule_index), ignoreNode)
489502
end
490503

491504
rulesoftype(::Hole, ::Vararg{Any}) = Set{Int}()
@@ -503,9 +516,10 @@ function rulesoftype(
503516
grammar::AbstractGrammar,
504517
ruletype::Symbol,
505518
ignoreNode::Union{Nothing, RuleNode} = nothing
506-
)
507-
rulesoftype(
508-
node, Set(grammar[ruletype]), ignoreNode)
519+
)
520+
return rulesoftype(
521+
node, Set(grammar[ruletype]), ignoreNode
522+
)
509523
end
510524

511525
"""
@@ -514,8 +528,11 @@ end
514528
Returns true if the rulenode contains the index.
515529
"""
516530
function contains_index(rulenode::AbstractRuleNode, index::Int)
517-
!isempty(rulesoftype(
518-
rulenode, index))
531+
return !isempty(
532+
rulesoftype(
533+
rulenode, index
534+
)
535+
)
519536
end
520537

521538
"""
@@ -525,7 +542,7 @@ Replace a node in `expr`, specified by `path`, with `new_expr`.
525542
Path is a sequence of child indices, starting from the root node.
526543
"""
527544
function swap_node(expr::AbstractRuleNode, new_expr::AbstractRuleNode, path::Vector{Int})
528-
if length(path) == 1
545+
return if length(path) == 1
529546
expr.children[path[begin]] = new_expr
530547
else
531548
swap_node(expr.children[path[begin]], new_expr, path[2:end])
@@ -538,7 +555,7 @@ end
538555
Replace child `i` of a node, a part of larger `expr`, with `new_expr`.
539556
"""
540557
function swap_node(expr::RuleNode, node::RuleNode, child_index::Int, new_expr::RuleNode)
541-
if expr == node
558+
return if expr == node
542559
node.children[child_index] = new_expr
543560
else
544561
for child in expr.children
@@ -554,7 +571,7 @@ Extract the derivation sequence from a path (sequence of child indices) and an [
554571
If the path is deeper than the deepest node, it returns what it has.
555572
"""
556573
function get_rulesequence(node::RuleNode, path::Vector{Int})
557-
if node.ind == 0 # sign for empty node
574+
if node.ind == 0 # sign for empty node
558575
return Vector{Int}()
559576
elseif isempty(node.children) # no childnen, nowehere to follow the path; still return the index
560577
return [node.ind]
@@ -563,13 +580,17 @@ function get_rulesequence(node::RuleNode, path::Vector{Int})
563580
elseif isassigned(path, 2)
564581
# at least two items are left in the path
565582
# need to access the child with get because it can happen that the child is not yet built
566-
return append!([node.ind],
567-
get_rulesequence(get(node.children, path[begin], RuleNode(0)), path[2:end]))
583+
return append!(
584+
[node.ind],
585+
get_rulesequence(get(node.children, path[begin], RuleNode(0)), path[2:end])
586+
)
568587
else
569588
# if only one item left in the path
570589
# need to access the child with get because it can happen that the child is not yet built
571-
return append!([node.ind],
572-
get_rulesequence(get(node.children, path[begin], RuleNode(0)), Vector{Int}()))
590+
return append!(
591+
[node.ind],
592+
get_rulesequence(get(node.children, path[begin], RuleNode(0)), Vector{Int}())
593+
)
573594
end
574595
end
575596

@@ -642,7 +663,8 @@ end
642663
Returns the path from the `root` to the `targetnode`. Returns nothing if no path exists.
643664
"""
644665
function get_path(
645-
root::AbstractRuleNode, targetnode::AbstractRuleNode)::Union{Vector{Int}, Nothing}
666+
root::AbstractRuleNode, targetnode::AbstractRuleNode
667+
)::Union{Vector{Int}, Nothing}
646668
if root === targetnode
647669
return Vector{Int}()
648670
end
@@ -661,11 +683,12 @@ end
661683
Recursively counts the number of holes in an [`AbstractRuleNode`](@ref)
662684
"""
663685
function number_of_holes(rn::RuleNode)
664-
reduce(
665-
+, [number_of_holes(c) for c in rn.children], init = 0)
686+
return reduce(
687+
+, [number_of_holes(c) for c in rn.children], init = 0
688+
)
666689
end
667690
function number_of_holes(rn::UniformHole)
668-
1 + reduce(+, [number_of_holes(c) for c in rn.children], init = 0)
691+
return 1 + reduce(+, [number_of_holes(c) for c in rn.children], init = 0)
669692
end
670693
number_of_holes(rn::Hole) = 1
671694

@@ -683,8 +706,10 @@ contains_hole(hole::AbstractHole) = true
683706
Checks if an [`AbstractRuleNode`](@ref) tree contains a [`Hole`](@ref).
684707
"""
685708
function contains_nonuniform_hole(rn::AbstractRuleNode)
686-
any(contains_nonuniform_hole(c)
687-
for c in rn.children)
709+
return any(
710+
contains_nonuniform_hole(c)
711+
for c in rn.children
712+
)
688713
end
689714
contains_nonuniform_hole(hole::Hole) = true
690715

0 commit comments

Comments
 (0)