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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
|
# File 'lib/math_ml/latex.rb', line 864
def grp_begin
matched = @scanner.matched
begin_pos = @scanner.pos - matched.size
en = @scanner.scan_block ? @scanner[1] : @scanner.scan_any
raise ParseError, '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 = []
(1..macro.num).each do
params << (@scanner.scan_block ? @scanner[1] : @scanner.scan_any)
raise ParseError, '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, '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, 'Syntax error.' if grpnest < 0
body << @scanner.scan_any(true)
end
@scanner.scan_command
unless en == (@scanner.scan_block ? @scanner[1] : @scanner.scan_any)
raise ParseError.new('Environment mismatched.',
@scanner.matched)
end
begin
return parse_into(@macro.expand_environment(en, body, params, option), [])
rescue CircularReferenceEnvironment
if @expanded_environment.size > 1
raise
else
@scanner.pos = begin_pos
raise ParseError, 'Circular reference.'
end
rescue ParseError => e
if @expanded_environment.size > 1
raise
else
@scanner.pos = begin_pos
raise ParseError, %[Error in macro(#{e.message} "#{e.rest.strip}").]
end
end
ensure
@expanded_environment.pop
end
end
raise ParseError, 'Undefined environment.' unless @environments.has_key?(en)
e = @environments[en]
e ||= en __send__("env_#{e}")
end
|