Class: CSVPlusPlus::Runtime::Runtime
- Inherits:
-
Object
- Object
- CSVPlusPlus::Runtime::Runtime
- 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
-
#cell ⇒ Cell
The current cell being processed.
-
#cell_index ⇒ Integer
The index of the current cell being processed (starts at 0).
-
#filename ⇒ String?
readonly
The filename that the input came from (mostly used for debugging since
filenamecan benilif it’s read from stdin.. -
#functions ⇒ Object
readonly
Returns the value of attribute functions.
-
#line_number ⇒ Integer
The line number of the original csvpp template (starts at 1).
-
#row_index ⇒ Integer
The index of the current row being processed (starts at 0).
-
#source_code ⇒ Object
readonly
Returns the value of attribute source_code.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Instance Method Summary collapse
-
#builtin_function?(fn_id) ⇒ T::Boolean
Is
fn_ida builtin function?. -
#builtin_variable?(var_id) ⇒ T::Boolean
Is
var_ida builtin variable?. -
#initialize(source_code:, functions: {}, variables: {}) ⇒ Runtime
constructor
A new instance of Runtime.
-
#parsing_code_section? ⇒ T::Boolean
Is the parser currently inside of the code section? (includes the
---). -
#parsing_csv_section? ⇒ T::Boolean
Is the parser currently inside of the CSV section?.
-
#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).
-
#raise_modifier_syntax_error(message, bad_input, wrapped_error: nil) ⇒ Object
Called when an error is encountered while parsing a modifier.
-
#start_at_csv!(&block) ⇒ Object
Reset the runtime state starting at the CSV section rubocop:disable Naming/BlockForwarding.
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.
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
#cell ⇒ Cell
The current cell being processed
17 18 19 |
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 17 def cell @cell end |
#cell_index ⇒ Integer
The index of the current cell being processed (starts at 0)
17 18 19 |
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 17 def cell_index @cell_index end |
#filename ⇒ String? (readonly)
The filename that the input came from (mostly used for debugging since filename can be nil if it’s read from stdin.
17 18 19 |
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 17 def filename @filename end |
#functions ⇒ Object (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_number ⇒ Integer
The line number of the original csvpp template (starts at 1)
17 18 19 |
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 17 def line_number @line_number end |
#row_index ⇒ Integer
The index of the current row being processed (starts at 0)
17 18 19 |
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 17 def row_index @row_index end |
#source_code ⇒ Object (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 |
#variables ⇒ Object (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?
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?
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 ---)
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?
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
97 98 99 |
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 97 def raise_formula_syntax_error(, bad_input, wrapped_error: nil) raise(::CSVPlusPlus::Error::FormulaSyntaxError.new(, 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.
110 111 112 |
# File 'lib/csv_plus_plus/runtime/runtime.rb', line 110 def raise_modifier_syntax_error(, 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 |