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



153
154
155
156
157
158
159
# File 'lib/plurimath/asciimath/parse.rb', line 153

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



176
177
178
179
180
181
182
183
184
# File 'lib/plurimath/asciimath/parse.rb', line 176

def dynamic_parser_rules(expr)
  first_value = str(expr.first.to_s)
  case expr.last
  when :symbol then first_value.as(:symbol)
  when :unary_class then (first_value.as(:unary_class) >> space.maybe >> sequence.maybe).as(:unary)
  when :fonts then first_value.as(:fonts_class) >> space.maybe >> sequence.as(:fonts_value)
  when :special_fonts then first_value.as(:bold_fonts)
  end
end

#hash_to_expression(arr) ⇒ Object



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

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



161
162
163
164
165
166
# File 'lib/plurimath/asciimath/parse.rb', line 161

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