Class: JsonCompleter::Scanners::ParsedStringToken

Inherits:
Struct
  • Object
show all
Defined in:
lib/json_completer/scanners.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role:, slot: nil, context: nil, buffer: nil, escape_state: nil, unicode_digits: nil, pending_high_surrogate: nil, visible_key: nil, visible_key_replaced_value: nil, visible_key_replaced_present: false) ⇒ ParsedStringToken

Returns a new instance of ParsedStringToken.



76
77
78
79
80
81
82
# File 'lib/json_completer/scanners.rb', line 76

def initialize(
  role:, slot: nil, context: nil, buffer: nil, escape_state: nil, unicode_digits: nil,
  pending_high_surrogate: nil, visible_key: nil, visible_key_replaced_value: nil, visible_key_replaced_present: false
)
  super
  self.buffer ||= String.new
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer

Returns:

  • (Object)

    the current value of buffer



71
72
73
# File 'lib/json_completer/scanners.rb', line 71

def buffer
  @buffer
end

#contextObject

Returns the value of attribute context

Returns:

  • (Object)

    the current value of context



71
72
73
# File 'lib/json_completer/scanners.rb', line 71

def context
  @context
end

#escape_stateObject

Returns the value of attribute escape_state

Returns:

  • (Object)

    the current value of escape_state



71
72
73
# File 'lib/json_completer/scanners.rb', line 71

def escape_state
  @escape_state
end

#pending_high_surrogateObject

Returns the value of attribute pending_high_surrogate

Returns:

  • (Object)

    the current value of pending_high_surrogate



71
72
73
# File 'lib/json_completer/scanners.rb', line 71

def pending_high_surrogate
  @pending_high_surrogate
end

#roleObject

Returns the value of attribute role

Returns:

  • (Object)

    the current value of role



71
72
73
# File 'lib/json_completer/scanners.rb', line 71

def role
  @role
end

#slotObject

Returns the value of attribute slot

Returns:

  • (Object)

    the current value of slot



71
72
73
# File 'lib/json_completer/scanners.rb', line 71

def slot
  @slot
end

#unicode_digitsObject

Returns the value of attribute unicode_digits

Returns:

  • (Object)

    the current value of unicode_digits



71
72
73
# File 'lib/json_completer/scanners.rb', line 71

def unicode_digits
  @unicode_digits
end

#visible_keyObject

Returns the value of attribute visible_key

Returns:

  • (Object)

    the current value of visible_key



71
72
73
# File 'lib/json_completer/scanners.rb', line 71

def visible_key
  @visible_key
end

#visible_key_replaced_presentObject

Returns the value of attribute visible_key_replaced_present

Returns:

  • (Object)

    the current value of visible_key_replaced_present



71
72
73
# File 'lib/json_completer/scanners.rb', line 71

def visible_key_replaced_present
  @visible_key_replaced_present
end

#visible_key_replaced_valueObject

Returns the value of attribute visible_key_replaced_value

Returns:

  • (Object)

    the current value of visible_key_replaced_value



71
72
73
# File 'lib/json_completer/scanners.rb', line 71

def visible_key_replaced_value
  @visible_key_replaced_value
end

Instance Method Details

#append_simple_escape(byte) ⇒ Object

ASCII escape bytes: 98/102/110/114/116 = b/f/n/r/t.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/json_completer/scanners.rb', line 93

def append_simple_escape(byte)
  buffer << case byte
            when 98
              "\b"
            when 102
              "\f"
            when 110
              "\n"
            when 114
              "\r"
            when 116
              "\t"
            else
              byte
            end
end

#append_slice(input, start_index, length) ⇒ Object



88
89
90
# File 'lib/json_completer/scanners.rb', line 88

def append_slice(input, start_index, length)
  buffer << input.byteslice(start_index, length)
end

#append_unicode_digit(byte) ⇒ Object



123
124
125
# File 'lib/json_completer/scanners.rb', line 123

def append_unicode_digit(byte)
  unicode_digits << byte
end

#finish_unicode_escape!Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/json_completer/scanners.rb', line 127

def finish_unicode_escape!
  codepoint = unicode_digits.to_i(16)

  if pending_high_surrogate
    unless codepoint.between?(0xDC00, 0xDFFF)
      self.pending_high_surrogate = nil
      return :invalid_unicode
    end

    combined = 0x10000 + ((pending_high_surrogate - 0xD800) << 10) + (codepoint - 0xDC00)
    buffer << combined.chr(Encoding::UTF_8)
    self.pending_high_surrogate = nil
    return :ok
  end

  if codepoint.between?(0xD800, 0xDBFF)
    self.pending_high_surrogate = codepoint
  elsif codepoint.between?(0xDC00, 0xDFFF)
    return :invalid_unicode
  else
    buffer << codepoint.chr(Encoding::UTF_8)
  end
  :ok
rescue RangeError
  self.pending_high_surrogate = nil
  :invalid_unicode
end

#invalid_unicode!Object



155
156
157
158
159
# File 'lib/json_completer/scanners.rb', line 155

def invalid_unicode!
  self.escape_state = nil
  self.unicode_digits = nil
  self.pending_high_surrogate = nil
end

#start_escape!Object



84
85
86
# File 'lib/json_completer/scanners.rb', line 84

def start_escape!
  self.escape_state = :backslash
end

#start_unicode_escape!Object



119
120
121
# File 'lib/json_completer/scanners.rb', line 119

def start_unicode_escape!
  self.unicode_digits = String.new
end

#terminate!Object



161
# File 'lib/json_completer/scanners.rb', line 161

def terminate!; end

#valid_simple_escape?(byte) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
# File 'lib/json_completer/scanners.rb', line 110

def valid_simple_escape?(byte)
  case byte
  when 34, 92, 47, 98, 102, 110, 114, 116
    true
  else
    false
  end
end