Class: SeeingIsBelieving

Inherits:
Object
  • Object
show all
Includes:
TracksLineNumbersSeen
Defined in:
lib/seeing_is_believing.rb,
lib/seeing_is_believing/line.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/debugger.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/align_file.rb,
lib/seeing_is_believing/binary/align_line.rb,
lib/seeing_is_believing/binary/arg_parser.rb,
lib/seeing_is_believing/binary/align_chunk.rb,
lib/seeing_is_believing/binary/line_formatter.rb,
lib/seeing_is_believing/binary/add_annotations.rb,
lib/seeing_is_believing/remove_inline_comments.rb,
lib/seeing_is_believing/evaluate_by_moving_files.rb,
lib/seeing_is_believing/tracks_line_numbers_seen.rb,
lib/seeing_is_believing/binary/remove_previous_annotations.rb

Overview

I think there is a bug where with xmpfilter_style set, the exceptions won’t be shown. But it’s not totally clear how to show them with this option set, anyway. probably do what xmpfilter does and print them at the bottom of the file (probably do this regardless of whether xmpfilter_style is set)

Would also be nice to support

1 + 1
# => 2

style updates like xmpfilter does

Defined Under Namespace

Modules: HasException, RemoveInlineComments, TracksLineNumbersSeen Classes: Binary, Debugger, EvaluateByMovingFiles, ExpressionList, HardCoreEnsure, Line, Queue, RecordedException, 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 =
'1.0.1'

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(program, options = {}) ⇒ SeeingIsBelieving

Returns a new instance of SeeingIsBelieving.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/seeing_is_believing.rb', line 22

def initialize(program, options={})
  program_string   = RemoveInlineComments::NonLeading.call program
  @stream          = to_stream program_string
  @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
  @timeout         = options[:timeout]
  @debugger        = options.fetch :debugger, Debugger.new(enabled: false)

  debugger.context("SOURCE WITHOUT COMMENTS") { program_string }
end

Class Method Details

.call(*args) ⇒ Object



18
19
20
# File 'lib/seeing_is_believing.rb', line 18

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

Instance Method Details

#callObject

I’d like 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.



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
74
75
76
77
78
79
80
81
82
83
# File 'lib/seeing_is_believing.rb', line 41

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


    # extract leading comments (e.g. encoding) so they don't get wrapped in begin/rescue/end
    while SyntaxAnalyzer.line_is_comment?(next_line_queue.peek)
      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 SyntaxAnalyzer.begins_multiline_comment?(next_line_queue.peek)
      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.empty? || 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
    debugger.context("TRANSLATED PROGRAM") { program }

    # return the result
    result_for program, max_line_number
  end
end