Class: Plurimath::Mathml::Parse

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

Instance Method Summary collapse

Instance Method Details

#array_to_expression(array, name = nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/plurimath/mathml/parse.rb', line 31

def array_to_expression(array, name = nil)
  initial_type = array.first.class
  array.reduce do |expr, tag|
    expr = str_to_expression(expr, name) if expr.is_a?(initial_type)
    expr | str_to_expression(tag, name)
  end
end

#attributesObject



53
54
55
56
# File 'lib/plurimath/mathml/parse.rb', line 53

def attributes
  (match["a-zA-Z"].repeat.as(:name) >>
    str("=") >> quoted_string).repeat
end

#parse_tag(opts) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/plurimath/mathml/parse.rb', line 45

def parse_tag(opts)
  tag = str("<")
  tag = tag >> str("/") if opts == :close
  tag = tag >> array_to_expression(Constants::TAGS, opts)
  tag = tag >> attributes.as(:attributes) if opts == :open
  tag >> str(">")
end

#parse_text_tagObject



63
64
65
# File 'lib/plurimath/mathml/parse.rb', line 63

def parse_text_tag
  str("<mtext>") >> match("[^<]").repeat.as(:quoted_text) >> str("</mtext>")
end

#quoted_stringObject



58
59
60
61
# File 'lib/plurimath/mathml/parse.rb', line 58

def quoted_string
  (str('"') >> match("[^\"]").repeat.as(:value) >> str('"')) |
    (str("'") >> match("[^\']").repeat.as(:value) >> str("'"))
end

#str_to_expression(string, name) ⇒ Object



39
40
41
42
43
# File 'lib/plurimath/mathml/parse.rb', line 39

def str_to_expression(string, name)
  return str(string) if name.nil?

  str(string).as(name)
end