class BELParser::Language::ExpressionValidator

ExpressionValidator validates the syntax and semantics of BEL expressions when supplied a {BELParser::Language::Specification} and Hash of namespaces.

Public Class Methods

new(spec, namespaces, uri_reader, url_reader, will_match_partial = false) click to toggle source
# File lib/bel_parser/language/expression_validator.rb, line 12
def initialize(spec, namespaces, uri_reader, url_reader, will_match_partial = false)
  @spec                         = spec
  @namespaces                   = namespaces || {}

  # double negate truthy or falsey value to strict boolean
  @will_match_partial           = !!will_match_partial

  @syntax_functions             = Syntax.syntax_functions
  @semantics_functions          = Semantics.semantics_functions
  @default_namespace_transform  =
    ApplyDefaultNamespace.new(@spec, @namespaces, uri_reader, url_reader)
  @namespace_encoding_transform =
    ApplyNamespaceEncoding.new(@spec, @namespaces, uri_reader, url_reader)

end

Public Instance Methods

validate(expression_node) click to toggle source

Validate the syntax and semantics of {BELParser::Parsers::AST::Node expression_node}.

@param [BELParser::Parsers::AST::Node] expression_node to validate @return [BELParser::Language::Syntax::SyntaxResult] syntax results

# File lib/bel_parser/language/expression_validator.rb, line 33
def validate(expression_node)
  if @spec.version >= "2.0"
    expression_node = @default_namespace_transform.process(expression_node)
  end

  @namespace_encoding_transform.process(expression_node)

  case expression_node
  when BELParser::Parsers::AST::SimpleStatement
    SimpleStatementResult.new(
      expression_node,
      validate(expression_node.statement.subject.term),
      validate(expression_node.statement.object.child),
      syntax(expression_node),
      semantics(expression_node))
  when BELParser::Parsers::AST::ObservedTerm
    ObservedTermResult.new(
      expression_node,
      validate(expression_node.statement.subject.term),
      syntax(expression_node),
      semantics(expression_node))
  when BELParser::Parsers::AST::NestedStatement
    NestedStatementResult.new(
      expression_node,
      validate(expression_node.statement.subject.term),
      validate(expression_node.statement.object.child),
      syntax(expression_node),
      semantics(expression_node))
  when BELParser::Parsers::AST::Statement
    SimpleStatementResult.new(
      expression_node,
      validate(expression_node.subject.term),
      validate(expression_node.object.child),
      syntax(expression_node),
      semantics(expression_node))
  when BELParser::Parsers::AST::Term
    TermResult.new(
      expression_node,
      syntax(expression_node),
      semantics(expression_node))
  when BELParser::Parsers::AST::Parameter
    ParameterResult.new(
      expression_node,
      syntax(expression_node),
      semantics(expression_node))
  else
    nil
  end
end

Private Instance Methods

semantics(expression_node) click to toggle source
# File lib/bel_parser/language/expression_validator.rb, line 91
def semantics(expression_node)
  expression_node.traverse.flat_map do |node|
    @semantics_functions.flat_map { |func|
      func.map(node, @spec, @namespaces, @will_match_partial)
    }
  end.compact
end
syntax(expression_node) click to toggle source
# File lib/bel_parser/language/expression_validator.rb, line 85
def syntax(expression_node)
  expression_node.traverse.flat_map do |node|
    @syntax_functions.map { |func| func.map(node, @spec, @namespaces) }
  end.compact
end