# class MathML::LaTeX::Macro

### Public Class Methods

new() click to toggle source

        # File lib/math_ml/latex.rb
    190 def initialize
    191   @commands = {}
    192   @environments = {}
    193 end

### Public Instance Methods

check_parameter_numbers(src, opt, whole) click to toggle source

        # File lib/math_ml/latex.rb
    237 def check_parameter_numbers(src, opt, whole)
    238   s = Scanner.new(src)
    239   until s.eos?
    240     case
    241     when s.scan(/#{MBEC}*?\#(\d+|.)/)
    242       raise parse_error('Need positive number.') unless s[1] =~ /\d+/
    243       raise parse_error("Parameter \# too large.", s[1] + s.rest, whole) if s[1].to_i > opt
    244     else
    245       return nil
    246     end
    247   end
    248 end

commands(com) click to toggle source

        # File lib/math_ml/latex.rb
    309 def commands(com)
    310   @commands[com]
    311 end

environments(env) click to toggle source

        # File lib/math_ml/latex.rb
    326 def environments(env)
    327   @environments[env]
    328 end

expand_command(com, params, opt = nil) click to toggle source

        # File lib/math_ml/latex.rb
    313 def expand_command(com, params, opt = nil)
    314   return nil unless @commands.has_key?(com)
    315
    316   c = @commands[com]
    317   opt = c.option if c.option && !opt
    318   params.unshift(opt) if c.option
    319   raise ParseError, 'Need more parameter.' if params.size < c.num
    320
    321   c.body.gsub(/(#{MBEC}*?)\#(\d+)/) do
    322     $1.to_s << params[$2.to_i - 1]
    323   end
    324 end

expand_environment(env, body, params, opt = nil) click to toggle source

        # File lib/math_ml/latex.rb
    330 def expand_environment(env, body, params, opt = nil)
    331   return nil unless @environments.has_key?(env)
    332
    333   e = @environments[env]
    334   opt = e.option if e.option && !opt
    335   params.unshift(opt) if e.option
    336   raise ParseError, 'Need more parameter.' if params.size < e.num
    337
    338   bg = e.beginning.gsub(/(#{MBEC}*?)\#(\d+)/) do
    339     $1.to_s << params[$2.to_i - 1]
    340   end
    341
    342   en = e.ending.gsub(/(#{MBEC}*?)\#(\d+)/) do
    343     $1.to_s << params[$2.to_i - 1]
    344   end
    345
    346   " #{bg} #{body} #{en} "
    347 end

parse(src) click to toggle source

        # File lib/math_ml/latex.rb
    202 def parse(src)
    203   @scanner = Scanner.new(src)
    204   until @scanner.eos?
    205     unless @scanner.scan_command
    206       @scanner.scan_space
    207       raise parse_error('Syntax error.')
    208     end
    209     case @scanner[1]
    210     when 'newcommand'
    211       parse_newcommand
    212     when 'newenvironment'
    213       parse_newenvironment
    214     else
    215       raise parse_error('Syntax error.', @scanner.matched)
    216     end
    217   end
    218 rescue BlockNotClosed => e
    219   raise parse_error('Block not closed.')
    220 rescue OptionNotClosed => e
    221   raise parse_error('Option not closed.')
    222 end

parse_error(message, rest = '', whole = nil) click to toggle source

        # File lib/math_ml/latex.rb
    195 def parse_error(message, rest = '', whole = nil)
    196   rest = whole[/\A.*?(#{Regexp.escape(rest)}.*\z)/, 1] if whole
    197   rest << @scanner.rest
    198   done = @scanner.string[0, @scanner.string.size - rest.size]
    199   ParseError.new(message, rest, done)
    200 end

parse_newcommand() click to toggle source

        # File lib/math_ml/latex.rb
    250 def parse_newcommand
    251   if @scanner.scan_block
    252     s = Scanner.new(@scanner[1])
    253     raise parse_error('Need newcommand.', s.rest + '}') unless s.scan_command
    254
    255     com = s[1]
    256     raise parse_error('Syntax error.', s.rest + '}') unless s.eos?
    257   elsif @scanner.scan_command
    258     s = Scanner.new(@scanner[1])
    259     com = s.scan_command
    260   else
    261     raise parse_error('Need newcommand.')
    262   end
    263
    264   optnum = scan_num_of_parameter
    265   opt = @scanner.scan_option ? @scanner[1] : nil
    266
    267   body = if @scanner.scan_block
    268            @scanner[1]
    269          elsif @scanner.scan_command
    270            @scanner.matched
    271          else
    272            @scanner.scan(/./)
    273          end
    274
    275   raise parse_error('Need parameter.') unless body
    276
    277   check_parameter_numbers(body, optnum, @scanner.matched)
    278
    279   optnum -= 1 if opt
    280   @commands[com] = Command.new(optnum, body, opt)
    281 end

parse_newenvironment() click to toggle source

        # File lib/math_ml/latex.rb
    283 def parse_newenvironment
    284   if @scanner.scan_block
    285     env = @scanner[1]
    286   elsif @scanner.scan_command
    287     raise ParseError
    288   elsif @scanner.scan(/./)
    289     env = @scanner.matched
    290   end
    291   raise parse_error('Syntax error.', env[/\A.*?(\\.*\z)/, 1], @scanner.matched) if env =~ /\\/
    292
    293   optnum = scan_num_of_parameter
    294   opt = @scanner.scan_option ? @scanner[1] : nil
    295
    296   b = @scanner.scan_block ? @scanner[1] : @scanner.scan_any
    297   raise parse_error('Need begin block.') unless b
    298
    299   check_parameter_numbers(b, optnum, @scanner.matched)
    300   e = @scanner.scan_block ? @scanner[1] : @scanner.scan_any
    301   raise parse_error('Need end block.') unless e
    302
    303   check_parameter_numbers(e, optnum, @scanner.matched)
    304
    305   optnum -= 1 if opt
    306   @environments[env] = Environment.new(optnum, b, e, opt)
    307 end

scan_num_of_parameter() click to toggle source

        # File lib/math_ml/latex.rb
    224 def scan_num_of_parameter
    225   if @scanner.scan_option
    226     unless @scanner[1] =~ /\A#{RE::SPACE}*\d+#{RE::SPACE}*\z/
    227       raise parse_error('Need positive number.',
    228                         @scanner[1] + ']')
    229     end
    230
    231     @scanner[1].to_i
    232   else
    233     0
    234   end
    235 end