// Copyright (c) 2009-2022 The MathJax Consortium
// SPDX-License-Identifier: Apache-2.0
// Source: ts/input/tex/base/BaseMethods.ts, MoveRoot.

package tex

import (
	
	
	
	

	
)

var rootIntegerPattern = regexp.MustCompile(`-?[0-9]+`)
var rootIntegerPrefix = regexp.MustCompile(`^[+-]?[0-9]+`)

// These handlers push ArrayItem subclasses, whose lexical environment does
// not inherit the enclosing index's inRoot flag. Equation and wrapper
// environments continue to inherit it normally.
func rootIndexArrayEnvironment( string) bool {
	, ,  := lookupMJSourceEntry(mjSourceEnvironmentMap, )
	if ! {
		return false
	}
	 := .Value
	if ,  := .(mjSourceList);  && len() != 0 {
		 = [0]
	}
	switch  {
	case "Array", "AlignedArray", "EqnArray", "AmsEqnArray", "AlignAt", "XalignAt", "FlalignArray",
		"Multline", "MtMultlined", "MtMatrix", "MtSmallMatrix", "Cases", "NumCases", "CD":
		return true
	}
	return false
}

func ( *parser) ( string) error {
	if !.inRoot {
		return texError("MisplacedMoveRoot", "\\%s can appear only within a root", )
	}
	 := .ensureStackGlobal()
	 := &.upRoot
	if  == "leftroot" {
		 = &.leftRoot
	}
	if * != "" {
		return texError("MultipleMoveRoot", "Multiple use of \\%s", )
	}
	, ,  := .readArgument(, false)
	if  != nil {
		return 
	}
	// The original validity check is intentionally unanchored, while
	// parseInt reads only a signed decimal prefix after JavaScript whitespace.
	if !rootIntegerPattern.MatchString() {
		return texError("IntegerArg", "The argument to \\%s must be an integer", )
	}
	 := math.NaN()
	if  := rootIntegerPrefix.FindString(strings.TrimLeftFunc(, internalTextSpace));  != "" {
		, _ = strconv.ParseFloat(, 64)
	}
	 := jscompat.NumberString(/15) + "em"
	if !strings.HasPrefix(, "-") {
		 = "+" + 
	}
	* = 
	return nil
}