Class: CSVPlusPlus::Runtime::Runtime

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
CanDefineReferences, CanResolveReferences, PositionTracker
Defined in:
lib/csv_plus_plus/runtime/runtime.rb

Overview

The runtime state of the compiler (the current line_number/row_index, cell being processed, etc) for parsing a given file. We take multiple runs through the input file for parsing so it’s really convenient to have a central place for these things to be managed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PositionTracker

#cleanup!, #input, #map_all_cells, #map_lines, #map_row, #map_rows, #rewrite_input!, #rownum, #start!

Methods included from CanResolveReferences

#bind_variable_in_expand, #bind_variable_to_cell, #in_scope?, #resolve_cell_value

Methods included from CanDefineReferences

#def_function, #def_variable, #def_variables, #defined_function?, #defined_variable?, #verbose_summary

Constructor Details

#initialize(source_code:, functions: {}, variables: {}) ⇒ Runtime

Returns a new instance of Runtime.

Parameters:

  • source_code (SourceCode)

    The source code being compiled

  • functions (Hash<Symbol, Function>) (defaults to: {})

    Pre-defined functions

  • variables (Hash<Symbol, Entity>) (defaults to: {})

    Pre-defined variables



43
44
45
46
47
48
49
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 43

def initialize(source_code:, functions: {}, variables: {})
  @functions = functions
  @variables = variables
  @source_code = source_code

  rewrite_input!(source_code.input)
end

Instance Attribute Details

#cellCell

The current cell being processed

Returns:

  • (Cell)

    the current value of cell



17
18
19
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 17

def cell
  @cell
end

#cell_indexInteger

The index of the current cell being processed (starts at 0)

Returns:

  • (Integer)

    the current value of cell_index



17
18
19
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 17

def cell_index
  @cell_index
end

#filenameString? (readonly)

The filename that the input came from (mostly used for debugging since filename can be nil if it’s read from stdin.

Returns:

  • (String, nil)

    the current value of filename



17
18
19
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 17

def filename
  @filename
end

#functionsObject (readonly)

Returns the value of attribute functions.



25
26
27
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 25

def functions
  @functions
end

#line_numberInteger

The line number of the original csvpp template (starts at 1)

Returns:

  • (Integer)

    the current value of line_number



17
18
19
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 17

def line_number
  @line_number
end

#row_indexInteger

The index of the current row being processed (starts at 0)

Returns:

  • (Integer)

    the current value of row_index



17
18
19
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 17

def row_index
  @row_index
end

#source_codeObject (readonly)

Returns the value of attribute source_code.



31
32
33
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 31

def source_code
  @source_code
end

#variablesObject (readonly)

Returns the value of attribute variables.



28
29
30
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 28

def variables
  @variables
end

Instance Method Details

#builtin_function?(fn_id) ⇒ T::Boolean

Is fn_id a builtin function?

Parameters:

  • fn_id (Symbol)

    The Function#id to check if it’s a runtime variable

Returns:

  • (T::Boolean)


57
58
59
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 57

def builtin_function?(fn_id)
  ::CSVPlusPlus::Entities::Builtins::FUNCTIONS.key?(fn_id)
end

#builtin_variable?(var_id) ⇒ T::Boolean

Is var_id a builtin variable?

Parameters:

  • var_id (Symbol)

    The Variable#id to check if it’s a runtime variable

Returns:

  • (T::Boolean)


67
68
69
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 67

def builtin_variable?(var_id)
  ::CSVPlusPlus::Entities::Builtins::VARIABLES.key?(var_id)
end

#parsing_code_section?T::Boolean

Is the parser currently inside of the code section? (includes the ---)

Returns:

  • (T::Boolean)


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

def parsing_code_section?
  source_code.in_code_section?(line_number)
end

#parsing_csv_section?T::Boolean

Is the parser currently inside of the CSV section?

Returns:

  • (T::Boolean)


83
84
85
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 83

def parsing_csv_section?
  source_code.in_csv_section?(line_number)
end

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

Called when an error is encoutered during parsing formulas (whether in the code section or a cell). It will construct a useful error with the current @row/@cell_index, @line_number and @filename

Parameters:

  • message (::String)

    A message relevant to why this error is being raised.

  • bad_input (::String)

    The offending input that caused this error to be thrown.

  • wrapped_error (StandardError, nil) (defaults to: nil)

    The underlying error that was raised (if it’s not from our own logic)

Raises:



97
98
99
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 97

def raise_formula_syntax_error(message, bad_input, wrapped_error: nil)
  raise(::CSVPlusPlus::Error::FormulaSyntaxError.new(message, bad_input, self, wrapped_error:))
end

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

Called when an error is encountered while parsing a modifier.

Parameters:

  • message (::String)

    A message relevant to why this error is being raised.

  • bad_input (::String)

    The offending input that caused this error to be thrown.

  • wrapped_error (StandardError, nil) (defaults to: nil)

    The underlying error that was raised (if it’s not from our own logic)

Raises:



110
111
112
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 110

def raise_modifier_syntax_error(message, bad_input, wrapped_error: nil)
  raise(::CSVPlusPlus::Error::ModifierSyntaxError.new(self, bad_input:, message:, wrapped_error:))
end

#start_at_csv!(&block) ⇒ Object

Reset the runtime state starting at the CSV section rubocop:disable Naming/BlockForwarding



119
120
121
122
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 119

def start_at_csv!(&block)
  self.line_number = source_code.length_of_code_section + 1
  start!(&block)
end