Module: MathML::LaTeX::BuiltinGroups

Included in:
Parser
Defined in:
lib/math_ml/latex.rb,
lib/math_ml/latex.rb

Defined Under Namespace

Classes: CircularReferenceEnvironment

Instance Method Summary collapse

Instance Method Details

#add_environment(*a) ⇒ Object



838
839
840
841
842
843
844
845
# File 'lib/math_ml/latex.rb', line 838

def add_environment(*a)
	@environments = Hash.new unless @environments
	if a.size==1 && Hash===a[0]
		@environments.merge!(hash)
	else
		a.each{|i| @environments[i] = false}
	end
end

#grp_beginObject

Raises:



847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
# File 'lib/math_ml/latex.rb', line 847

def grp_begin
	matched = @scanner.matched
	begin_pos = @scanner.pos-matched.size
	en = @scanner.scan_block ? @scanner[1] : @scanner.scan_any
	raise ParseError.new('Environment name not exist.') unless en

	macro = @macro.environments(en)
	if macro
		begin
			flg = @expanded_environment.include?(en)
			@expanded_environment.push(en)
			raise CircularReferenceEnvironment if flg

			pos = @scanner.pos
			option = (macro.option && @scanner.scan_option) ? @scanner[1] : nil
			params = Array.new
			(1..macro.num).each do
				params << (@scanner.scan_block ? @scanner[1] : @scanner.scan_any)
				raise ParseError.new("Need more parameter.") unless params.last
			end
			body = ""
			grpnest = 0
			until @scanner.peek_command=="end" && grpnest==0
				if @scanner.eos?
					@scanner.pos = pos
					raise ParseError.new('Matching \end not exist.')
				end
				com = @scanner.peek_command
				grpnest += 1 if @group_begins.has_key?(com)
				grpnest -=1 if @group_ends.has_key?(com) && @group_begins[com]
				raise ParseError.new("Syntax error.") if grpnest<0

				body << @scanner.scan_any(true)
			end
			@scanner.scan_command
			raise ParseError.new("Environment mismatched.", @scanner.matched) unless en==(@scanner.scan_block ? @scanner[1] : @scanner.scan_any)
			begin
				return parse_into(@macro.expand_environment(en, body, params, option), Array.new)
			rescue CircularReferenceEnvironment
				if @expanded_environment.size>1
					raise
				else
					@scanner.pos = begin_pos
					raise ParseError.new("Circular reference.")
				end
			rescue ParseError => e
				if @expanded_environment.size>1
					raise
				else
					@scanner.pos = begin_pos
					raise ParseError.new(%[Error in macro(#{e.message} "#{e.rest.strip}").])
				end
			end
		ensure
			@expanded_environment.pop
		end
	end

	raise ParseError.new("Undefined environment.") unless @environments.has_key?(en)
	e = @environments[en]
	e = en unless e # default method name
	__send__("env_#{e.to_s}")
end

#grp_left_etcObject

Raises:



911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
# File 'lib/math_ml/latex.rb', line 911

def grp_left_etc
	right =
		case @scanner[1]
		when "left"
			"right"
		when "bigg"
			"bigg"
		end

	f = Fenced.new
	p = @scanner.pos
	o = @scanner.scan_any
	raise ParseError.new('Need brace here.') unless o && (o=~RE::BRACES || @delimiters.include?(o[RE::COMMANDS, 1]))
	f.open = (o=~RE::BRACES ? o : parse_symbol_command(o[RE::COMMANDS, 1], true))
	f << push_container(Row.new) do |r|
		until @scanner.peek_command==right
			if @scanner.eos?
				@scanner.pos = p
				raise ParseError.new('Brace not closed.')
			end
			r << parse_to_element(true)
		end
	end
	@scanner.scan_command # skip right
	c = @scanner.scan_any
	raise ParseError.new('Need brace here.') unless c=~RE::BRACES || @delimiters.include?(c[RE::COMMANDS, 1])
	f.close = (c=~RE::BRACES ? c : parse_symbol_command(c[RE::COMMANDS, 1], true))
	f
end

#initializeObject



829
830
831
832
833
834
835
836
# File 'lib/math_ml/latex.rb', line 829

def initialize
	add_group("begin", "end")
	add_group("left", "right", :left_etc)
	add_group("bigg", "bigg", :left_etc)
	@environments = Hash.new

	super
end