module BELParser::Expression::Model::Converters

Public Instance Methods

ast_to_namespace(ast, namespace_hash = {}) click to toggle source
# File lib/bel_parser/expression/model/namespace.rb, line 148
def ast_to_namespace(ast, namespace_hash = {})
  return nil if ast.nil?

  case ast
  when BELParser::Parsers::AST::Prefix
    if ast.namespace
      dataset = ast.namespace
      case
      when dataset.uri?
        Namespace.new(dataset.keyword, dataset.identifier, nil)
      when dataset.url?
        Namespace.new(dataset.keyword, nil, dataset.identifier)
      else
        Namespace.new(dataset.keyword, nil, nil)
      end
    else
      return nil unless ast.identifier
      prefix_s = ast.identifier.string_literal
      namespace_hash[prefix_s]
    end
  when BELParser::Parsers::AST::NamespaceDefinition
    keyword, domain = ast.children
    keyword_s       = keyword.identifier.string_literal
    case
    when domain.uri?
      uri = domain.child.string_literal
      Namespace.new(keyword_s, uri, nil)
    when domain.url?
      url = domain.child.string_literal
      Namespace.new(keyword_s, ast.uri, url)
    end
  end
end
ast_to_parameter(ast, namespace_hash = {}) click to toggle source
# File lib/bel_parser/expression/model/parameter.rb, line 87
def ast_to_parameter(ast, namespace_hash = {})
  return nil if ast.nil? ||
    !ast.is_a?(BELParser::Parsers::AST::Parameter)
  namespace = ast_to_namespace(ast.prefix, namespace_hash)
  value     = ast.value.children[0].string_literal
  if namespace
    namespace[value]
  else
    BELParser::Expression::Model::Parameter.new(
      nil,   # nil namespace
      value,
      nil    # nil encoding
    )
  end
end
ast_to_statement(ast, spec, namespaces = {}) click to toggle source
# File lib/bel_parser/expression/model/statement.rb, line 158
def ast_to_statement(ast, spec, namespaces = {})
  statement =
    case ast
    when BELParser::Parsers::AST::Statement
      ast
    when ObservedTerm, SimpleStatement, NestedStatement
      ast.statement
    else
      nil
    end
  return nil if statement.nil?

  spec  ||= BELParser::Language.default_specification
  comment = statement.comment && statement.comment.children[0]

  Statement.new(
    ast_to_term(statement.subject.term, spec, namespaces),
    convert_relationship(statement.relationship, spec),
    convert_object(statement.object, spec, namespaces),
    comment)
end
ast_to_term(ast, spec, namespaces = {}) click to toggle source
# File lib/bel_parser/expression/model/term.rb, line 117
def ast_to_term(ast, spec, namespaces = {})
  return nil if ast.nil? ||
    !ast.is_a?(BELParser::Parsers::AST::Term)
  spec    ||= BELParser::Language.default_specification
  convert   = method(:convert_argument).to_proc.curry[spec][namespaces]

  Term.new(
    convert_function(ast.function, spec),
    ast.arguments.map(&convert))
end
convert_argument(spec, namespaces, argument) click to toggle source
# File lib/bel_parser/expression/model/term.rb, line 140
def convert_argument(spec, namespaces, argument)
  child = argument.child
  case child
  when BELParser::Parsers::AST::Parameter
    ast_to_parameter(child, namespaces)
  when BELParser::Parsers::AST::Term
    ast_to_term(child, spec, namespaces)
  else
    nil
  end
end
convert_function(ast, spec) click to toggle source
# File lib/bel_parser/expression/model/term.rb, line 128
def convert_function(ast, spec)
  func      = ast.identifier.string_literal
  spec_func = spec.function(func.to_sym)

  unless spec_func
    raise(
      ArgumentError,
      %(ast has invalid function "#{func}" for BEL #{spec.version}))
  end
  spec_func
end
convert_object(ast, spec, namespaces) click to toggle source
# File lib/bel_parser/expression/model/statement.rb, line 185
def convert_object(ast, spec, namespaces)
  case
  when ast.term?
    ast_to_term(ast.child, spec, namespaces)
  when ast.statement?
    ast_to_statement(ast.child, spec, namespaces)
  else
    nil
  end
end
convert_relationship(ast, spec) click to toggle source
# File lib/bel_parser/expression/model/statement.rb, line 180
def convert_relationship(ast, spec)
  relationship = ast.string_literal
  relationship && spec.relationship(relationship.to_sym)
end