Sha256: d58570b995550f3ddd94b2c300e1537f4bb490a4e9c837cbaf373867b0b156e1

Contents?: true

Size: 1.39 KB

Versions: 27

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true
require 'mustermann/ast/translator'

module Mustermann
  module AST
    # Make sure #start and #stop is set on every node and within its parents #start and #stop.
    # @!visibility private
    class Boundaries < Translator
      # @return [Mustermann::AST::Node] the ast passed as first argument
      # @!visibility private
      def self.set_boundaries(ast, string: nil, start: 0, stop: string.length)
        new.translate(ast, start, stop)
        ast
      end

      translate(:node) do |start, stop|
        t.set_boundaries(node, start, stop)
        t(payload, node.start, node.stop)
      end

      translate(:with_look_ahead) do |start, stop|
        t.set_boundaries(node, start, stop)
        t(head,    node.start, node.stop)
        t(payload, node.start, node.stop)
      end

      translate(Array) do |start, stop|
        each do |subnode|
          t(subnode, start, stop)
          start = subnode.stop
        end
      end

      translate(Object) { |*| node }

      # Checks that a node is within the given boundaries.
      # @!visibility private
      def set_boundaries(node, start, stop)
        node.start = start                      if node.start.nil? or node.start < start
        node.stop  = node.start + node.min_size if node.stop.nil?  or node.stop  < node.start
        node.stop  = stop                       if node.stop > stop
      end
    end
  end
end

Version data entries

27 entries across 24 versions & 5 rubygems

Version Path
mustermann-3.0.3 lib/mustermann/ast/boundaries.rb
mustermann-3.0.2 lib/mustermann/ast/boundaries.rb
mustermann-3.0.1 lib/mustermann/ast/boundaries.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/ast/boundaries.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/ast/boundaries.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/ast/boundaries.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/ast/boundaries.rb
mustermann-3.0.0 lib/mustermann/ast/boundaries.rb
mustermann-2.0.2 lib/mustermann/ast/boundaries.rb
mustermann-2.0.1 lib/mustermann/ast/boundaries.rb
mustermann-2.0.0 lib/mustermann/ast/boundaries.rb
mustermann-1.1.2 lib/mustermann/ast/boundaries.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.0.0/gems/mustermann-1.1.1/lib/mustermann/ast/boundaries.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.1.0/gems/mustermann-1.1.1/lib/mustermann/ast/boundaries.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/2.6.0/gems/mustermann-1.1.1/lib/mustermann/ast/boundaries.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/mustermann-1.0.3/lib/mustermann/ast/boundaries.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/mustermann-1.1.1/lib/mustermann/ast/boundaries.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/mustermann-1.0.3/lib/mustermann/ast/boundaries.rb
mustermann-1.1.1 lib/mustermann/ast/boundaries.rb
mustermann-1.1.0 lib/mustermann/ast/boundaries.rb