Sha256: 7ee2ecd64fd2bd0c076ded9968c7979cb6dc100c1005650a29ba592c51134756

Contents?: true

Size: 1.86 KB

Versions: 105

Compression:

Stored size: 1.86 KB

Contents

require          'concurrent/hash'

require_relative '../parsers/common'
require_relative '../parsers/expression'
require_relative '../parsers/bel_script'

require_relative '../ast_filter'
require_relative '../ast_generator'

require_relative 'state_function'
require_relative '../language/syntax_function'

module BELParser
  module Script
    # 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 Validator

      def initialize(state_aggregator)
        @state_aggregator = state_aggregator

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

      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

      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

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

Version data entries

105 entries across 105 versions & 1 rubygems

Version Path
bel_parser-1.1.6-java lib/bel_parser/script/validator.rb
bel_parser-1.1.6 lib/bel_parser/script/validator.rb
bel_parser-1.1.5 lib/bel_parser/script/validator.rb
bel_parser-1.1.4-java lib/bel_parser/script/validator.rb
bel_parser-1.1.4 lib/bel_parser/script/validator.rb
bel_parser-1.1.3-java lib/bel_parser/script/validator.rb
bel_parser-1.1.3 lib/bel_parser/script/validator.rb
bel_parser-1.1.2-java lib/bel_parser/script/validator.rb
bel_parser-1.1.2 lib/bel_parser/script/validator.rb
bel_parser-1.1.1-java lib/bel_parser/script/validator.rb
bel_parser-1.1.1 lib/bel_parser/script/validator.rb
bel_parser-1.0.8-java lib/bel_parser/script/validator.rb
bel_parser-1.0.8 lib/bel_parser/script/validator.rb
bel_parser-1.0.7-java lib/bel_parser/script/validator.rb
bel_parser-1.0.7 lib/bel_parser/script/validator.rb
bel_parser-1.0.6-java lib/bel_parser/script/validator.rb
bel_parser-1.0.6 lib/bel_parser/script/validator.rb
bel_parser-1.0.5-java lib/bel_parser/script/validator.rb
bel_parser-1.0.5 lib/bel_parser/script/validator.rb
bel_parser-1.0.4-java lib/bel_parser/script/validator.rb