Class: Elisp2any::File

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, synopsis:, commentary:, code:) ⇒ File

:nodoc:



24
25
26
27
28
29
# File 'lib/elisp2any/file.rb', line 24

def initialize(name:, synopsis:, commentary:, code:) # :nodoc:
  @name = name
  @synopsis = synopsis
  @commentary = commentary
  @code = code
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



6
7
8
# File 'lib/elisp2any/file.rb', line 6

def code
  @code
end

#commentaryObject (readonly)

Returns the value of attribute commentary.



6
7
8
# File 'lib/elisp2any/file.rb', line 6

def commentary
  @commentary
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/elisp2any/file.rb', line 6

def name
  @name
end

#synopsisObject (readonly)

Returns the value of attribute synopsis.



6
7
8
# File 'lib/elisp2any/file.rb', line 6

def synopsis
  @synopsis
end

Class Method Details

.parse(source) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/elisp2any/file.rb', line 8

def self.parse(source)
  source = source.respond_to?(:read) ? source.read : source
  ts_node = TreeSitterParser.parse(source)
  first_heading, second_heading, *nodes, last_heading = Node.from_tree_sitter(source, ts_node)
  name, synopsis = first_heading.name_and_synopsis
  second_heading.commentary? or raise Error, "no commentary heading: #{second_heading.inspect}"
  commentary = []
  until nodes.empty?
    (node = nodes.shift).code? and break
    commentary << node
  end
  code = nodes
  last_heading.final_name == name or raise Error, 'different names'
  new(name: name, synopsis: synopsis, commentary: commentary, code: code)
end