module BELParser::Language::Semantics

Semantics module aggregates the generic {SemanticsFunction} implementations that apply to all {BELParser::Language::Specification BEL specifications}.

Semantics capture BEL version-independent semantics for terms and statements.

Public Class Methods

match(input_ast, semantic_ast, spec, will_match_partial = false, match_results = []) click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/bel_parser/language/semantics_ast.rb, line 13
def self.match(input_ast, semantic_ast, spec, will_match_partial = false, match_results = [])
  res = semantic_ast.match(input_ast, spec, will_match_partial)
  match_results.concat(res)
  if res.flatten.all?(&:success?) && !semantic_ast.terminal?
    return match_results if semantic_ast.children.empty?

    var_test = semantic_ast.children.any? do |x|
      x.is_a?(SemanticVariadicArguments)
    end
    if var_test
      test_pairs =
        input_ast
        .children
        .zip(semantic_ast.children)
        .select do |pair|
          !pair.include?(nil)
        end

      test_pairs.each do |(input_child, semantic_child)|
        if semantic_child.is_a?(SemanticVariadicArguments)
          input_children   = input_ast.children
          input_arguments  =
            input_children[input_children.index(input_child)..-1]
          argument_pattern = semantic_child.children.first
          input_arguments.each do |argument_child|
            res = semantic_child.match(argument_child, spec, will_match_partial)
            match_results << res
            if res.all?(&:success?)
              param_or_term = argument_child.children.first
              match(param_or_term, argument_pattern, spec, will_match_partial, match_results)
            end
          end
        else
          match(input_child, semantic_child, spec, will_match_partial, match_results)
        end
      end
    else
      if input_ast
        semantic_ast
          .children
          .zip(input_ast.children)
          .each do |semantic_child, input_child|
            match(input_child, semantic_child, spec, will_match_partial, match_results)
          end
      end
    end
  end
  match_results.flatten
end
semantics_functions() click to toggle source
# File lib/bel_parser/language/semantics.rb, line 13
def self.semantics_functions
  constants.collect do |symbol|
    const = const_get(symbol)
    const if
      const.respond_to?(:include?) &&
      const.include?(SemanticsFunction)
  end.compact
end