// Copyright (c) 2009-2022 The MathJax Consortium
// SPDX-License-Identifier: Apache-2.0
// Source: ts/input/tex/{TexParser,ParseUtil}.ts, ams/AmsMethods.ts.

package tex

import (
	

	
)

// Genfrac needs GetDelimiterArg's raw key, not the generic converter's glyph.
// A nil key is absent; a present dot still constructs a zero-content fence.
func ( *parser) ( string) (*string, error) {
	, ,  := .readArgument(, false)
	if  != nil {
		return nil, 
	}
	 := strings.TrimFunc(, internalTextSpace)
	if strings.HasSuffix(, "\\") && strings.HasSuffix(, " ") {
		 += " "
	}
	if  == "" {
		return nil, nil
	}
	if ,  := lookupDelimiter(); ! {
		return nil, texError("MissingOrUnrecognizedDelim", "Missing or unrecognized delimiter for \\%s", )
	}
	return &, nil
}

// This caller-specific fixedFence path leaves binomial and infix helpers intact.
func ( *parser) ( *string,  *mml.Node,  *string) (*mml.Node, error) {
	var ,  any
	if  != nil {
		 = *
	}
	if  != nil {
		 = *
	}
	 := forcedRow(nil, false)
	.SetProperty("open", )
	.SetProperty("close", )
	.SetProperty("texClass", mml.TeXClassOrd)
	.TeXClass = mml.TeXClassOrd
	if  != nil {
		,  := .amsGenfracPalette(*, "l")
		if  != nil {
			return nil, 
		}
		.AppendChild()
	}
	.AppendChild()
	if  != nil {
		,  := .amsGenfracPalette(*, "r")
		if  != nil {
			return nil, 
		}
		.AppendChild()
	}
	return , nil
}

func ( *parser) (,  string) (*mml.Node, error) {
	if  == "{" ||  == "}" {
		 = "\\" + 
	}
	 := "{\\bigg" +  + " " +  + "}"
	 := "{\\big" +  + " " +  + "}"
	 := "\\mathchoice" +  +  +  + 
	// mathPalette creates an empty-environment TexParser with the same
	// configuration. Preserve shared tables/factory and the caller's count.
	 := .state.macroCount
	.state.macroCount = 0
	defer func() { .state.macroCount =  }()
	 := &parser{source: , state: .state, vectorFactory: .vectorFactory,
		genfracPalette: true, derivativeChildren: .derivativeChildren}
	, ,  := .parseRow(0, false)
	if  != nil {
		return nil, 
	}
	 := row(, true)
	// The source's empty lexical environment must survive a deferred outer
	// font walk, including tokens that selected their default variant.
	.Walk(func( *mml.Node) bool {
		if .Flags.Token {
			.SetProperty(resolvedFontScope, true)
		}
		return true
	})
	return , nil
}

// Both Sqrt and Root create a fresh parseRoot index parser. Its counter is
// independent even when the surrounding expression is ordinary authored TeX.
func ( *parser) ( string) (*mml.Node, error) {
	 := .state.macroCount
	.state.macroCount = 0
	defer func() { .state.macroCount =  }()
	 := .inRoot
	.inRoot = true
	defer func() { .inRoot =  }()
	 := &parserStackGlobal{}
	,  := .parseStringWithStack(, )
	if  != nil {
		return nil, 
	}
	if .leftRoot == "" && .upRoot == "" {
		return , nil
	}
	 := node("mpadded", )
	if .leftRoot != "" {
		.Attributes.Set("width", .leftRoot)
	}
	if .upRoot != "" {
		.Attributes.Set("voffset", .upRoot)
		.Attributes.Set("height", .upRoot)
	}
	return , nil
}