Class: Ritex::Parser

Inherits:
Racc::Parser
  • Object
show all
Defined in:
lib/ritex.rb,
lib/ritex/parser.rb

Overview

The parser for itex and the main entry point for Ritex. This class is partially defined here and partially generated by Racc from lib/parser.y.

Create the parser with #new. Parse strings with #parse. That’s all there is to it.

Constant Summary collapse

FORMATS =
[:mathml]
Racc_arg =
[
racc_action_table,
racc_action_check,
racc_action_default,
racc_action_pointer,
racc_goto_table,
racc_goto_check,
racc_goto_default,
racc_goto_pointer,
racc_nt_base,
racc_reduce_table,
racc_token_table,
racc_shift_n,
racc_reduce_n,
racc_use_result_var ]
Racc_token_to_s_table =
[
"$end",
"error",
"NUMBER",
"VAR",
"SYMBOL",
"FUNC0",
"FUNC1",
"FUNC2",
"FUNC3",
"MACRO0",
"MACRO1",
"MACRO2",
"MACRO3",
"ENV",
"DOUBLEBACK",
"DEFINE",
"LEFT",
"RIGHT",
"ARRAY",
"ARRAYOPTS",
"ROWOPTS",
"CELLOPTS",
"COLALIGN",
"ROWALIGN",
"ALIGN",
"PADDING",
"EQUALCOLS",
"EQUALROWS",
"ROWLINES",
"COLLINES",
"FRAME",
"ROWSPAN",
"COLSPAN",
"SPACE",
"OPERATOR",
"\"-\"",
"\"^\"",
"\"_\"",
"\"{\"",
"\"}\"",
"\".\"",
"\"[\"",
"\"]\"",
"\"&\"",
"\",\"",
"\";\"",
"\":\"",
"\"<\"",
"\">\"",
"\"=\"",
"\"#\"",
"\"!\"",
"\"+\"",
"\"*\"",
"\"/\"",
"\"(\"",
"\")\"",
"\"|\"",
"$start",
"exp",
"atom",
"nontoken",
"subsup",
"symbol",
"func",
"macro",
"arg",
"array",
"macrodef",
"delim",
"token",
"string",
"stringel",
"escapeme",
"rawtoken",
"rawdelimtoken",
"symspec",
"arrayopts",
"ainnards",
"arrayoptsinnards",
"arow",
"arow1",
"acell",
"cellopts",
"celloptsinnards" ]
Racc_debug_parser =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format = :mathml) ⇒ Parser

format is the desired output format and must be in the FORMATS list. Right now that’s just :mathml.



50
51
52
53
54
55
# File 'lib/ritex.rb', line 50

def initialize format = :mathml
  self.format = format
  @macros = {}
  Ritex.global_parser = self # lame
  @merror = false
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



76
77
78
# File 'lib/ritex.rb', line 76

def format
  @format
end

#merrorObject

If true, Ritex will output a <merror>…</merror> message in the MathML if an unknown entity is encountered. If false (the default), Ritex will throw a Ritex::Error.



46
47
48
# File 'lib/ritex.rb', line 46

def merror
  @merror
end

Instance Method Details

#_reduce_none(val, _values, result) ⇒ Object



1181
1182
1183
# File 'lib/ritex/parser.rb', line 1181

def _reduce_none(val, _values, result)
  val[0]
end

#envsObject

:nodoc:



132
133
134
135
136
137
# File 'lib/ritex.rb', line 132

def envs #:nodoc:
  case @format
  when :mathml
    MathML::ENVS
  end
end

#flush_macrosObject

Delete all macros



83
# File 'lib/ritex.rb', line 83

def flush_macros; @macros = {}; end

#funcsObject

:nodoc:



125
126
127
128
129
130
# File 'lib/ritex.rb', line 125

def funcs #:nodoc:
  case @format
  when :mathml
    MathML::FUNCTIONS
  end
end

#lookup(sym) ⇒ Object

:nodoc:



103
104
105
106
107
108
109
# File 'lib/ritex.rb', line 103

def lookup sym #:nodoc:
  case @format
  when :mathml
    return error("unknown entity #{sym.inspect}") unless MathML::ENTITIES.member? sym
    MathML::ENTITIES[sym]
  end
end

#macrosObject

:nodoc:



139
140
141
# File 'lib/ritex.rb', line 139

def macros #:nodoc:
  @macros
end

#markup(what, tag, opts = []) ⇒ Object

:nodoc:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ritex.rb', line 85

def markup what, tag, opts=[] #:nodoc:
  case @format
  when :mathml
    tag, opts = case tag
      when String
        [tag, opts]
      when Symbol
        a, b = MathML::MARKUP[tag]
        [a, [b, opts].flatten.compact.join(" ")]
      end
    unless opts.empty?
      "<#{tag} #{opts}>#{what}</#{tag}>"
    else
      "<#{tag}>#{what}</#{tag}>"
    end
  end
end

#op(o, opts = []) ⇒ Object



118
119
120
121
122
123
# File 'lib/ritex.rb', line 118

def op o, opts=[]
  case @format
  when :mathml
    markup(token(o), "mo", opts)
  end
end

#op_symbolsObject

:nodoc:



143
144
145
146
147
148
# File 'lib/ritex.rb', line 143

def op_symbols #:nodoc:
  case @format
  when :mathml
    MathML::OPERATORS.merge(MathML::UNARY_OPERATORS).merge(MathML::MATH_FUNCTIONS)
  end
end

#parse(s, wrap = true, inline = true) ⇒ Object

Parse a string. Returns the MathML output in string form. Note that macro definitios are cumulative and persistent across calls to #parse. If you don’t want this behavior, you must explicitly call #flush_macros after every #parse call.

wrap denotes whether you want the output wrapped in the top-level XML math tag. Unless you’re generating these tags yourself, you want this.

inline denotes whether you want inline markup versus block or “display” markup. For mathml output this only has an effect if wrap is true.



69
70
71
72
73
74
# File 'lib/ritex.rb', line 69

def parse s, wrap = true, inline = true
  @lex = Lexer.new(self, s)
  r = yyparse @lex, :lex
  r = markup r, (inline ? :math : :displaymath) if wrap
  r
end

#token(o) ⇒ Object

:nodoc:



111
112
113
114
115
116
# File 'lib/ritex.rb', line 111

def token o #:nodoc:
  case @format
  when :mathml
    MathML::TOKENS[o] || o
  end
end