import { ErrorListener , Style , ParserErrorCode , ParseMode , } from '../public/core' ; import type { Box } from './box' ; import type { Token } from './tokenizer' ; import { ArgumentType } from './parser' ; import type { GroupAtom } from '../core-atoms/group' ; import { Atom , ToLatexOptions } from './atom' ; import { NormalizedMacroDictionary } from '../core-definitions/definitions-utils' ; export interface ParseTokensOptions { macros : NormalizedMacroDictionary ; smartFence : boolean ; style : Style ; args : ( arg : string ) => string ; parse : ( mode : ArgumentType , tokens : Token [ ] , options : ParseTokensOptions ) => [ Atom [ ] , Token [ ] ] ; } export class Mode { static _registry : Record < string , Mode > = { } ; constructor ( name ) { Mode . _registry [ name ] = this ; } static createAtom ( mode , command , style ) { return Mode . _registry [ mode ] . createAtom ( command , style ) ; } static parseTokens ( mode , tokens , onError , options ) { return Mode . _registry [ mode ] . parseTokens ( tokens , onError , options ) ; } static serialize ( run , options ) { console . assert ( run . length > 0 ) ; const mode = Mode . _registry [ run [ 0 ] . mode ] ; return mode . serialize ( run , options ) ; } static applyStyle ( mode , box , style ) { return Mode . _registry [ mode ] . applyStyle ( box , style ) ; } createAtom ( _command , _style ) { return null ; } parseTokens ( _tokens , _onError , _options ) { return null ; } serialize ( _run , _options ) { return '' ; } applyStyle ( _box , _style ) { return '' ; } } export function getModeRuns ( atoms ) { const result = [ ] ; let run = [ ] ; let currentMode = 'NONE' ; for ( const atom of atoms ) { if ( atom . type !== 'first' ) { if ( atom . mode !== currentMode ) { if ( run . length > 0 ) result . push ( run ) ; run = [ atom ] ; currentMode = atom . mode ; } else { run . push ( atom ) ; } } } if ( run . length > 0 ) result . push ( run ) ; return result ; } export function getPropertyRuns ( atoms , property ) { const result = [ ] ; let run = [ ] ; let currentValue = undefined ; for ( const atom of atoms ) { if ( atom . type !== 'first' ) { let value ; if ( property === 'variant' ) { value = atom . style . variant ; if ( atom . style . variantStyle && atom . style . variantStyle !== 'up' ) { value += '-' + atom . style . variantStyle ; } } else if ( property === 'cssClass' ) { if ( atom . type === 'group' ) { value = ( atom as GroupAtom ) . customClass ; } } else { value = atom . style [ property ] ; } if ( value === currentValue ) { run . push ( atom ) ; } else { if ( run . length > 0 ) result . push ( run ) ; run = [ atom ] ; currentValue = value ; } } } if ( run . length > 0 ) result . push ( run ) ; return result ; }