File: boundaries.rb

package info (click to toggle)
ruby-mustermann19 0.4.3%2Bgit20160621-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 756 kB
  • ctags: 445
  • sloc: ruby: 7,197; makefile: 3
file content (48 lines) | stat: -rw-r--r-- 1,588 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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)
      def self.set_boundaries(ast, options = {})
        string = options.fetch(:string, nil)
        start  = options.fetch(:start, 0)
        stop   = options.fetch(: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