Class: MathML::LaTeX::Macro
- Inherits:
-
Object
- Object
- MathML::LaTeX::Macro
show all
- Defined in:
- lib/math_ml.rb
Defined Under Namespace
Classes: Command, Environment
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Macro
Returns a new instance of Macro.
438
439
440
441
|
# File 'lib/math_ml.rb', line 438
def initialize
@commands = Hash.new
@environments = Hash.new
end
|
Instance Method Details
#check_parameter_numbers(src, opt, whole) ⇒ Object
481
482
483
484
485
486
487
488
489
490
491
492
|
# File 'lib/math_ml.rb', line 481
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
553
554
555
|
# File 'lib/math_ml.rb', line 553
def commands(com)
@commands[com]
end
|
#environments(env) ⇒ Object
569
570
571
|
# File 'lib/math_ml.rb', line 569
def environments(env)
@environments[env]
end
|
#expand_command(com, params, opt = nil) ⇒ Object
557
558
559
560
561
562
563
564
565
566
567
|
# File 'lib/math_ml.rb', line 557
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.new("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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
|
# File 'lib/math_ml.rb', line 573
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.new("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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
# File 'lib/math_ml.rb', line 450
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
443
444
445
446
447
448
|
# File 'lib/math_ml.rb', line 443
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
# File 'lib/math_ml.rb', line 494
def parse_newcommand
case
when @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?
when @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
case
when @scanner.scan_block
body = @scanner[1]
when @scanner.scan_command
body = @scanner.matched
else
body = @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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
# File 'lib/math_ml.rb', line 528
def parse_newenvironment
case
when @scanner.scan_block
env = @scanner[1]
when @scanner.scan_command
raise ParseError.new
when @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
472
473
474
475
476
477
478
479
|
# File 'lib/math_ml.rb', line 472
def scan_num_of_parameter
if @scanner.scan_option
raise parse_error("Need positive number.", @scanner[1]+"]") unless @scanner[1]=~/\A#{RE::SPACE}*\d+#{RE::SPACE}*\z/
@scanner[1].to_i
else
0
end
end
|