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



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

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



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

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



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

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



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

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