Class: IpynbDiff::IpynbSymbolMap
- Inherits:
-
Object
- Object
- IpynbDiff::IpynbSymbolMap
- 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
-
#char_idx ⇒ Object
readonly
Returns the value of attribute char_idx.
-
#current_line ⇒ Object
readonly
Returns the value of attribute current_line.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Class Method Summary collapse
Instance Method Summary collapse
- #add_result(key, line_number) ⇒ Object
- #check_for_new_line ⇒ Object
- #current_char ⇒ Object
- #current_should_be(another_char) ⇒ Object
- #increment_char_index ⇒ Object
-
#initialize(notebook) ⇒ IpynbSymbolMap
constructor
A new instance of IpynbSymbolMap.
- #next_and_skip_whitespaces ⇒ Object
- #parse(prefix = '.') ⇒ Object
- #parse_array(prefix) ⇒ Object
- #parse_object(prefix) ⇒ Object
- #parse_string ⇒ Object
- #parse_value ⇒ Object
- #skip_beginning(closing_char) ⇒ Object
- #skip_whitespaces ⇒ Object
Constructor Details
#initialize(notebook) ⇒ IpynbSymbolMap
Returns a new instance of IpynbSymbolMap.
21 22 23 24 25 26 |
# File 'lib/ipynb_symbol_map.rb', line 21 def initialize(notebook) @chars = notebook.chars @current_line = 0 @char_idx = 0 @results = {} end |
Instance Attribute Details
#char_idx ⇒ Object (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_line ⇒ Object (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 |
#results ⇒ Object (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) ⇒ Object
10 11 12 |
# File 'lib/ipynb_symbol_map.rb', line 10 def parse(notebook) IpynbSymbolMap.new(notebook).parse('') end |
Instance Method Details
#add_result(key, line_number) ⇒ Object
104 105 106 |
# File 'lib/ipynb_symbol_map.rb', line 104 def add_result(key, line_number) @results[key] = line_number end |
#check_for_new_line ⇒ Object
136 137 138 |
# File 'lib/ipynb_symbol_map.rb', line 136 def check_for_new_line @current_line += 1 if current_char == "\n" end |
#current_char ⇒ Object
128 129 130 |
# File 'lib/ipynb_symbol_map.rb', line 128 def current_char @chars[@char_idx] end |
#current_should_be(another_char) ⇒ Object
132 133 134 |
# File 'lib/ipynb_symbol_map.rb', line 132 def current_should_be(another_char) raise InvalidTokenError unless current_char == another_char end |
#increment_char_index ⇒ Object
119 120 121 |
# File 'lib/ipynb_symbol_map.rb', line 119 def increment_char_index @char_idx += 1 end |
#next_and_skip_whitespaces ⇒ Object
123 124 125 126 |
# File 'lib/ipynb_symbol_map.rb', line 123 def next_and_skip_whitespaces increment_char_index skip_whitespaces end |
#parse(prefix = '.') ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ipynb_symbol_map.rb', line 28 def parse(prefix = '.') 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ipynb_symbol_map.rb', line 44 def parse_array(prefix) # [1, 2, {"some": "object"}, [1]] i = 0 current_should_be '[' loop do 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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ipynb_symbol_map.rb', line 64 def parse_object(prefix) # {"name":"value", "another_name": [1, 2, 3]} current_should_be '{' loop do break if skip_beginning('}') prop_name = parse_string new_prefix = "#{prefix}.#{prop_name}" add_result(new_prefix, current_line) next_and_skip_whitespaces current_should_be ':' next_and_skip_whitespaces parse(new_prefix) end end |
#parse_string ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ipynb_symbol_map.rb', line 88 def parse_string value = '' prev_char = nil current_should_be '"' loop do increment_char_index break if (c = current_char) == '"' && prev_char != '\\' value += (prev_char = c) end value end |
#parse_value ⇒ Object
108 109 110 |
# File 'lib/ipynb_symbol_map.rb', line 108 def parse_value increment_char_index until VALUE_STOPPERS.include?(current_char) end |
#skip_beginning(closing_char) ⇒ Object
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/ipynb_symbol_map.rb', line 140 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_whitespaces ⇒ Object
112 113 114 115 116 117 |
# File 'lib/ipynb_symbol_map.rb', line 112 def skip_whitespaces while WHITESPACE_CHARS.include?(current_char) check_for_new_line increment_char_index end end |