Module: MODL::Parser

Defined in:
lib/modl/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)


287
288
289
290
291
# File 'lib/modl/parser/parser.rb', line 287

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

.parse_modl(str) ⇒ Object



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

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

    return MODL::Model::Modl.new MODL::Model::ModlMap.new(result) if all_pairs?(result)

    MODL::Model::Modl.new MODL::Model::ModlArray.new(result)
  end
end

.parse_modl_array(tokens) ⇒ Object



229
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
# File 'lib/modl/parser/parser.rb', line 229

def self.parse_modl_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_modl_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
  MODL::Model::ModlArray.new entries
end

.parse_modl_map(tokens) ⇒ Object



198
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
# File 'lib/modl/parser/parser.rb', line 198

def self.parse_modl_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_modl_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
  MODL::Model::ModlMap.new entries
end

.parse_modl_value(tokens) ⇒ Object

Raises:



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

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

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

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

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

      return MODL::Model::ModlQuoted.new(replace_escapes(unquote(first_token.value)))
    end
  when :integer
    return MODL::Model::ModlInteger.new first_token.value
  when :float
    return MODL::Model::ModlFloat.new first_token.value
  when :null
    return MODL::Model::ModlBoolNull::MODL_NULL
  when true
    return MODL::Model::ModlBoolNull::MODL_TRUE
  when false
    return MODL::Model::ModlBoolNull::MODL_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:



116
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
# File 'lib/modl/parser/parser.rb', line 116

def self.parse_pair_value(tokens)
  first_token = tokens.shift
  case first_token.type
  when :lbracket
    tokens.unshift first_token
    return parse_modl_array tokens
  when :lparen
    tokens.unshift first_token
    return parse_modl_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 MODL::Model::ModlString.new replace_escapes(first_token.value) if first_token.type == :string

      return MODL::Model::ModlQuoted.new replace_escapes(unquote(first_token.value))
    end
    raise ParserError, "Unexpected token: \'#{first_token}\'"
  when :integer
    return MODL::Model::ModlInteger.new first_token.value
  when :float
    return MODL::Model::ModlFloat.new first_token.value
  when :null
    return MODL::Model::ModlBoolNull::MODL_NULL
  when true
    return MODL::Model::ModlBoolNull::MODL_TRUE
  when false
    return MODL::Model::ModlBoolNull::MODL_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:



158
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
# File 'lib/modl/parser/parser.rb', line 158

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 = MODL::Model::ModlBoolNull::MODL_NULL
  when true
    result = MODL::Model::ModlBoolNull::MODL_TRUE
  when false
    result = MODL::Model::ModlBoolNull::MODL_FALSE
  when :quoted
    result = MODL::Model::ModlQuoted.new replace_escapes(unquote(tok.value))
  when :string
    result = MODL::Model::ModlString.new replace_escapes(tok.value)
  when :integer
    result = MODL::Model::ModlInteger.new tok.value
  when :float
    result = MODL::Model::ModlFloat.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



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

def self.parse_structures(tokens)
  result = []
  until tokens.empty?
    result.push parse_modl_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



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

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)


55
56
57
# File 'lib/modl/parser/parser.rb', line 55

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

.unquote(str) ⇒ Object



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

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