Class: DTRCore::Parser
- Inherits:
-
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
#capture_section, #clean_name, get_input_content, nested_array_to_string, paren_contents, parse_instruction_input, #split_strip_select
Constructor Details
#initialize(file_path, content: nil) ⇒ Parser
Returns a new instance of 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
#content ⇒ Object
Returns the value of attribute content.
20
21
22
|
# File 'lib/dtr_core/parser.rb', line 20
def content
@content
end
|
#sections ⇒ Object
Returns the value of attribute sections.
21
22
23
|
# File 'lib/dtr_core/parser.rb', line 21
def sections
@sections
end
|
Instance Method Details
#helpers_section ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/dtr_core/parser.rb', line 79
def helpers_section
return @helpers_section if @helpers_section
helpers_section = capture_section(/\[Helpers\]:(?<all>.*)\s*:\[Helpers\]/m)
return nil if helpers_section.nil?
function_definitions = helpers_section.split('-()').map do |x|
DTRCore::Function.from_definition(x.strip.to_s)
end
function_definitions.reject! { |x| x.name.nil? }
@helpers_section ||= function_definitions
end
|
#interface_section ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/dtr_core/parser.rb', line 47
def interface_section
return @function_definitions if @function_definitions
interface_section = capture_section(/\[Interface\]:(?<all>.*):\[Interface\]/m)
return nil if interface_section.nil?
function_definitions = interface_section.split('-()').map do |x|
DTRCore::Function.from_definition(x.strip.to_s)
end
function_definitions.reject! { |x| x.name.nil? }
@interface_section ||= function_definitions
end
|
#name_section ⇒ Object
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 = capture_section(/\[Contract\]:\s*(.+)/)
raise 'Missing contract name.' if name_section.nil?
@name_section ||= name_section.strip
end
|
#non_translatable_section ⇒ Object
95
96
97
98
99
100
101
102
103
|
# File 'lib/dtr_core/parser.rb', line 95
def non_translatable_section
return @non_translatable_section if @non_translatable_section
non_translatable_section = capture_section(/\[NonTranslatable\]:(?<all>.*)\s*:\[NonTranslatable\]/m)
return nil if non_translatable_section.nil?
@non_translatable_section = non_translatable_section
end
|
#state_section ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/dtr_core/parser.rb', line 33
def state_section
return @state_definitions if @state_definitions
state_section = capture_section(/\[State\]:\s*((?:\s*\*\s*\[.+?\]\n(?:\s*\*.+\n?)*)*)\s*:\[State\]/)
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) }
@state_section ||= state_definitions
end
|
#user_defined_types_section ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/dtr_core/parser.rb', line 63
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 = capture_section(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
|