Class: MathML::LaTeX::Macro
- Inherits:
-
Object
- Object
- MathML::LaTeX::Macro
show all
- Defined in:
- lib/math_ml/latex.rb
Defined Under Namespace
Classes: Command, Environment
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Macro
Returns a new instance of Macro.
190
191
192
193
|
# File 'lib/math_ml/latex.rb', line 190
def initialize
@commands = {}
@environments = {}
end
|
Instance Method Details
#check_parameter_numbers(src, opt, whole) ⇒ Object
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/math_ml/latex.rb', line 237
def check_parameter_numbers(src, opt, whole)
s = Scanner.new(src)
until s.eos?
case
when s.scan(/#{MBEC}*?\#(\d+|.)/)
raise parse_error('Need positive number.') unless s[1] =~ /\d+/
raise parse_error("Parameter \# too large.", s[1] + s.rest, whole) if s[1].to_i > opt
else
return nil
end
end
end
|
#commands(com) ⇒ Object
309
310
311
|
# File 'lib/math_ml/latex.rb', line 309
def commands(com)
@commands[com]
end
|
#environments(env) ⇒ Object
326
327
328
|
# File 'lib/math_ml/latex.rb', line 326
def environments(env)
@environments[env]
end
|
#expand_command(com, params, opt = nil) ⇒ Object
313
314
315
316
317
318
319
320
321
322
323
324
|
# File 'lib/math_ml/latex.rb', line 313
def expand_command(com, params, opt = nil)
return nil unless @commands.has_key?(com)
c = @commands[com]
opt = c.option if c.option && !opt
params.unshift(opt) if c.option
raise ParseError, 'Need more parameter.' if params.size < c.num
c.body.gsub(/(#{MBEC}*?)\#(\d+)/) do
$1.to_s << params[$2.to_i - 1]
end
end
|
#expand_environment(env, body, params, opt = nil) ⇒ Object
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
# File 'lib/math_ml/latex.rb', line 330
def expand_environment(env, body, params, opt = nil)
return nil unless @environments.has_key?(env)
e = @environments[env]
opt = e.option if e.option && !opt
params.unshift(opt) if e.option
raise ParseError, 'Need more parameter.' if params.size < e.num
bg = e.beginning.gsub(/(#{MBEC}*?)\#(\d+)/) do
$1.to_s << params[$2.to_i - 1]
end
en = e.ending.gsub(/(#{MBEC}*?)\#(\d+)/) do
$1.to_s << params[$2.to_i - 1]
end
" #{bg} #{body} #{en} "
end
|
#parse(src) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/math_ml/latex.rb', line 202
def parse(src)
@scanner = Scanner.new(src)
until @scanner.eos?
unless @scanner.scan_command
@scanner.scan_space
raise parse_error('Syntax error.')
end
case @scanner[1]
when 'newcommand'
parse_newcommand
when 'newenvironment'
parse_newenvironment
else
raise parse_error('Syntax error.', @scanner.matched)
end
end
rescue BlockNotClosed => e
raise parse_error('Block not closed.')
rescue OptionNotClosed => e
raise parse_error('Option not closed.')
end
|
#parse_error(message, rest = '', whole = nil) ⇒ Object
195
196
197
198
199
200
|
# File 'lib/math_ml/latex.rb', line 195
def parse_error(message, rest = '', whole = nil)
rest = whole[/\A.*?(#{Regexp.escape(rest)}.*\z)/, 1] if whole
rest << @scanner.rest
done = @scanner.string[0, @scanner.string.size - rest.size]
ParseError.new(message, rest, done)
end
|
#parse_newcommand ⇒ Object
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
# File 'lib/math_ml/latex.rb', line 250
def parse_newcommand
if @scanner.scan_block
s = Scanner.new(@scanner[1])
raise parse_error('Need newcommand.', s.rest + '}') unless s.scan_command
com = s[1]
raise parse_error('Syntax error.', s.rest + '}') unless s.eos?
elsif @scanner.scan_command
s = Scanner.new(@scanner[1])
com = s.scan_command
else
raise parse_error('Need newcommand.')
end
optnum = scan_num_of_parameter
opt = @scanner.scan_option ? @scanner[1] : nil
body = if @scanner.scan_block
@scanner[1]
elsif @scanner.scan_command
@scanner.matched
else
@scanner.scan(/./)
end
raise parse_error('Need parameter.') unless body
check_parameter_numbers(body, optnum, @scanner.matched)
optnum -= 1 if opt
@commands[com] = Command.new(optnum, body, opt)
end
|
#parse_newenvironment ⇒ Object
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# File 'lib/math_ml/latex.rb', line 283
def parse_newenvironment
if @scanner.scan_block
env = @scanner[1]
elsif @scanner.scan_command
raise ParseError
elsif @scanner.scan(/./)
env = @scanner.matched
end
raise parse_error('Syntax error.', env[/\A.*?(\\.*\z)/, 1], @scanner.matched) if env =~ /\\/
optnum = scan_num_of_parameter
opt = @scanner.scan_option ? @scanner[1] : nil
b = @scanner.scan_block ? @scanner[1] : @scanner.scan_any
raise parse_error('Need begin block.') unless b
check_parameter_numbers(b, optnum, @scanner.matched)
e = @scanner.scan_block ? @scanner[1] : @scanner.scan_any
raise parse_error('Need end block.') unless e
check_parameter_numbers(e, optnum, @scanner.matched)
optnum -= 1 if opt
@environments[env] = Environment.new(optnum, b, e, opt)
end
|
#scan_num_of_parameter ⇒ Object
224
225
226
227
228
229
230
231
232
233
234
235
|
# File 'lib/math_ml/latex.rb', line 224
def scan_num_of_parameter
if @scanner.scan_option
unless @scanner[1] =~ /\A#{RE::SPACE}*\d+#{RE::SPACE}*\z/
raise parse_error('Need positive number.',
@scanner[1] + ']')
end
@scanner[1].to_i
else
0
end
end
|