Class: MathML::LaTeX::Macro

Inherits:
Object
  • Object
show all
Defined in:
lib/math_ml/latex.rb

Defined Under Namespace

Classes: Command, Environment

Instance Method Summary collapse

Constructor Details

#initializeMacro

Returns a new instance of Macro.



185
186
187
188
# File 'lib/math_ml/latex.rb', line 185

def initialize
	@commands = Hash.new
	@environments = Hash.new
end

Instance Method Details

#check_parameter_numbers(src, opt, whole) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/math_ml/latex.rb', line 228

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



300
301
302
# File 'lib/math_ml/latex.rb', line 300

def commands(com)
	@commands[com]
end

#environments(env) ⇒ Object



316
317
318
# File 'lib/math_ml/latex.rb', line 316

def environments(env)
	@environments[env]
end

#expand_command(com, params, opt = nil) ⇒ Object

Raises:



304
305
306
307
308
309
310
311
312
313
314
# File 'lib/math_ml/latex.rb', line 304

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

Raises:



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/math_ml/latex.rb', line 320

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/math_ml/latex.rb', line 197

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



190
191
192
193
194
195
# File 'lib/math_ml/latex.rb', line 190

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_newcommandObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/math_ml/latex.rb', line 241

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_newenvironmentObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/math_ml/latex.rb', line 275

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_parameterObject



219
220
221
222
223
224
225
226
# File 'lib/math_ml/latex.rb', line 219

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