Class: Plurimath::Asciimath::Parse

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

Instance Method Summary collapse

Instance Method Details

#arr_to_expression(arr, name = nil) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/plurimath/asciimath/parse.rb', line 167

def arr_to_expression(arr, name = nil)
  type = arr.first.class
  arr.reduce do |expression, expr_string|
    expression = str(expression).as(name) if expression.is_a?(type)
    expression | str(expr_string).as(name)
  end
end

#dynamic_parser_rules(expr) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/plurimath/asciimath/parse.rb', line 190

def dynamic_parser_rules(expr)
  first_value = str(expr.first.to_s)
  case expr.last
  when :symbol then (str("\\").as(:slash) >> match("\s").repeat >> str("\n")) | first_value.as(:symbol)
  when :unary_class then unary_functions(first_value)
  when :fonts then first_value.as(:fonts_class) >> space? >> sequence.as(:fonts_value)
  when :special_fonts then first_value.as(:bold_fonts)
  end
end

#hash_to_expression(arr) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/plurimath/asciimath/parse.rb', line 182

def hash_to_expression(arr)
  type = arr.first.class
  @@expression ||= arr.reduce do |expression, expr_string|
    expression = dynamic_parser_rules(expression) if expression.is_a?(type)
    expression | dynamic_parser_rules(expr_string)
  end
end

#read_textObject



175
176
177
178
179
180
# File 'lib/plurimath/asciimath/parse.rb', line 175

def read_text
  dynamic do |_sour, context|
    rparen = Constants::PARENTHESIS[context.captures[:paren].to_sym]
    match("[^#{rparen}]").repeat
  end
end

#unary_functions(first_value) ⇒ Object



200
201
202
203
204
205
206
207
208
# File 'lib/plurimath/asciimath/parse.rb', line 200

def unary_functions(first_value)
  if ["'underbrace'", "'ubrace'"].include?(first_value.to_s)
    (first_value.as(:unary_class) >> space? >> str("_").as(:symbol)).as(:unary) |
      (first_value.as(:unary_class) >> space? >> sequence.maybe).as(:unary)
  else
    (first_value.as(:unary_class).as(:power_base) >> space? >> power_base).as(:unary) |
      (first_value.as(:unary_class) >> space? >> sequence.maybe).as(:unary)
  end
end