Class: CSVPlusPlus::Compiler

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/csv_plus_plus/compiler.rb

Overview

Encapsulates the parsing and building of objects (Template -> Row -> Cell). Variable resolution is delegated to the Scope

rubocop:disable Metrics/ClassLength

Direct Known Subclasses

BenchmarkedCompiler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options:, runtime:) ⇒ Compiler

Returns a new instance of Compiler.

Parameters:



46
47
48
49
50
51
52
53
54
55
# File 'lib/csv_plus_plus/compiler.rb', line 46

def initialize(options:, runtime:)
  @options = options
  @runtime = runtime

  # TODO: infer a type
  # allow user-supplied key/values to override anything global or from the code section
  @runtime.scope.def_variables(
    options.key_values.transform_values { |v| ::CSVPlusPlus::Entities::String.new(v.to_s) }
  )
end

Instance Attribute Details

#optionsOptions (readonly)

The Options to compile with

Returns:

  • (Options)

    the current value of options



11
12
13
# File 'lib/csv_plus_plus/compiler.rb', line 11

def options
  @options
end

#runtimeRuntime (readonly)

The runtime execution

Returns:

  • (Runtime)

    the current value of runtime



11
12
13
# File 'lib/csv_plus_plus/compiler.rb', line 11

def runtime
  @runtime
end

Class Method Details

.with_compiler(options:, runtime:, &block) ⇒ Object

Create a compiler and make sure it gets cleaned up

Parameters:

  • options (Options)
  • runtime (Runtime)

    The initial Runtime for the compiler



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/csv_plus_plus/compiler.rb', line 31

def self.with_compiler(options:, runtime:, &block)
  if options.verbose
    ::CSVPlusPlus::BenchmarkedCompiler.with_benchmarks(options:, runtime:) do |c|
      block.call(c)
    end
  else
    block.call(new(options:, runtime:))
  end
ensure
  runtime.position.cleanup!
end

Instance Method Details

#benchmark=(benchmark) ⇒ Object

Attach a Benchmark and a place to store timings to the compiler class.

Parameters:

  • benchmark (Benchmark)

    A Benchmark instance



61
62
63
64
# File 'lib/csv_plus_plus/compiler.rb', line 61

def benchmark=(benchmark)
  @benchmark = ::T.let(benchmark, ::T.nilable(::Benchmark::Report))
  @timings = ::T.let([], ::T.nilable(::T::Array[::Benchmark::Tms]))
end

#compile_templateTemplate

Compile a template and return a ::CSVPlusPlus::Template instance ready to be written with a Writer

Returns:



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/csv_plus_plus/compiler.rb', line 70

def compile_template
  parse_code_section!
  rows = parse_csv_section!

  ::CSVPlusPlus::Template.new(rows:, runtime: @runtime).tap do |t|
    t.validate_infinite_expands
    expanding! { t.expand_rows! }
    bind_all_vars! { t.bind_all_vars!(@runtime) }
    resolve_all_cells!(t)
  end
end

#outputting!(&block) ⇒ Object

Write the compiled results



84
85
86
# File 'lib/csv_plus_plus/compiler.rb', line 84

def outputting!(&block)
  @runtime.start_at_csv! { block.call(@runtime.position) }
end