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



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

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



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

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



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

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



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

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



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

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