@@ -55,6 +55,7 @@ struct MacroExpansion {
5555pub struct MacroExpander < ' a > {
5656 pub lexer : Lexer < ' a > ,
5757 pub mode : Mode ,
58+ parser_recursion_depth : usize ,
5859 stack : Vec < Token > ,
5960 macros : MacroNamespace ,
6061 expansion_count : usize ,
@@ -164,6 +165,7 @@ impl<'a> MacroExpander<'a> {
164165 let mut me = Self {
165166 lexer : Lexer :: new ( input) ,
166167 mode,
168+ parser_recursion_depth : 0 ,
167169 stack : Vec :: new ( ) ,
168170 macros : MacroNamespace :: new ( ) ,
169171 expansion_count : 0 ,
@@ -173,6 +175,10 @@ impl<'a> MacroExpander<'a> {
173175 me
174176 }
175177
178+ pub fn set_parser_recursion_depth ( & mut self , depth : usize ) {
179+ self . parser_recursion_depth = depth;
180+ }
181+
176182 fn load_builtins ( & mut self ) {
177183 let builtins: & [ ( & str , & str ) ] = & [
178184 // ── Grouping ──
@@ -898,8 +904,12 @@ impl<'a> MacroExpander<'a> {
898904 MacroDefinition :: Function ( |me : & mut MacroExpander | -> ParseResult < Vec < Token > > {
899905 let args = me. consume_args ( 1 ) ?;
900906 let s = crate :: mhchem:: mhchem_arg_tokens_to_string ( & args[ 0 ] ) ;
901- let tex = crate :: mhchem:: chem_parse_str ( & s, "ce" )
902- . map_err ( |e| ParseError :: msg ( format ! ( "\\ ce: {e}" ) ) ) ?;
907+ let tex = crate :: mhchem:: chem_parse_str_with_parser_depth (
908+ & s,
909+ "ce" ,
910+ me. parser_recursion_depth ,
911+ )
912+ . map_err ( |e| ParseError :: msg ( format ! ( "\\ ce: {e}" ) ) ) ?;
903913 Ok ( lex_string_to_stack_tokens ( & tex) )
904914 } ) ,
905915 ) ;
@@ -908,8 +918,12 @@ impl<'a> MacroExpander<'a> {
908918 MacroDefinition :: Function ( |me : & mut MacroExpander | -> ParseResult < Vec < Token > > {
909919 let args = me. consume_args ( 1 ) ?;
910920 let s = crate :: mhchem:: mhchem_arg_tokens_to_string ( & args[ 0 ] ) ;
911- let tex = crate :: mhchem:: chem_parse_str ( & s, "pu" )
912- . map_err ( |e| ParseError :: msg ( format ! ( "\\ pu: {e}" ) ) ) ?;
921+ let tex = crate :: mhchem:: chem_parse_str_with_parser_depth (
922+ & s,
923+ "pu" ,
924+ me. parser_recursion_depth ,
925+ )
926+ . map_err ( |e| ParseError :: msg ( format ! ( "\\ pu: {e}" ) ) ) ?;
913927 Ok ( lex_string_to_stack_tokens ( & tex) )
914928 } ) ,
915929 ) ;
0 commit comments