Class: Elisp2any::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/elisp2any/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, range) ⇒ Node

:nodoc:



61
62
63
64
# File 'lib/elisp2any/node.rb', line 61

def initialize(content, range) # :nodoc:
  @content = content
  @range = range
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



10
11
12
# File 'lib/elisp2any/node.rb', line 10

def content
  @content
end

#rangeObject (readonly)

:nodoc:



9
10
11
# File 'lib/elisp2any/node.rb', line 9

def range
  @range
end

Class Method Details

.from_tree_sitter(source, ts_node) ⇒ Object

:nodoc:



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
49
50
51
52
53
54
# File 'lib/elisp2any/node.rb', line 12

def self.from_tree_sitter(source, ts_node) #:nodoc:
  nodes = []
  (0 ... ts_node.child_count).map { |index| ts_node[index] }.each do |top_level_node|
    range = top_level_node.start_byte .. top_level_node.end_byte
    content = source.byteslice(range)
    node = Node.new(content, range)
    case top_level_node.type
    when :comment
      scanner = StringScanner.new(content)
      scanner.skip(';') or raise Error, 'no semicolon for comment'
      scanner.skip(';') or
        begin
          (last_node = nodes.last) && last_node.is_a?(CodeBlock) or raise Error, 'no prior code for single semicolon comment'
          last_node.append(source, range.end)
          next
        end

      if (level = scanner.skip(/;+/))
        scanner.skip(' ') or raise Error, 'no space after heading semicolons'
        nodes << Heading.new(node, level, scanner.rest.chomp)
        next
      elsif scanner.skip("\n")
        next
      end

      scanner.skip(' ') or raise Error, "no space after semicolons: #{scanner.inspect}"
      line = Line.parse(scanner.rest)
      if (last_node = nodes.last) && last_node.is_a?(Paragraph) && last_node.end_row + 1 == top_level_node.start_point.row
        last_node << line
      else
        paragraph = Paragraph.new(node, [line], top_level_node.end_point.row)
        nodes << paragraph
      end
    else
      if (last_node = nodes.last) && last_node.is_a?(CodeBlock)
        last_node.append(source, range.end)
      else
        nodes << CodeBlock.new(node)
      end
    end
  end
  nodes
end

Instance Method Details

#append(source, end_byte) ⇒ Object

:nodoc:



56
57
58
59
# File 'lib/elisp2any/node.rb', line 56

def append(source, end_byte) # :nodoc:
  @range = range = @range.begin .. end_byte
  @content = source.byteslice(range)
end