Class: IpynbDiff::IpynbSymbolMap

Inherits:
Object
  • Object
show all
Defined in:
lib/ipynb_symbol_map.rb

Overview

Creates a symbol map for a ipynb file (JSON format)

Constant Summary collapse

WHITESPACE_CHARS =
["\t", "\r", ' ', "\n"].freeze
VALUE_STOPPERS =
[',', '[', ']', '{', '}', *WHITESPACE_CHARS].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notebook, objects_to_ignore = []) ⇒ IpynbSymbolMap

Returns a new instance of IpynbSymbolMap.



21
22
23
24
25
26
27
# File 'lib/ipynb_symbol_map.rb', line 21

def initialize(notebook, objects_to_ignore = [])
  @chars = notebook.chars
  @current_line = 0
  @char_idx = 0
  @results = {}
  @objects_to_ignore = objects_to_ignore
end

Instance Attribute Details

#char_idxObject (readonly)

Returns the value of attribute char_idx.



15
16
17
# File 'lib/ipynb_symbol_map.rb', line 15

def char_idx
  @char_idx
end

#current_lineObject (readonly)

Returns the value of attribute current_line.



15
16
17
# File 'lib/ipynb_symbol_map.rb', line 15

def current_line
  @current_line
end

#resultsObject (readonly)

Returns the value of attribute results.



15
16
17
# File 'lib/ipynb_symbol_map.rb', line 15

def results
  @results
end

Class Method Details

.parse(notebook, objects_to_ignore = []) ⇒ Object



10
11
12
# File 'lib/ipynb_symbol_map.rb', line 10

def parse(notebook, objects_to_ignore = [])
  IpynbSymbolMap.new(notebook, objects_to_ignore).parse('')
end

Instance Method Details

#add_result(key, line_number) ⇒ Object



117
118
119
# File 'lib/ipynb_symbol_map.rb', line 117

def add_result(key, line_number)
  @results[key] = line_number
end

#check_for_new_lineObject



156
157
158
# File 'lib/ipynb_symbol_map.rb', line 156

def check_for_new_line
  @current_line += 1 if current_char == "\n"
end

#current_charObject



142
143
144
145
146
# File 'lib/ipynb_symbol_map.rb', line 142

def current_char
  raise_if_file_ended

  @chars[@char_idx]
end

#current_should_be(another_char) ⇒ Object

Raises:



152
153
154
# File 'lib/ipynb_symbol_map.rb', line 152

def current_should_be(another_char)
  raise InvalidTokenError unless current_char == another_char
end

#increment_char_indexObject



133
134
135
# File 'lib/ipynb_symbol_map.rb', line 133

def increment_char_index
  @char_idx += 1
end

#next_and_skip_whitespacesObject



137
138
139
140
# File 'lib/ipynb_symbol_map.rb', line 137

def next_and_skip_whitespaces
  increment_char_index
  skip_whitespaces
end

#parse(prefix = '.') ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ipynb_symbol_map.rb', line 29

def parse(prefix = '.')
  raise_if_file_ended

  skip_whitespaces

  if (c = current_char) == '"'
    parse_string
  elsif c == '['
    parse_array(prefix)
  elsif c == '{'
    parse_object(prefix)
  else
    parse_value
  end

  results
end

#parse_array(prefix) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ipynb_symbol_map.rb', line 47

def parse_array(prefix)
  # [1, 2, {"some": "object"}, [1]]

  i = 0

  current_should_be '['

  loop do
    raise_if_file_ended

    break if skip_beginning(']')

    new_prefix = "#{prefix}.#{i}"

    add_result(new_prefix, current_line)

    parse(new_prefix)

    i += 1
  end
end

#parse_object(prefix) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ipynb_symbol_map.rb', line 69

def parse_object(prefix)
  # {"name":"value", "another_name": [1, 2, 3]}

  current_should_be '{'

  loop do
    raise_if_file_ended

    break if skip_beginning('}')

    prop_name = parse_string(return_value: true)

    next_and_skip_whitespaces

    current_should_be ':'

    next_and_skip_whitespaces

    if @objects_to_ignore.include? prop_name
      skip
    else
      new_prefix = "#{prefix}.#{prop_name}"

      add_result(new_prefix, current_line)

      parse(new_prefix)
    end
  end
end

#parse_string(return_value: false) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ipynb_symbol_map.rb', line 99

def parse_string(return_value: false)
  current_should_be '"'
  init_idx = @char_idx

  loop do
    increment_char_index

    raise_if_file_ended

    if current_char == '"' && !prev_backslash?
      init_idx += 1
      break
    end
  end

  @chars[init_idx...@char_idx].join if return_value
end

#parse_valueObject



121
122
123
# File 'lib/ipynb_symbol_map.rb', line 121

def parse_value
  increment_char_index until raise_if_file_ended || VALUE_STOPPERS.include?(current_char)
end

#prev_backslash?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/ipynb_symbol_map.rb', line 148

def prev_backslash?
  @chars[@char_idx - 1] == '\\' && @chars[@char_idx - 2] != '\\'
end

#raise_if_file_endedObject



160
161
162
# File 'lib/ipynb_symbol_map.rb', line 160

def raise_if_file_ended
  @char_idx >= @chars.size && raise(InvalidTokenError)
end

#skipObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ipynb_symbol_map.rb', line 164

def skip
  raise_if_file_ended

  skip_whitespaces

  if (c = current_char) == '"'
    parse_string
  elsif c == '['
    skip_array
  elsif c == '{'
    skip_object
  else
    parse_value
  end
end

#skip_arrayObject



180
181
182
183
184
185
186
187
188
# File 'lib/ipynb_symbol_map.rb', line 180

def skip_array
  loop do
    raise_if_file_ended

    break if skip_beginning(']')

    skip
  end
end

#skip_beginning(closing_char) ⇒ Object



209
210
211
212
213
214
215
216
217
218
# File 'lib/ipynb_symbol_map.rb', line 209

def skip_beginning(closing_char)

  check_for_new_line

  next_and_skip_whitespaces

  return true if current_char == closing_char

  next_and_skip_whitespaces if current_char == ','
end

#skip_objectObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/ipynb_symbol_map.rb', line 190

def skip_object

  loop do
    raise_if_file_ended

    break if skip_beginning('}')

    parse_string

    next_and_skip_whitespaces

    current_should_be ':'

    next_and_skip_whitespaces

    skip
  end
end

#skip_whitespacesObject



125
126
127
128
129
130
131
# File 'lib/ipynb_symbol_map.rb', line 125

def skip_whitespaces
  while WHITESPACE_CHARS.include?(current_char)
    raise_if_file_ended
    check_for_new_line
    increment_char_index
  end
end