Skip to content

Commit

Permalink
Allow for module specification in isvariable(...)
Browse files Browse the repository at this point in the history
Allow the specification one or more `Module` when checking if a
rule used by a `RuleNode` represents a variable, or if it is
defined in one of the specified `Module`s, `Main`, or `Base`.

Fixes #21.
  • Loading branch information
ReubenJ committed Jan 18, 2024
1 parent ce84c2a commit bcfec29
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/rulenode_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,48 @@ nchildren(grammar::Grammar, node::RuleNode)::Int = length(child_types(grammar, n
"""
isvariable(grammar::Grammar, node::RuleNode)::Bool
Returns true if the rule used by `node` represents a variable.
Return true if the rule used by `node` represents a variable.
"""
isvariable(grammar::Grammar, node::RuleNode)::Bool = grammar.isterminal[node.ind] && grammar.rules[node.ind] isa Symbol
isvariable(grammar::Grammar, node::RuleNode)::Bool = (
grammar.isterminal[node.ind] &&
grammar.rules[node.ind] isa Symbol &&
!_is_defined_in_modules(grammar.rules[node.ind], [Main, Base])
)
"""
isvariable(grammar::Grammar, node::RuleNode, mod::Module)::Bool
Return true if the rule used by `node` represents a variable.
Taking into account the symbols defined in the given module(s).
"""
isvariable(grammar::Grammar, node::RuleNode, mod::Module...)::Bool = (
grammar.isterminal[node.ind] &&
grammar.rules[node.ind] isa Symbol &&
!_is_defined_in_modules(grammar.rules[node.ind], [mod..., Main, Base])
)

"""
isvariable(grammar::Grammar, ind::Int)::Bool
isvariable(grammar::Grammar, ind::Int)::Bool = grammar.isterminal[ind] && grammar.rules[ind] isa Symbol
Return true if the rule with index `ind` represents a variable.
"""
isvariable(grammar::Grammar, ind::Int)::Bool = (
grammar.isterminal[ind] &&
grammar.rules[ind] isa Symbol &&
!_is_defined_in_modules(grammar.rules[ind], [Main, Base])
)
"""
isvariable(grammar::Grammar, ind::Int, mod::Module)::Bool
Return true if the rule with index `ind` represents a variable.
Taking into account the symbols defined in the given module(s).
"""
isvariable(grammar::Grammar, ind::Int, mod::Module...)::Bool = (
grammar.isterminal[ind] &&
grammar.rules[ind] isa Symbol &&
!_is_defined_in_modules(grammar.rules[ind], [mod..., Main, Base])
)

"""
contains_returntype(node::RuleNode, grammar::Grammar, sym::Symbol, maxdepth::Int=typemax(Int))
Expand Down

0 comments on commit bcfec29

Please sign in to comment.