Class: CSVPlusPlus::Language::Compiler

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

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime:, options:, scope: nil) ⇒ Compiler

Returns a new instance of Compiler.

Parameters:



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

def initialize(runtime:, options:, scope: nil)
  @options = options
  @runtime = runtime
  @scope = scope || ::CSVPlusPlus::Language::Scope.new(runtime:)
end

Instance Attribute Details

#benchmarkObject (readonly)

Returns the value of attribute benchmark.



20
21
22
# File 'lib/csv_plus_plus/language/compiler.rb', line 20

def benchmark
  @benchmark
end

#optionsOptions (readonly)

The Options to compile with

Returns:

  • (Options)

    the current value of options



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

def options
  @options
end

#runtimeRuntime (readonly)

The runtime execution

Returns:

  • (Runtime)

    the current value of runtime



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

def runtime
  @runtime
end

#scopeScope (readonly)

Scope for variable resolution

Returns:

  • (Scope)

    the current value of scope



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

def scope
  @scope
end

#timingsObject (readonly)

Returns the value of attribute timings.



20
21
22
# File 'lib/csv_plus_plus/language/compiler.rb', line 20

def timings
  @timings
end

Class Method Details

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

Create a compiler and make sure it gets cleaned up

Parameters:

  • runtime (Runtime)

    The initial Runtime for the compiler

  • options (Options)


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/csv_plus_plus/language/compiler.rb', line 26

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

Instance Method Details

#compile_templateTemplate

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

Returns:



57
58
59
60
61
62
63
64
65
66
# File 'lib/csv_plus_plus/language/compiler.rb', line 57

def compile_template
  parse_code_section!
  rows = parse_csv_section!

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

#outputting!Object

Write the compiled results



49
50
51
52
# File 'lib/csv_plus_plus/language/compiler.rb', line 49

def outputting!
  @runtime.start_at_csv!
  yield
end

#to_sString

Returns:

  • (String)


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

def to_s
  "Compiler(options: #{@options}, runtime: #{@runtime}, scope: #{@scope})"
end