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, content: nil) ⇒ Parser



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

def initialize(file_path, content: nil)
  if content
    @content = content
  else
    raise "Unable to find file: #{file_path}." unless File.exist?(file_path)

    @content = File.read(file_path)
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



20
21
22
# File 'lib/dtr_core/parser.rb', line 20

def content
  @content
end

#sectionsObject

Returns the value of attribute sections.



21
22
23
# File 'lib/dtr_core/parser.rb', line 21

def sections
  @sections
end

Instance Method Details

#function_sectionObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dtr_core/parser.rb', line 49

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



23
24
25
26
27
28
29
30
31
# File 'lib/dtr_core/parser.rb', line 23

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.strip
end

#state_sectionObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dtr_core/parser.rb', line 33

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

#user_defined_types_sectionObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dtr_core/parser.rb', line 67

def user_defined_types_section
  return @user_defined_types if @user_defined_types

  user_defined_types_regex = /\[User Defined Types\]:([\s\S]*?)\s*:\[User Defined Types\]/
  user_defined_types_section_parsed_out = first_match_for_content(user_defined_types_regex)

  return nil if user_defined_types_section_parsed_out.nil?

  user_defined_types = user_defined_types_section_parsed_out
                       .split(/\n\s*\*\s*\(/).map { |x| "(#{x.strip}" }
                       .filter { |x| x.length > 1 }
                       .map { |definition| DTRCore::UserDefinedType.from_definition(definition) }

  @user_defined_types_section ||= user_defined_types
end