Class: Plurimath::Latex::Parse

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

Instance Method Summary collapse

Instance Method Details

#arr_to_expression(array, name) ⇒ Object



164
165
166
167
168
169
170
171
# File 'lib/plurimath/latex/parse.rb', line 164

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

#dynamic_power_baseObject



215
216
217
218
219
220
# File 'lib/plurimath/latex/parse.rb', line 215

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



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/plurimath/latex/parse.rb', line 180

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("{") >> (match("[^\}]").repeat).as(:first_value) >> str("}")))
  end
end

#hash_to_expression(hash) ⇒ Object



173
174
175
176
177
178
# File 'lib/plurimath/latex/parse.rb', line 173

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

#slashed_value(first_value, name = nil) ⇒ Object



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

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

#unary_rules(first_value) ⇒ Object



209
210
211
212
213
# File 'lib/plurimath/latex/parse.rb', line 209

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