katex-parser

    A MoonBit parser for KaTeX, lexing and parsing LaTeX math expressions with macro expansion

    katex
    latex
    math
    parser
    tex
    Download zip
    Author
    Version
    0.5.0
    License
    AGPL-3.0
    Last updated
    13 days ago
    Downloads
    108

    #KaTeX Parser

    A port of the KaTeX parser, lexing LaTeX math expressions into a typed AST with macro expansion support.

    EnvironmentHandler

    type EnvironmentHandler = (EnvironmentContext, Array[ParseNode], Array[ParseNode?]) -> ParseNode raise ParseFailure

    FunctionHandler

    type FunctionHandler = (FunctionContext, Array[ParseNode], Array[ParseNode?]) -> ParseNode raise ParseFailure

    StrictHandler

    type StrictHandler = (String, String, SourceLocation?) -> StrictResponse raise ParseFailure

    StrictWarningHandler

    type StrictWarningHandler = (String) -> Unit

    TrustHandler

    type TrustHandler = (TrustContext) -> Bool

    ParseFailure

    pub(all) suberror ParseFailure {
    UnexpectedCharacter(message~ : String, loc~ : SourceLocation?)
    ExpectedToken(expected~ : String, actual~ : Diagnostic)
    UndefinedControlSequence(name~ : String, loc~ : SourceLocation?)
    InvalidArgument(message~ : String, loc~ : SourceLocation?)
    DoubleSuperscript(loc~ : SourceLocation?)
    DoubleSubscript(loc~ : SourceLocation?)
    ExpectedGroupAfter(symbol~ : String, loc~ : SourceLocation?)
    FunctionNotAllowed(func_name~ : String, context~ : String, loc~ : SourceLocation?)
    MissingFunctionHandler(func_name~ : String, loc~ : SourceLocation?)
    TooManyExpansions(limit~ : Int)
    InternalInvariant(message~ : String)
    } derive(Eq, ToJson,
    Debug
    )

    ParseFailure::equal

    ParseFailure::not_equal

    fn ParseFailure::not_equal(x : ParseFailure, y : ParseFailure) -> Bool

    ParseFailure::to_json

    ArgType

    pub(all) enum ArgType {
    ColorArg
    SizeArg
    UrlArg
    RawArg
    OriginalArg
    HboxArg
    PrimitiveArg
    MathArg
    TextArg
    }

    ArrayColumn

    pub(all) enum ArrayColumn {
    AlignColumn(alignment~ : String, pre_gap~ : Double, post_gap~ : Double)
    SeparatorColumn(separator~ : String)
    } derive(Eq, ToJson,
    Debug
    )

    ArrayColumn::equal

    fn ArrayColumn::equal(ArrayColumn, ArrayColumn) -> Bool

    ArrayColumn::not_equal

    fn ArrayColumn::not_equal(x : ArrayColumn, y : ArrayColumn) -> Bool

    ArrayColumn::to_json

    fn ArrayColumn::to_json(ArrayColumn) -> Json

    ArrayEnvironmentOptions

    pub(all) struct ArrayEnvironmentOptions {
    columns : Array[ArrayColumn]?
    array_stretch : Double
    hskip_before_and_after : Bool
    cell_style : StyleLevel
    max_columns : Int?
    single_row : Bool
    auto_tag : Bool?
    leqno : Bool
    add_jot : Bool
    column_separation_type : ColumnSeparationType?
    }

    AtomFamily

    pub(all) enum AtomFamily {
    Mord
    Mop
    Mbin
    Mrel
    Mopen
    Mclose
    Mpunct
    Minner
    } derive(Eq, ToJson,
    Debug
    )

    AtomFamily::equal

    fn AtomFamily::equal(AtomFamily, AtomFamily) -> Bool

    AtomFamily::not_equal

    fn AtomFamily::not_equal(x : AtomFamily, y : AtomFamily) -> Bool

    AtomFamily::to_json

    fn AtomFamily::to_json(AtomFamily) -> Json

    ColumnSeparationType

    pub(all) enum ColumnSeparationType {
    AlignSeparation
    AlignAtSeparation
    GatherSeparation
    SmallSeparation
    CdSeparation
    } derive(Eq, ToJson,
    Debug
    )

    ColumnSeparationType::equal

    ColumnSeparationType::not_equal

    ColumnSeparationType::to_json

    Diagnostic

    pub(all) struct Diagnostic {
    text : String
    loc : SourceLocation?
    } derive(Eq, ToJson,
    Debug
    )

    Diagnostic::equal

    fn Diagnostic::equal(Diagnostic, Diagnostic) -> Bool

    Diagnostic::not_equal

    fn Diagnostic::not_equal(x : Diagnostic, y : Diagnostic) -> Bool

    Diagnostic::to_json

    fn Diagnostic::to_json(Diagnostic) -> Json

    EnvironmentContext

    pub(all) struct EnvironmentContext {
    mode : Mode
    display_mode : Bool
    leqno : Bool
    env_name : String
    parse_array : (ArrayEnvironmentOptions) -> ParseNode raise ParseFailure
    parse_matrix_alignment : () -> String? raise ParseFailure
    parse_cd : () -> ParseNode raise ParseFailure
    }

    EnvironmentRegistry

    pub(all) struct EnvironmentRegistry {
    entries : Map[String, EnvironmentSpec]
    }

    EnvironmentRegistry::get

    fn EnvironmentRegistry::get(self : EnvironmentRegistry, name : String) -> EnvironmentSpec?

    EnvironmentRegistry::make

    EnvironmentRegistry::register

    fn EnvironmentRegistry::register(self : EnvironmentRegistry, spec : EnvironmentSpec) -> Unit

    EnvironmentSpec

    pub(all) struct EnvironmentSpec {
    names : Array[String]
    num_args : Int
    num_optional_args : Int
    arg_types : Array[ArgType]
    handler : (EnvironmentContext, Array[ParseNode], Array[ParseNode?]) -> ParseNode raise ParseFailure
    }

    EnvironmentSpec::make

    fn EnvironmentSpec::make(names : Array[String], num_args : Int, num_optional_args? : Int, arg_types? : Array[ArgType], handler~ : (EnvironmentContext, Array[ParseNode], Array[ParseNode?]) -> ParseNode raise ParseFailure) -> EnvironmentSpec

    FunctionContext

    pub(all) struct FunctionContext {
    func_name : String
    mode : Mode
    token : Token?
    break_on_token_text : String?
    set_macro : (String, MacroDefinition?) -> Unit
    report_nonstrict : (String, String) -> Unit raise ParseFailure
    is_trusted : (TrustContext) -> Bool
    parse_optional_size : () -> Measurement? raise ParseFailure
    display_mode : Bool
    use_strict_behavior : (String, String) -> Bool
    current_color : () -> String? raise ParseFailure
    in_left_right : () -> Bool
    parse_expression : (Bool, String?) -> Array[ParseNode] raise ParseFailure
    parse_math_mode : (String) -> Array[ParseNode] raise ParseFailure
    parse_left_right : (String) -> ParseNode raise ParseFailure
    pop_token : () -> Token raise ParseFailure
    future_token : () -> Token raise ParseFailure
    push_token : (Token) -> Unit
    consume_spaces : () -> Unit raise ParseFailure
    consume_macro_arg : () -> Array[Token] raise ParseFailure
    expand_tokens : (Array[Token]) -> Array[Token] raise ParseFailure
    get_macro : (String) -> MacroDefinition?
    set_macro_definition : (String, MacroDefinition, Bool) -> Unit
    is_expandable : (String) -> Bool
    parse_prefixed_function : (String) -> ParseNode raise ParseFailure
    parse_environment : (String) -> ParseNode raise ParseFailure
    }

    FunctionRegistry

    pub(all) struct FunctionRegistry {
    entries : Map[String, FunctionSpec]
    }

    FunctionRegistry::get

    fn FunctionRegistry::get(self : FunctionRegistry, name : String) -> FunctionSpec?

    FunctionRegistry::make

    FunctionRegistry::register

    fn FunctionRegistry::register(self : FunctionRegistry, spec : FunctionSpec) -> Unit

    FunctionSpec

    pub(all) struct FunctionSpec {
    names : Array[String]
    num_args : Int
    num_optional_args : Int
    arg_types : Array[ArgType]
    allowed_in_argument : Bool
    allowed_in_text : Bool
    allowed_in_math : Bool
    infix : Bool
    primitive : Bool
    primitive_after_missing_optional : Int?
    handler : (FunctionContext, Array[ParseNode], Array[ParseNode?]) -> ParseNode raise ParseFailure?
    }

    FunctionSpec::make

    fn FunctionSpec::make(names : Array[String], num_args : Int, num_optional_args? : Int, arg_types? : Array[ArgType], allowed_in_argument? : Bool, allowed_in_text? : Bool, allowed_in_math? : Bool, infix? : Bool, primitive? : Bool, primitive_after_missing_optional? : Int, handler? : (FunctionContext, Array[ParseNode], Array[ParseNode?]) -> ParseNode raise ParseFailure) -> FunctionSpec

    LapAlignment

    pub(all) enum LapAlignment {
    LLap
    RLap
    CLap
    } derive(Eq, ToJson,
    Debug
    )

    LapAlignment::equal

    LapAlignment::not_equal

    fn LapAlignment::not_equal(x : LapAlignment, y : LapAlignment) -> Bool

    LapAlignment::to_json

    MacroDefinition

    pub(all) enum MacroDefinition {
    Text(String)
    Expansion(MacroExpansion)
    }

    MacroExpansion

    pub(all) struct MacroExpansion {
    tokens : Array[Token]
    num_args : Int
    delimiters : Array[Array[String]]?
    unexpandable : Bool
    }

    Macros

    pub struct Macros(Map[String, MacroDefinition])

    Macros::make

    fn Macros::make(macros? : Map[String, String]) -> Macros

    Measurement

    pub(all) struct Measurement {
    number : Double
    unit : String
    } derive(Eq, ToJson,
    Debug
    )

    Measurement::equal

    fn Measurement::equal(Measurement, Measurement) -> Bool

    Measurement::not_equal

    fn Measurement::not_equal(x : Measurement, y : Measurement) -> Bool

    Measurement::to_json

    fn Measurement::to_json(Measurement) -> Json

    Mode

    pub(all) enum Mode {
    Math
    Text
    } derive(Eq, ToJson,
    Debug
    )

    Mode::equal

    fn Mode::equal(Mode, Mode) -> Bool

    Mode::not_equal

    fn Mode::not_equal(x : Mode, y : Mode) -> Bool

    Mode::to_json

    fn Mode::to_json(Mode) -> Json

    Mode::to_repr

    OperatorContent

    pub(all) enum OperatorContent {
    SymbolOperator(String)
    BodyOperator(Array[ParseNode])
    NamedOperator(String)
    } derive(Eq, ToJson,
    Debug
    )

    OperatorContent::equal

    OperatorContent::not_equal

    fn OperatorContent::not_equal(x : OperatorContent, y : OperatorContent) -> Bool

    OperatorContent::to_json

    ParseNode

    pub(all) enum ParseNode {
    Internal(mode~ : Mode)
    Raw(mode~ : Mode, string~ : String)
    ColorToken(mode~ : Mode, color~ : String)
    Size(mode~ : Mode, value~ : Measurement, is_blank~ : Bool)
    Url(mode~ : Mode, url~ : String)
    Styling(mode~ : Mode, body~ : Array[ParseNode], style~ : StyleLevel, reset_font~ : Bool)
    Sqrt(mode~ : Mode, body~ : ParseNode, index~ : ParseNode?)
    Infix(mode~ : Mode, replace_with~ : String, size~ : Measurement?, loc~ : SourceLocation?)
    GenFrac(mode~ : Mode, numer~ : ParseNode, denom~ : ParseNode, continued~ : Bool, has_bar_line~ : Bool, bar_size~ : Measurement?, left_delim~ : String?, right_delim~ : String?)
    Text(mode~ : Mode, body~ : Array[ParseNode], font~ : String)
    Font(mode~ : Mode, font~ : String, body~ : ParseNode)
    MClass(mode~ : Mode, mclass~ : AtomFamily, body~ : Array[ParseNode], is_character_box~ : Bool)
    Op(mode~ : Mode, limits~ : Bool, always_handle_sup_sub~ : Bool, parent_is_sup_sub~ : Bool, suppress_base_shift~ : Bool, content~ : OperatorContent)
    OperatorName(mode~ : Mode, body~ : Array[ParseNode], always_handle_sup_sub~ : Bool, limits~ : Bool, parent_is_sup_sub~ : Bool)
    Overline(mode~ : Mode, body~ : ParseNode)
    Underline(mode~ : Mode, body~ : ParseNode)
    Smash(mode~ : Mode, body~ : ParseNode, smash_height~ : Bool, smash_depth~ : Bool)
    Phantom(mode~ : Mode, body~ : Array[ParseNode])
    VPhantom(mode~ : Mode, body~ : ParseNode)
    Pmb(mode~ : Mode, mclass~ : AtomFamily, body~ : Array[ParseNode])
    VCenter(mode~ : Mode, body~ : ParseNode)
    Rule(mode~ : Mode, shift~ : Measurement?, width~ : Measurement, height~ : Measurement)
    RaiseBox(mode~ : Mode, dy~ : Measurement, body~ : ParseNode)
    HBox(mode~ : Mode, body~ : Array[ParseNode])
    Lap(mode~ : Mode, alignment~ : LapAlignment, body~ : ParseNode)
    MathChoice(mode~ : Mode, display~ : Array[ParseNode], text~ : Array[ParseNode], script~ : Array[ParseNode], scriptscript~ : Array[ParseNode])
    Sizing(mode~ : Mode, size~ : Int, body~ : Array[ParseNode])
    HorizBrace(mode~ : Mode, label~ : String, is_over~ : Bool, base~ : ParseNode)
    XArrow(mode~ : Mode, label~ : String, body~ : ParseNode, below~ : ParseNode?)
    AccentUnder(mode~ : Mode, label~ : String, base~ : ParseNode)
    DelimSizing(mode~ : Mode, size~ : Int, mclass~ : AtomFamily, delim~ : String)
    LeftRightRight(mode~ : Mode, delim~ : String, color~ : String?)
    LeftRight(mode~ : Mode, body~ : Array[ParseNode], left~ : String, right~ : String, right_color~ : String?)
    Middle(mode~ : Mode, delim~ : String)
    Kern(mode~ : Mode, dimension~ : Measurement)
    Enclose(mode~ : Mode, body~ : ParseNode, label~ : String, background_color~ : String?, border_color~ : String?)
    Href(mode~ : Mode, href~ : String, body~ : Array[ParseNode])
    Html(mode~ : Mode, attributes~ : Map[String, String], body~ : Array[ParseNode])
    IncludeGraphics(mode~ : Mode, alt~ : String, width~ : Measurement, height~ : Measurement, totalheight~ : Measurement, src~ : String)
    Tag(mode~ : Mode, body~ : Array[ParseNode], tag~ : Array[ParseNode])
    Array(mode~ : Mode, body~ : Array[Array[ParseNode]], add_jot~ : Bool, array_stretch~ : Double, columns~ : Array[ArrayColumn]?, row_gaps~ : Array[Measurement?], hskip_before_and_after~ : Bool, hlines_before_row~ : Array[Array[Bool]], column_separation_type~ : ColumnSeparationType?, tags~ : Array[Array[ParseNode]?]?, auto_tags~ : Array[Bool]?, leqno~ : Bool)
    EnvironmentEnd(mode~ : Mode, name~ : String)
    CdLabel(mode~ : Mode, side~ : String, label~ : ParseNode)
    CdParent(mode~ : Mode, fragment~ : ParseNode)
    Cr(mode~ : Mode, new_line~ : Bool, size~ : Measurement?)
    HtmlMathML(mode~ : Mode, html~ : Array[ParseNode], mathml~ : Array[ParseNode])
    OrdGroup(mode~ : Mode, loc~ : SourceLocation?, body~ : Array[ParseNode], semisimple~ : Bool)
    SupSub(mode~ : Mode, base~ : ParseNode?, sup~ : ParseNode?, sub~ : ParseNode?)
    TextOrd(mode~ : Mode, loc~ : SourceLocation?, text~ : String)
    MathOrd(mode~ : Mode, loc~ : SourceLocation?, text~ : String)
    Spacing(mode~ : Mode, loc~ : SourceLocation?, text~ : String)
    AccentToken(mode~ : Mode, loc~ : SourceLocation?, text~ : String)
    OperatorToken(mode~ : Mode, loc~ : SourceLocation?, text~ : String)
    Accent(mode~ : Mode, loc~ : SourceLocation?, label~ : String, is_stretchy~ : Bool, is_shifty~ : Bool, base~ : ParseNode)
    Verb(mode~ : Mode, loc~ : SourceLocation?, body~ : String, star~ : Bool)
    Atom(mode~ : Mode, loc~ : SourceLocation?, family~ : AtomFamily, text~ : String)
    Color(mode~ : Mode, color~ : String, body~ : Array[ParseNode])
    } derive(Eq, ToJson,
    Debug
    )

    ParseNode::equal

    fn ParseNode::equal(ParseNode, ParseNode) -> Bool

    ParseNode::mode

    fn ParseNode::mode(self : ParseNode) -> Mode

    ParseNode::not_equal

    fn ParseNode::not_equal(x : ParseNode, y : ParseNode) -> Bool

    ParseNode::to_json

    fn ParseNode::to_json(ParseNode) -> Json

    Settings

    pub struct Settings {
    throw_on_error : Bool
    display_mode : Bool
    leqno : Bool
    error_color : String
    color_is_text_color : Bool
    max_expand : Int
    global_group : Bool
    macros : Map[String, String]
    macro_store : Macros?
    strict : Strictness
    strict_warning_handler : (String) -> Unit?
    trust : TrustPolicy
    }

    impl Default for Settings

    Settings::default

    fn Settings::default() -> Settings

    Settings::make

    fn Settings::make(throw_on_error? : Bool, display_mode? : Bool, leqno? : Bool, error_color? : String, color_is_text_color? : Bool, max_expand? : Int, global_group? : Bool, macros? : Map[String, String], macro_store? : Macros, strict? : Strictness, strict_warning_handler? : (String) -> Unit, trust? : TrustPolicy) -> Settings

    SourceLocation

    pub(all) struct SourceLocation {
    input : String
    start : Int
    end : Int
    } derive(Eq, ToJson,
    Debug
    )

    SourceLocation::equal

    SourceLocation::make

    fn SourceLocation::make(input : String, start~ : Int, end~ : Int) -> SourceLocation

    SourceLocation::not_equal

    fn SourceLocation::not_equal(x : SourceLocation, y : SourceLocation) -> Bool

    SourceLocation::to_json

    StrictResponse

    pub(all) enum StrictResponse {
    Ignore
    Warn
    Error
    }

    Strictness

    pub(all) enum Strictness {
    Ignore
    Warn
    Error
    Callback((String, String, SourceLocation?) -> StrictResponse raise ParseFailure)
    }

    StyleLevel

    pub(all) enum StyleLevel {
    DisplayStyle
    TextStyle
    ScriptStyle
    ScriptScriptStyle
    } derive(Eq, ToJson,
    Debug
    )

    StyleLevel::equal

    fn StyleLevel::equal(StyleLevel, StyleLevel) -> Bool

    StyleLevel::not_equal

    fn StyleLevel::not_equal(x : StyleLevel, y : StyleLevel) -> Bool

    StyleLevel::to_json

    fn StyleLevel::to_json(StyleLevel) -> Json

    Token

    pub(all) struct Token {
    text : String
    loc : SourceLocation?
    noexpand : Bool
    treat_as_relax : Bool
    }

    TrustContext

    pub(all) enum TrustContext {
    UrlTrust(command~ : String, url~ : String, protocol~ : String?)
    HtmlClass(class~ : String)
    HtmlId(id~ : String)
    HtmlStyle(style~ : String)
    HtmlData(attributes~ : Map[String, String])
    }

    TrustPolicy

    pub(all) enum TrustPolicy {
    Untrusted
    Trusted
    Callback((TrustContext) -> Bool)
    }

    UnicodeScript

    pub struct UnicodeScript {
    replacement : String
    kind : UnicodeScriptKind
    }

    UnicodeScriptKind

    pub(all) enum UnicodeScriptKind {
    UnicodeSubscript
    UnicodeSuperscript
    } derive(Eq)

    UnicodeScriptKind::equal

    UnicodeScriptKind::not_equal

    fn UnicodeScriptKind::not_equal(x : UnicodeScriptKind, y : UnicodeScriptKind) -> Bool

    U00A0

    let U00A0 : String

    build_environment_registry

    fn build_environment_registry(extra_specs : Array[EnvironmentSpec]) -> EnvironmentRegistry

    Builds an environment registry from the builtin specs plus caller-provided extension specs (which override builtins sharing the same name).

    build_function_registry

    fn build_function_registry(extra_specs : Array[FunctionSpec]) -> FunctionRegistry

    Builds a function registry from the builtin specs plus caller-provided extension specs (e.g. \eval registered by the eval package).

    parse

    fn parse(input : String, settings? : Settings, extra_specs? : Array[FunctionSpec], extra_env_specs? : Array[EnvironmentSpec]) -> Array[ParseNode] raise ParseFailure

    supported_codepoint

    fn supported_codepoint(code : Int) -> Bool

    True when the codepoint falls within one of the supported Unicode script blocks. Mirrors KaTeX's supportedCodepoint (unicodeScripts.ts).

    unicode_font_character

    fn unicode_font_character(font : String, char : String) -> String?

    Returns the Unicode mathematical alphanumeric character for a given font style and ASCII letter/digit, or None when no mapping exists.

    Font styles that match LaTeX commands:
    • mathbf / boldsymbol — Bold / Bold Italic
    • mathit — Italic
    • mathbb — Double-struck (blackboard bold)
    • mathcal — Calligraphic / Script
    • mathfrak — Fraktur
    • mathscr — Script (rsfs)
    • mathsf — Sans-serif
    • mathtt — Monospace
    • mathsfit — Sans-serif Italic
    • mathrm / mathnormal — Roman (identity, not handled here) / Italic

    unicode_script_character

    fn unicode_script_character(kind : UnicodeScriptKind, replacement : String) -> String?

    Returns the Unicode superscript or subscript character for replacement.

    The parser and renderers share unicode_scripts as the canonical table.

    unicode_symbol

    fn unicode_symbol(name : String) -> String?

    Returns a registered symbol's Unicode replacement, if it has one.