Class: Plurimath::Latex::Parse

Inherits:
Parslet::Parser
  • Object
show all
Includes:
BaseNumberPrefix::Parser
Defined in:
lib/plurimath/latex/parse.rb

Instance Method Summary collapse

Methods included from BaseNumberPrefix::Parser

included

Instance Method Details

#arr_to_expression(array, name) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/plurimath/latex/parse.rb', line 196

def arr_to_expression(array, name)
  @@new_hash ||= {}
  type = array.first.class
  @@new_hash[name] ||= array.reduce do |expression, expr_string|
    expression = str(expression).as(name) if expression.is_a?(type)
    expression | str(expr_string).as(name)
  end
end

#color_rulesObject



275
276
277
278
# File 'lib/plurimath/latex/parse.rb', line 275

def color_rules
  (str("{") >> slash >> str("color").as(:binary) >> color.as(:first_value) >> (sequence >> iteration.maybe).as(:second_value).maybe >> str("}")) |
    (slash >> str("color").as(:binary) >> color.as(:first_value) >> expression.as(:second_value).maybe)
end

#decimal_markerObject



205
206
207
208
209
# File 'lib/plurimath/latex/parse.rb', line 205

def decimal_marker
  # Latex::Parser entity-encodes input before Parslet sees it, so
  # non-ASCII locale markers must match that encoded parser input.
  str(Utility.string_to_html_entity(Plurimath.configuration.decimal))
end

#dynamic_power_baseObject



268
269
270
271
272
273
# File 'lib/plurimath/latex/parse.rb', line 268

def dynamic_power_base
  (base >> intermediate_exp.as(:subscript) >> power >> intermediate_exp.as(:supscript)) |
    (power >> intermediate_exp.as(:supscript) >> base >> intermediate_exp.as(:subscript)) |
    (power >> intermediate_exp.as(:supscript)) |
    (base >> intermediate_exp.as(:subscript))
end

#dynamic_rules(expr, name) ⇒ Object



221
222
223
224
225
226
227
228
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
# File 'lib/plurimath/latex/parse.rb', line 221

def dynamic_rules(expr, name)
  first_value = str(expr.to_s)
  case name
  when :operant
    (first_value.as(:operant) | slashed_value(first_value, :symbols))
  when :symbols
    slashed_value(first_value, :symbols)
  when :unary
    unary_rules(first_value)
  when :fonts
    (slashed_value(first_value,
                   :fonts) >> (binary_functions | intermediate_exp).as(:intermediate_exp))
  when :power_base
    (slashed_value(first_value,
                   :binary) >> dynamic_power_base).as(:power_base) |
      slashed_value(first_value, :binary)
  when :underover
    (slashed_value(first_value, :underover) >> dynamic_power_base) |
      (slashed_value(first_value,
                     :underover) >> intermediate_exp.maybe.as(:first_value) >> dynamic_power_base) |
      slashed_value(first_value, :underover)
  when :binary
    (slashed_value(first_value,
                   :binary) >> intermediate_exp.as(:first_value) >> intermediate_exp.as(:second_value)).as(:binary)
  when :text
    (slashed_value(first_value,
                   :text) >> (str("{") >> parsing_text_values.as(:first_value) >> str("}")))
  when :ternary
    (slashed_value(first_value,
                   :ternary_functions) >> dynamic_power_base >> sequence.as(:third_value).maybe).as(:ternary_class) |
      slashed_value(first_value, :ternary)
  end
end

#hash_to_expression(hash) ⇒ Object



211
212
213
214
215
216
217
218
219
# File 'lib/plurimath/latex/parse.rb', line 211

def hash_to_expression(hash)
  @@expression ||= hash.reduce do |expression, (key, value)|
    if expression.is_a?(Array)
      expression = dynamic_rules(expression.first,
                                 expression.last)
    end
    expression | dynamic_rules(key, value)
  end
end

#slashed_value(first_value, name = nil) ⇒ Object



255
256
257
# File 'lib/plurimath/latex/parse.rb', line 255

def slashed_value(first_value, name = nil)
  (slash >> first_value.as(name))
end

#unary_rules(first_value) ⇒ Object



259
260
261
262
263
264
265
266
# File 'lib/plurimath/latex/parse.rb', line 259

def unary_rules(first_value)
  (slashed_value(first_value, :unary_functions) >> dynamic_power_base) |
    (slashed_value(first_value,
                   :unary) >> left_right.as(:first_value)).as(:unary_functions) |
    (slashed_value(first_value,
                   :unary) >> intermediate_exp.as(:first_value)).as(:unary_functions) |
    slashed_value(first_value, :unary)
end