Class: DTRCore::Parser
- Inherits:
-
Object
- Object
- DTRCore::Parser
- Includes:
- Common
- Defined in:
- lib/dtr_core/parser.rb
Overview
Parses a DTR file and returns a Contract object.
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#sections ⇒ Object
Returns the value of attribute sections.
Instance Method Summary collapse
- #function_section ⇒ Object
-
#initialize(file_path, content: nil) ⇒ Parser
constructor
A new instance of Parser.
- #name_section ⇒ Object
- #state_section ⇒ Object
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
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 (readonly)
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
#function_section ⇒ Object
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_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 = first_match_for_content(/\[Contract\]:\s*(.+)/) raise 'Missing contract name.' if name_section.nil? @name_section ||= name_section.strip end |
#state_section ⇒ Object
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 |