Class: DTRCore::Parser

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/dtr_core/parser.rb

Overview

Parses a DTR file and returns a Contract object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#clean_name, #first_match_for_content, #split_strip_select, #strip_and_remove_quotes

Constructor Details

#initialize(file_path) ⇒ Parser

Returns a new instance of Parser.



10
11
12
13
14
# File 'lib/dtr_core/parser.rb', line 10

def initialize(file_path)
  raise "Unable to find file: #{file_path}." unless File.exist?(file_path)

  @content = File.read(file_path)
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



16
17
18
# File 'lib/dtr_core/parser.rb', line 16

def content
  @content
end

#sectionsObject

Returns the value of attribute sections.



17
18
19
# File 'lib/dtr_core/parser.rb', line 17

def sections
  @sections
end

Instance Method Details

#function_sectionObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dtr_core/parser.rb', line 45

def function_section
  return @function_definitions if @function_definitions

  function_section = first_match_for_content(/\[Functions\]:(?<all>.*):\[Functions\]/m)

  return nil if function_section.nil?

  function_definitions = function_section.split('-()').map do |x|
    DTRCore::Function.from_definition(x.strip.to_s)
  end

  function_definitions.reject! { |x| x.name.nil? }

  raise 'Empty function section.' if function_definitions.empty?

  @function_section ||= function_definitions
end

#name_sectionObject



19
20
21
22
23
24
25
26
27
# File 'lib/dtr_core/parser.rb', line 19

def name_section
  return @name_section if @name_section

  name_section = first_match_for_content(/\[Contract\]:\s*(.+)/)

  raise 'Missing contract name.' if name_section.nil?

  @name_section ||= name_section
end

#state_sectionObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dtr_core/parser.rb', line 29

def state_section
  return @state_definitions if @state_definitions

  state_section = first_match_for_content(/\[State\]:\s*((?:\s*\*\s*\[.+?\]\n(?:\s*  \* .+\n?)*)*)/)

  return nil if state_section.nil?

  state_definitions = state_section
                      .split(/\n\s*\*\s*\[/).map { |x| "[#{x.strip}" }
                      .map { |definition| DTRCore::State.from_definition(definition) }

  raise 'Empty state section.' if state_definitions.empty?

  @state_section ||= state_definitions
end