Class: BELParser::Script::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/bel_parser/script/validator.rb

Overview

Validator defines a BEL Script syntax validator. This validator expects to receive the BEL Script state for each node. This is accomplished by initializing with a StateAggregator that this will enumerate.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_aggregator) ⇒ Validator

Returns a new instance of Validator.



21
22
23
24
25
26
# File 'lib/bel_parser/script/validator.rb', line 21

def initialize(state_aggregator)
  @state_aggregator = state_aggregator

  Validator.require_script_path
  @syntax_functions = Validator.syntax_constants(Syntax)
end

Class Method Details

.require_script_pathObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/bel_parser/script/validator.rb', line 46

def self.require_script_path
  base_path = File.expand_path(File.dirname(__FILE__)) + File::SEPARATOR
  ['state', 'syntax'].each do |set|
    Dir[File.join(base_path, set, '*.rb')]
      .each do |ruby_file|
        ruby_file.sub!(/^#{Regexp.escape(base_path)}/, '')
        require_relative ruby_file
      end
  end
end

.syntax_constants(mod) ⇒ Object



57
58
59
60
61
62
# File 'lib/bel_parser/script/validator.rb', line 57

def self.syntax_constants(mod)
  mod.constants.collect do |symbol|
    const = mod.const_get(symbol)
    const if const.respond_to?(:map)
  end.compact
end

Instance Method Details

#eachObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bel_parser/script/validator.rb', line 28

def each
  if block_given?
    @state_aggregator.each do |(line_number, line, ast_node, state)|
      ast_node.traverse.flat_map do |node|
        @syntax_functions.flat_map do |func|
          func.map(node, state)
        end
      end.compact.each do |syntax_result|
        ast_node.add_syntax_error(syntax_result)
      end

      yield [line_number, line, ast_node, state]
    end
  else
    enum_for(:each)
  end
end