Class: CSVPlusPlus::Language::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_plus_plus/language/runtime.rb

Overview

The runtime state of the compiler (the current linenumber/row, cell, etc)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input:, filename:) ⇒ Runtime

initialize



24
25
26
27
28
29
# File 'lib/csv_plus_plus/language/runtime.rb', line 24

def initialize(input:, filename:)
  @filename = filename || 'stdin'

  init_input!(input)
  init!(1)
end

Instance Attribute Details

#cellObject

Returns the value of attribute cell.



21
22
23
# File 'lib/csv_plus_plus/language/runtime.rb', line 21

def cell
  @cell
end

#cell_indexObject

Returns the value of attribute cell_index.



21
22
23
# File 'lib/csv_plus_plus/language/runtime.rb', line 21

def cell_index
  @cell_index
end

#filenameObject (readonly)

Returns the value of attribute filename.



19
20
21
# File 'lib/csv_plus_plus/language/runtime.rb', line 19

def filename
  @filename
end

#length_of_code_sectionObject (readonly)

Returns the value of attribute length_of_code_section.



19
20
21
# File 'lib/csv_plus_plus/language/runtime.rb', line 19

def length_of_code_section
  @length_of_code_section
end

#length_of_csv_sectionObject (readonly)

Returns the value of attribute length_of_csv_section.



19
20
21
# File 'lib/csv_plus_plus/language/runtime.rb', line 19

def length_of_csv_section
  @length_of_csv_section
end

#length_of_original_fileObject (readonly)

Returns the value of attribute length_of_original_file.



19
20
21
# File 'lib/csv_plus_plus/language/runtime.rb', line 19

def length_of_original_file
  @length_of_original_file
end

#line_numberObject

Returns the value of attribute line_number.



21
22
23
# File 'lib/csv_plus_plus/language/runtime.rb', line 21

def line_number
  @line_number
end

#row_indexObject

Returns the value of attribute row_index.



21
22
23
# File 'lib/csv_plus_plus/language/runtime.rb', line 21

def row_index
  @row_index
end

Instance Method Details

#cleanup!Object

Clean up the Tempfile we’re using for parsing



119
120
121
122
123
124
125
# File 'lib/csv_plus_plus/language/runtime.rb', line 119

def cleanup!
  return unless @tmp

  @tmp.close
  @tmp.unlink
  @tmp = nil
end

#init!(start_line_number_at) ⇒ Object

Each time we run a parse on the input, call this so that the runtime state is set to it’s default values



75
76
77
78
# File 'lib/csv_plus_plus/language/runtime.rb', line 75

def init!(start_line_number_at)
  @row_index = @cell_index = nil
  @line_number = start_line_number_at
end

#inputObject

The currently available input for parsing. The tmp state will be re-written between parsing the code section and the CSV section



107
108
109
# File 'lib/csv_plus_plus/language/runtime.rb', line 107

def input
  @tmp
end

#map_lines(lines, &block) ⇒ Object

map over an unparsed file and keep track of line_number and row_index



32
33
34
35
36
37
# File 'lib/csv_plus_plus/language/runtime.rb', line 32

def map_lines(lines, &block)
  @line_number = 1
  lines.map do |line|
    block.call(line).tap { next_line! }
  end
end

#map_row(row, &block) ⇒ Object

map over a single row and keep track of the cell and it’s index



40
41
42
43
44
45
46
# File 'lib/csv_plus_plus/language/runtime.rb', line 40

def map_row(row, &block)
  @cell_index = 0
  row.map.with_index do |cell, index|
    set_cell!(cell, index)
    block.call(cell, index)
  end
end

#map_rows(rows, cells_too: false, &block) ⇒ Object

map over all rows and keep track of row and line numbers



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/csv_plus_plus/language/runtime.rb', line 49

def map_rows(rows, cells_too: false, &block)
  @row_index = 0
  map_lines(rows) do |row|
    if cells_too
      # it's either CSV or a Row object
      map_row((row.is_a?(::CSVPlusPlus::Row) ? row.cells : row), &block)
    else
      block.call(row)
    end
  end
end

#next_line!Object

Increment state to the next line



62
63
64
65
# File 'lib/csv_plus_plus/language/runtime.rb', line 62

def next_line!
  @row_index += 1 unless @row_index.nil?
  @line_number += 1
end

#raise_syntax_error(message, bad_input, wrapped_error: nil) ⇒ Object

Called when an error is encoutered during parsing. It will construct a useful error with the current @row/@cell_index, @line_number and @filename



101
102
103
# File 'lib/csv_plus_plus/language/runtime.rb', line 101

def raise_syntax_error(message, bad_input, wrapped_error: nil)
  raise(::CSVPlusPlus::Language::SyntaxError.new(message, bad_input, self, wrapped_error:))
end

#rewrite_input!(data) ⇒ Object

We mutate the input over and over. It’s ok because it’s just a Tempfile



112
113
114
115
116
# File 'lib/csv_plus_plus/language/runtime.rb', line 112

def rewrite_input!(data)
  @tmp.truncate(0)
  @tmp.write(data)
  @tmp.rewind
end

#runtime_value(var_id) ⇒ Object

get the current (entity) value of a runtime value



86
87
88
89
90
91
92
# File 'lib/csv_plus_plus/language/runtime.rb', line 86

def runtime_value(var_id)
  if runtime_variable?(var_id)
    ::RUNTIME_VARIABLES[var_id.to_sym].resolve_fn.call(self)
  else
    raise_syntax_error('Undefined variable', var_id)
  end
end

#runtime_variable?(var_id) ⇒ Boolean

Is var_id a runtime variable? (it’s a static variable otherwise)

Returns:

  • (Boolean)


95
96
97
# File 'lib/csv_plus_plus/language/runtime.rb', line 95

def runtime_variable?(var_id)
  ::RUNTIME_VARIABLES.key?(var_id.to_sym)
end

#set_cell!(cell, cell_index) ⇒ Object

Set the current cell and index



68
69
70
71
# File 'lib/csv_plus_plus/language/runtime.rb', line 68

def set_cell!(cell, cell_index)
  @cell = cell
  @cell_index = cell_index
end

#to_sObject

to_s



81
82
83
# File 'lib/csv_plus_plus/language/runtime.rb', line 81

def to_s
  "Runtime(cell: #{@cell}, row_index: #{@row_index}, cell_index: #{@cell_index})"
end