class BELParser::Expression::Parser

Parser for BEL expressions that return common objects.

Public Class Methods

new( input, filter = Expression.filter, spec = BELParser::Language.default_specification, namespaces = {}) click to toggle source
# File lib/bel_parser/expression/parser.rb, line 53
def initialize(
  input,
  filter     = Expression.filter,
  spec       = BELParser::Language.default_specification,
  namespaces = {})

  @input      = input
  @filter     = filter
  @spec       = spec
  @namespaces = namespaces
end

Public Instance Methods

parse() click to toggle source
# File lib/bel_parser/expression/parser.rb, line 65
def parse
  case @input
  when ::String # conflicts with ...AST::String
    results = parse_string(@input, @filter)
    return nil if results.nil?
    convert_ast(
      @spec,
      @namespaces,
      results.first)
  when Array
    convert_multiple(
      parse_array(@input, @filter),
      @spec,
      @namespaces)
  when IO, StringIO
    convert_stream(
      parse_io(@input, @filter),
      @spec,
      @namespaces)
  else
    raise ArgumentError,
      %(expected "input" to be one of String, Array, IO: #{@input.class})
  end
end

Private Instance Methods

convert_ast(spec, namespaces, ast) click to toggle source
# File lib/bel_parser/expression/parser.rb, line 92
def convert_ast(spec, namespaces, ast)
  case ast
  when Parameter
    ast_to_parameter(ast, namespaces)
  when Term
    ast_to_term(ast, spec, namespaces)
  when Statement, ObservedTerm, SimpleStatement, NestedStatement
    ast_to_statement(ast, spec, namespaces)
  else
    nil
  end
end
convert_multiple(asts, spec, namespaces) click to toggle source
# File lib/bel_parser/expression/parser.rb, line 105
def convert_multiple(asts, spec, namespaces)
  mult = method(:convert_ast).to_proc.curry[spec][namespaces]
  asts.lazy.map do |ast|
    if ast.respond_to?(:each)
      ast.map(&mult)
    else
      convert_ast(spec, namespaces, ast)
    end
  end
end
convert_stream(asts, spec, namespaces) click to toggle source
# File lib/bel_parser/expression/parser.rb, line 116
def convert_stream(asts, spec, namespaces)
  mult = method(:convert_ast).to_proc.curry[spec][namespaces]
  asts.lazy.flat_map do |ast|
    if ast.respond_to?(:each)
      ast.map(&mult)
    else
      convert_ast(spec, namespaces, ast)
    end
  end
end
parse_array(array, filter) { |parse_string(expression, filter)| ... } click to toggle source
# File lib/bel_parser/expression/parser.rb, line 139
def parse_array(array, filter)
  if block_given?
    array.each do |expression|
      yield parse_string(expression.to_s, filter)
    end
    nil
  else
    array.map do |expression|
      parse_string(expression.to_s, filter)
    end
  end
end
parse_io(io, filter) { |results| ... } click to toggle source
# File lib/bel_parser/expression/parser.rb, line 152
def parse_io(io, filter)
  if block_given?
    enum = filter.each(BELParser::ASTGenerator.new(io))
    enum.each do |(num, line, results)|
      yield results
    end
    nil
  else
    enum_for(:parse_io, io, filter)
  end
end
parse_string(string, filter) { |results| ... } click to toggle source
# File lib/bel_parser/expression/parser.rb, line 127
def parse_string(string, filter)
  enum = filter.each(
    BELParser::ASTGenerator.new(StringIO.new(string)))
  num, line, results = enum.first
  if block_given?
    yield results
    nil
  else
    results
  end
end