Module: CompactData::Parser

Defined in:
lib/compactdata/parser/parser.rb

Overview

A Parser module

Constant Summary collapse

REPLACEMENTS =
{
  '\t' => "\t",
  '\n' => "\n",
  '\b' => "\b",
  '\f' => "\f",
  '\r' => "\r",
  '~t' => "\t",
  '~n' => "\n",
  '~b' => "\b",
  '~f' => "\f",
  '~r' => "\r",
  '~\\' => '\\',
  '\\\\' => '\\',
  '~~' => '~',
  '\~' => '~',
  '~(' => '(',
  '\(' => '(',
  '~)' => ')',
  '\)' => ')',
  '~}' => '}',
  '\}' => '}',
  '~[' => '[',
  '\[' => '[',
  '~]' => ']',
  '\]' => ']',
  '~;' => ';',
  '\;' => ';',
  '~`' => '`',
  '\`' => '`',
  '~"' => '"',
  '"' => '"',
  '~=' => '=',
  '\=' => '='
}.freeze

Class Method Summary collapse

Class Method Details

.all_pairs?(arr) ⇒ Boolean

Returns:

  • (Boolean)


288
289
290
291
292
# File 'lib/compactdata/parser/parser.rb', line 288

def self.all_pairs?(arr)
  result = true
  arr.each { |i| result = false unless i.instance_of? CompactData::Model::CompactDataPair }
  result
end

.parse_compactdata(str) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/compactdata/parser/parser.rb', line 40

def self.parse_compactdata(str)
  tokens = CompactData::Tokeniser.tokenise str
  if tokens.nil? || tokens.empty?
    CompactData::Model::CompactData.new nil
  elsif root_primitive? tokens[0]
    CompactData::Model::CompactData.new tokens[0].value
  else
    result = parse_structures(tokens)
    return CompactData::Model::CompactData.new result[0] if result.length == 1

    return CompactData::Model::CompactData.new CompactData::Model::CompactDataMap.new(result) if all_pairs?(result)

    CompactData::Model::CompactData.new CompactData::Model::CompactDataArray.new(result)
  end
end

.parse_compactdata_array(tokens) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/compactdata/parser/parser.rb', line 230

def self.parse_compactdata_array(tokens)
  first_token = tokens.shift
  entries = []

  until tokens.empty?
    peek = tokens[0]
    if !peek.nil? && peek.type == :rbracket
      tokens.shift
      break
    end

    mp = parse_compactdata_value tokens
    entries.push mp

    peek = tokens[0]

    raise ParserError, "Expected ']' near #{first_token}" if peek.nil?

    case peek.type
    when :rbracket
      tokens.shift
      break
    when :struct_sep
      tokens.shift
      peek = tokens[0]
      raise ParserError, "Unexpected ; before ] at #{peek}" if !peek.nil? && peek.type == :rparen
    end
  end
  CompactData::Model::CompactDataArray.new entries
end

.parse_compactdata_map(tokens) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/compactdata/parser/parser.rb', line 199

def self.parse_compactdata_map(tokens)
  first_token = tokens.shift
  entries = []

  until tokens.empty?
    peek = tokens[0]
    if !peek.nil? && peek.type == :rparen
      tokens.shift
      break
    end

    mp = parse_compactdata_value tokens
    entries.push mp

    peek = tokens[0]

    raise ParserError, "Expected ')' near #{first_token}" if peek.nil?

    case peek.type
    when :rparen
      tokens.shift
      break
    when :struct_sep
      tokens.shift
      peek = tokens[0]
      raise ParserError, "Unexpected ; before ] at #{peek}" if !peek.nil? && peek.type == :rparen
    end
  end
  CompactData::Model::CompactDataMap.new entries
end

.parse_compactdata_value(tokens) ⇒ Object

Raises:



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/compactdata/parser/parser.rb', line 72

def self.parse_compactdata_value(tokens)
  first_token = tokens.shift

  case first_token.type
  when :lbracket
    tokens.unshift first_token
    return parse_compactdata_array tokens
  when :lparen
    tokens.unshift first_token
    return parse_compactdata_map tokens
  when :string, :quoted
    peek = tokens[0]
    key = first_token.value
    if !peek.nil? && peek.type == :equals
      tokens.shift
      return CompactData::Model::CompactDataPair.new replace_escapes(unquote(key)), parse_pair_value(tokens)
    end

    if !peek.nil? && (peek.type == :lbracket || peek.type == :lparen)
      return CompactData::Model::CompactDataPair.new replace_escapes(unquote(key)), parse_pair_value(tokens)
    end

    if peek.nil? || peek.type == :struct_sep || peek.type == :rparen || peek.type == :rbracket
      return CompactData::Model::CompactDataString.new(replace_escapes(first_token.value)) if first_token.type == :string 

      return CompactData::Model::CompactDataQuoted.new(replace_escapes(unquote(first_token.value)))
    end
  when :integer
    return CompactData::Model::CompactDataInteger.new first_token.value
  when :float
    return CompactData::Model::CompactDataFloat.new first_token.value
  when :null
    return CompactData::Model::CompactDataBoolNull::CompactData_NULL
  when true
    return CompactData::Model::CompactDataBoolNull::CompactData_TRUE
  when false
    return CompactData::Model::CompactDataBoolNull::CompactData_FALSE
  else
    tokens.unshift first_token
    maybe_primitive = parse_primitive tokens
    return maybe_primitive unless maybe_primitive.nil?
  end
  raise ParserError, "Unexpected token: \'#{first_token}\'"
end

.parse_pair_value(tokens) ⇒ Object

Raises:



117
118
119
120
121
122
123
124
125
126
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
154
155
156
157
# File 'lib/compactdata/parser/parser.rb', line 117

def self.parse_pair_value(tokens)
  first_token = tokens.shift
  case first_token.type
  when :lbracket
    tokens.unshift first_token
    return parse_compactdata_array tokens
  when :lparen
    tokens.unshift first_token
    return parse_compactdata_map tokens
  when :string, :quoted
    peek = tokens[0]

    raise ParserError, "Unexpected token: \'#{first_token}\'" if !peek.nil? && peek.type == :equals

    if !peek.nil? && (peek.type == :lbracket || peek.type == :lparen)
      raise ParserError, "Unexpected token: \'#{first_token}\'"
    end

    if peek.nil? || peek.type == :struct_sep || peek.type == :rparen || peek.type == :rbracket
      return CompactData::Model::CompactDataString.new replace_escapes(first_token.value) if first_token.type == :string

      return CompactData::Model::CompactDataQuoted.new replace_escapes(unquote(first_token.value))
    end
    raise ParserError, "Unexpected token: \'#{first_token}\'"
  when :integer
    return CompactData::Model::CompactDataInteger.new first_token.value
  when :float
    return CompactData::Model::CompactDataFloat.new first_token.value
  when :null
    return CompactData::Model::CompactDataBoolNull::CompactData_NULL
  when true
    return CompactData::Model::CompactDataBoolNull::CompactData_TRUE
  when false
    return CompactData::Model::CompactDataBoolNull::CompactData_FALSE
  else
    tokens.unshift first_token
    maybe_primitive = parse_primitive tokens
    return maybe_primitive unless maybe_primitive.nil?
  end
  raise ParserError, "Unexpected token: \'#{first_token}\'"
end

.parse_primitive(tokens) ⇒ Object

Raises:



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/compactdata/parser/parser.rb', line 159

def self.parse_primitive(tokens)
  result = nil
  tok = tokens.shift

  case tok.type
  when :lparen, :rparen, :lbracket, :rbracket, :equals
    raise ParserError, "Unexpected token: \'#{tok}\'" if tokens.empty?

    tokens.unshift tok
    return nil
  when :null
    result = CompactData::Model::CompactDataBoolNull::CompactData_NULL
  when true
    result = CompactData::Model::CompactDataBoolNull::CompactData_TRUE
  when false
    result = CompactData::Model::CompactDataBoolNull::CompactData_FALSE
  when :quoted
    result = CompactData::Model::CompactDataQuoted.new replace_escapes(unquote(tok.value))
  when :string
    result = CompactData::Model::CompactDataString.new replace_escapes(tok.value)
  when :integer
    result = CompactData::Model::CompactDataInteger.new tok.value
  when :float
    result = CompactData::Model::CompactDataFloat.new tok.value
  else
    raise ParserError, "Unknown token type in: \'#{tok}\'"
  end

  peek = tokens[0]
  raise ParserError, 'Only one primitive allowed at the root.' if !peek.nil? && peek.type == :struct_sep

  if !peek.nil? && (peek.type == :lparen || peek.type == :lbracket || peek.type == :equals)
    s.unshift tok
    return nil
  elsif !peek.nil?
    raise ParserError, "Unexpected token: \'#{peek}\'"
  end
  result
end

.parse_structures(tokens) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/compactdata/parser/parser.rb', line 60

def self.parse_structures(tokens)
  result = []
  until tokens.empty?
    result.push parse_compactdata_value(tokens)
    expect_separator = tokens.shift
    if !expect_separator.nil? && expect_separator.type != :struct_sep
      raise ParserError, "Expected ';' near #{tokens}"
    end
  end
  result
end

.replace_escapes(str) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/compactdata/parser/parser.rb', line 271

def self.replace_escapes(str)
  return str unless str.instance_of?(String)

  result = str
  i = 0
  while i < str.length
    REPLACEMENTS.each_pair do |key, value|
      if result.slice(i..).start_with?(key)
        result = result.sub(key, value)
        break
      end
    end
    i += 1
  end
  result
end

.root_primitive?(token) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/compactdata/parser/parser.rb', line 56

def self.root_primitive?(token)
  %i[string quoted null true false integer float].include?(token)
end

.unquote(str) ⇒ Object



261
262
263
264
265
266
267
268
269
# File 'lib/compactdata/parser/parser.rb', line 261

def self.unquote(str)
  return str unless str.instance_of?(String)

  if (str.start_with?('`') && str.end_with?('`')) || (str.start_with?('"') && str.end_with?('"'))
    str[1..-2]
  else
    str
  end
end