Class: SeeingIsBelieving

Inherits:
Object
  • Object
show all
Includes:
TracksLineNumbersSeen
Defined in:
lib/seeing_is_believing.rb,
lib/seeing_is_believing/error.rb,
lib/seeing_is_believing/queue.rb,
lib/seeing_is_believing/binary.rb,
lib/seeing_is_believing/result.rb,
lib/seeing_is_believing/version.rb,
lib/seeing_is_believing/has_exception.rb,
lib/seeing_is_believing/expression_list.rb,
lib/seeing_is_believing/syntax_analyzer.rb,
lib/seeing_is_believing/hard_core_ensure.rb,
lib/seeing_is_believing/binary/arg_parser.rb,
lib/seeing_is_believing/binary/line_formatter.rb,
lib/seeing_is_believing/evaluate_by_moving_files.rb,
lib/seeing_is_believing/tracks_line_numbers_seen.rb,
lib/seeing_is_believing/binary/print_results_next_to_lines.rb

Overview

A lot of colouring going on in this file, maybe should extract a debugging object to contain it

Defined Under Namespace

Modules: HasException, TracksLineNumbersSeen Classes: Binary, EvaluateByMovingFiles, ExpressionList, HardCoreEnsure, Queue, Result, SyntaxAnalyzer, TempFileAlreadyExists

Constant Summary collapse

BLANK_REGEX =
/\A\s*\Z/
SeeingIsBelievingError =

all our errors will inherit from this so that a user can catch any error generated by this lib

Class.new StandardError
VERSION =
'0.0.13'

Constants included from TracksLineNumbersSeen

TracksLineNumbersSeen::INITIAL_LINE_NUMBER

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TracksLineNumbersSeen

#max_line_number, #min_line_number, #track_line_number

Constructor Details

#initialize(string_or_stream, options = {}) ⇒ SeeingIsBelieving

Returns a new instance of SeeingIsBelieving.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/seeing_is_believing.rb', line 18

def initialize(string_or_stream, options={})
  @string          = string_or_stream
  @stream          = to_stream string_or_stream
  @matrix_filename = options[:matrix_filename]
  @filename        = options[:filename]
  @stdin           = to_stream options.fetch(:stdin, '')
  @require         = options.fetch :require, []
  @load_path       = options.fetch :load_path, []
  @encoding        = options.fetch :encoding, nil
  @line_number     = 1
end

Class Method Details

.call(*args) ⇒ Object



14
15
16
# File 'lib/seeing_is_believing.rb', line 14

def self.call(*args)
  new(*args).call
end

Instance Method Details

#callObject

I’d lik to refactor this, but I was unsatisfied with the three different things I tried. In the end, I prefer keeping all manipulation of the line number here in the main function And I like that the higher-level construct of how the program gets built can be found here.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/seeing_is_believing.rb', line 33

def call
  @memoized_result ||= begin
    leading_comments = ''

    # extract leading comments (e.g. encoding) so they don't get wrapped in begin/rescue/end
    while next_line_queue.peek =~ /^\s*#/
      leading_comments << next_line_queue.dequeue << "\n"
      @line_number += 1
    end

    # extract leading =begin/=end so they don't get wrapped in begin/rescue/end
    while next_line_queue.peek == '=begin'
      lines = next_line_queue.dequeue << "\n"
      @line_number += 1
      until SyntaxAnalyzer.begin_and_end_comments_are_complete? lines
        lines << next_line_queue.dequeue << "\n"
        @line_number += 1
      end
      leading_comments << lines
    end

    # extract program body
    body = ''
    until next_line_queue.peek.nil? || data_segment?
      expression, expression_size = expression_list.call
      body << expression
      track_line_number @line_number
      @line_number += expression_size
    end

    # extract data segment
    data_segment = ''
    data_segment = "\n#{the_rest_of_the_stream}" if data_segment?

    # build the program
    program = leading_comments << record_exceptions_in(body) << data_segment

    # return the result
    result_for program, max_line_number
  end
end