import { Expression , LatexSyntax } from '@cortex-js/compute-engine' ; import { LatexDictionary , ParserFunction , Scanner , SerializerFunction } from '@cortex-js/compute-engine/dist/types/latex-syntax/public' ; const MISSING = 'Missing' ; /**
 * The dictionary has to be customized a bit for QuantumSheet
 * Things include, but are not limited to
 * `==` meaning equality
 * `=` being used to (numerically) evaluate something
 * `->` being used to symbolically evaluate something
 */ export const dictionary = LatexSyntax . getDictionary ( ) as LatexDictionary < any > ; dictionary . find ( ( v ) => v . name === 'EqualEqual' ) ! . precedence = 265 ; dictionary . find ( ( v ) => v . name === 'Equal' ) ! . associativity = 'left' ; export const EVALUATE = 'Evaluate' ; dictionary . push ( { precedence : 260 , name : EVALUATE , optionalLatexArg : 1 , requiredLatexArg : 1 , associativity : 'left' , trigger : { infix : '\\xrightarrow' } , serialize : < SerializerFunction < number > > function ( emitter , expr ) { if ( ! Array . isArray ( expr ) ) throw new Error ( 'Expect array expression' ) ; return ( emitter . wrap ( expr [ 1 ] , 260 ) + `\\xrightarrow{${ Array . isArray ( expr [ 2 ] ) && expr [ 2 ] [ 0 ] == MISSING ? '\\placeholder{}' : expr [ 2 ] }}` + emitter . wrap ( expr [ 3 ] , 260 ) ) ; } , parse : < ParserFunction < number > > function ( lhs , scanner , minPrec ) { if ( 260 < minPrec ) return [ lhs , null ] ; if ( ! scanner . match ( '\\xrightarrow' ) ) return [ lhs , null ] ; scanner . matchOptionalLatexArgument ( ) ; let solveArgument = '' ; scanner . skipSpace ( ) ; if ( scanner . match ( '<{>' ) ) { let level = 1 ; while ( ! scanner . atEnd && level !== 0 ) { if ( scanner . match ( '<{>' ) ) { level += 1 ; } else if ( scanner . match ( '<}>' ) ) { level -= 1 ; } else if ( scanner . match ( '<space>' ) ) { solveArgument += ' ' ; } else { solveArgument += scanner . next ( ) ; } } } if ( solveArgument . startsWith ( '\\placeholder' ) ) { solveArgument = [ MISSING , solveArgument . replace ( /^\\placeholder/ , '' ) ] ; } const rhs = scanner . matchExpression ( 260 ) ; return [ null , [ 'Evaluate' , lhs , solveArgument , rhs ] ] ; } , } ) ; dictionary . push ( { precedence : 260 , name : 'Text' , requiredLatexArg : 1 , associativity : 'non' , trigger : { symbol : '\\text' } , serialize : function ( emitter , expr ) { if ( ! Array . isArray ( expr ) ) throw new Error ( 'Expect array expression' ) ; return ` \\text{${ expr [ 1 ] . str }} ` ; } , parse : < ParserFunction < number > > function ( lhs , scanner , minPrec ) { if ( 260 < minPrec ) return [ lhs , null ] ; if ( ! scanner . match ( '\\text' ) ) return [ lhs , null ] ; let text = '' ; scanner . skipSpace ( ) ; if ( scanner . match ( '<{>' ) ) { let level = 1 ; while ( ! scanner . atEnd && level !== 0 ) { if ( scanner . match ( '<{>' ) ) { level += 1 ; } else if ( scanner . match ( '<}>' ) ) { level -= 1 ; } else if ( scanner . match ( '<space>' ) ) { text += ' ' ; } else { text += scanner . next ( ) ; } } } return [ null , [ 'Text' , { str : text } ] ] ; } , } ) ;