bobzhang/pygments/lexer does not have a README file

    CallbackFn

    type CallbackFn = (Ctx,
    Match
    , Array[CallbackArg]) -> Unit raise

    A hand-written callback (Python: a callable token action).

    Options

    type Options = Map[String, String]

    Lexer, formatter and filter options. As on the pygmentize command line, every value is a string; typed accessors mirror pygments.util.

    Tokenizer

    type Tokenizer = (Lexer, String, Array[String]) -> Array[Token] raise

    Tokenizes preprocessed text starting from a state stack.

    LexError

    pub suberror LexError {
    MissingCallback(String)
    UnknownState(String)
    } derive(
    Debug
    )

    Errors raised while lexing.

    OptionError

    pub suberror OptionError {
    OptionError(String)
    } derive(
    Debug
    )

    Errors raised for invalid option values.

    Action

    pub(all) enum Action {
    Nop
    Emit(
    TokenType
    )
    ByGroups(Array[Action])
    Using(UsingTarget, Array[String]?, Array[(String, String)])
    Call(String, Array[CallbackArg])
    }

    What to do with a match (Python: the second element of a rule).

    CallbackArg

    pub(all) enum CallbackArg {
    Str(String)
    Tok(
    TokenType
    )
    Bool(Bool)
    Int(Int)
    } derive(
    Debug
    )

    Arguments captured by a callback factory (e.g. the token type passed to YAML's save_indent(Text, start=True)).

    Ctx

    pub(all) struct Ctx {
    lexer : Lexer
    def : RegexDef
    text : String
    pos : Int
    end : Int
    stack : Array[Int]
    out : Array[Token]
    callbacks : Map[String, (Ctx,
    Match
    , Array[CallbackArg]) -> Unit raise]
    }

    The lexing context handed to callbacks (Python's LexerContext plus the output buffer). Callbacks may move pos and end.

    Ctx::apply

    fn Ctx::apply(self : Ctx, action : Action, m :
    Match
    ) -> Unit raise

    Applies action to m (Python: calling a token action).

    Ctx::emit

    fn Ctx::emit(self : Ctx, index : Int, ttype :
    TokenType
    , value : String) -> Unit

    Appends a token to the output.

    Ctx::emit_lexed

    fn Ctx::emit_lexed(self : Ctx, lexer : Lexer, text : String, offset : Int, stack? : Array[String]) -> Unit raise

    Lexes text with lexer and appends the tokens shifted by offset (Python: for i, t, v in lx.get_tokens_unprocessed(...): yield i + offset, t, v).

    Ctx::is_extended

    fn Ctx::is_extended(self : Ctx) -> Bool

    Whether this context follows ExtendedRegexLexer semantics.

    Ctx::new

    fn Ctx::new(lexer : Lexer, def : RegexDef, text : String, callbacks? : Map[String, (Ctx,
    Match
    , Array[CallbackArg]) -> Unit raise], pos? : Int, end? : Int, stack? : Array[String]) -> Ctx raise

    Creates a context for run_context, e.g. to pre-set the position or stack.

    Ctx::pop_state

    fn Ctx::pop_state(self : Ctx) -> Unit

    Pops a state, keeping at least one on the stack.

    Ctx::push_state

    fn Ctx::push_state(self : Ctx, name : String) -> Unit raise

    Pushes the state name.

    Ctx::set_state_stack

    fn Ctx::set_state_stack(self : Ctx, names : Array[String]) -> Unit raise

    Replaces the state stack.

    Ctx::state_stack

    fn Ctx::state_stack(self : Ctx) -> Array[String]

    Names of the states on the stack, bottom first.

    Ctx::top_state

    fn Ctx::top_state(self : Ctx) -> String

    Name of the current (top) state.

    Filter

    pub(all) struct Filter {
    name : String
    apply : (Lexer?, Array[(
    TokenType
    , String)]) -> Array[(
    TokenType
    , String)] raise
    }

    A token stream filter (see pygments.filters).

    Lexer

    pub struct Lexer {
    info : LexerInfo
    options : Map[String, String]
    stripnl : Bool
    stripall : Bool
    ensurenl : Bool
    tabsize : Int
    filters : Array[Filter]
    // private fields
    }

    An instance of a lexer class with its options.

    Lexer::add_filter

    fn Lexer::add_filter(self : Lexer, f : Filter) -> Unit

    Appends a filter to this lexer.

    Lexer::get_tokens

    fn Lexer::get_tokens(self : Lexer, text : String, unfiltered? : Bool) -> Array[(
    TokenType
    , String)] raise

    Python's Lexer.get_tokens: preprocesses text, tokenizes it and applies the lexer's filters (unless unfiltered).

    Lexer::get_tokens_unprocessed

    fn Lexer::get_tokens_unprocessed(self : Lexer, text : String, stack? : Array[String]) -> Array[Token] raise

    Python's get_tokens_unprocessed: tokenizes text as is, starting with stack (default ["root"]). Offsets are UTF-16 offsets into text.

    Lexer::name

    fn Lexer::name(self : Lexer) -> String

    A lexer's display name, e.g. Python.

    Lexer::new

    fn Lexer::new(info : LexerInfo, options : Map[String, String], create : (Map[String, String]) -> Lexer raise, tokenizer : (Lexer, String, Array[String]) -> Array[Token] raise) -> Lexer raise OptionError

    Builds a lexer instance. create must construct a new instance of the same lexer class (used by using(this, ...)).

    Lexer::preprocess

    fn Lexer::preprocess(self : Lexer, text : String) -> String

    Python's Lexer._preprocess_lexer_input for text input: drops a BOM, normalizes newlines and applies stripall/stripnl/tabsize/ensurenl.

    Lexer::recreate

    fn Lexer::recreate(self : Lexer, options : Map[String, String]) -> Lexer raise

    Creates a new instance of the same lexer class with options.

    LexerInfo

    pub(all) struct LexerInfo {
    class_name : String
    name : String
    aliases : Array[String]
    filenames : Array[String]
    alias_filenames : Array[String]
    mimetypes : Array[String]
    priority : Double
    url : String
    version_added : String
    analyse_text : (String) -> Double
    doc : String
    }

    Static metadata of a lexer class (name, aliases, file patterns, ...).

    RegexDef

    pub struct RegexDef {
    name : String
    state_names : Array[String]
    extended : Bool
    // private fields
    }

    The processed token definitions of a RegexLexer class: named states, each an ordered list of rules (includes, inheritance, words() and combined() already resolved by the exporter).

    RegexDef::new

    fn RegexDef::new(name : String, state_names : Array[String], rules : Array[Rule], states : Array[Array[Int]], extended? : Bool) -> RegexDef

    Builds a definition from rules and, per state, the indices of its rules.

    RegexDef::state

    fn RegexDef::state(self : RegexDef, name : String) -> Int

    Index of the state name.

    Rule

    pub struct Rule {
    pattern : String
    flags : Int
    action : Action
    transition : Array[StateOp]?
    // private fields
    }

    A lexing rule: regex, action and optional transition.

    Rule::new

    fn Rule::new(pattern : String, flags : Int, action : Action, transition : Array[StateOp]?) -> Rule

    Creates a rule. transition is None when the rule does not change the state stack.

    StateOp

    pub(all) enum StateOp {
    Push(String)
    Pop
    Dup
    } derive(
    Debug
    )

    One state-stack operation of a transition.

    Token

    pub(all) struct Token {
    index : Int
    ttype :
    TokenType

    value : String
    } derive(Eq,
    Debug
    )

    One token produced by a lexer: its UTF-16 start offset, type and text.

    UsingTarget

    pub(all) enum UsingTarget {
    This
    Other((Map[String, String]) -> Lexer raise)
    }

    Target of a using(...) action.

    budget

    The budget of the lexing request in progress.

    clamp_score

    fn clamp_score(x : Double) -> Double

    Python's make_analysator wrapper: clamps to [0, 1].

    default_budget_steps

    let default_budget_steps : Int

    Default step budget for one lexing request (shared by nested lexers).

    delegate

    fn delegate(root : Lexer, language : Lexer, text : String, needle? :
    TokenType
    ) -> Array[Token] raise

    Python's DelegatingLexer.get_tokens_unprocessed: lexes with language, then lexes the concatenation of all needle tokens with root and re-inserts the other language tokens.

    do_insertions

    fn do_insertions(insertions : Array[(Int, Array[Token])], tokens : Array[Token]) -> Array[Token]

    Python's do_insertions: inserts each token list of insertions at its index into the stream tokens, splitting tokens as needed.

    doctype_matches

    fn doctype_matches(text : String, regex : String) -> Bool

    Python's doctype_matches.

    expand_tabs

    fn expand_tabs(s : String, tabsize : Int) -> String

    Python's str.expandtabs(tabsize).

    get_bool_opt

    fn get_bool_opt(options : Map[String, String], name : String, default : Bool) -> Bool raise OptionError

    Python's get_bool_opt.

    get_choice_opt

    fn get_choice_opt(options : Map[String, String], name : String, allowed : Array[String], default : String, normcase? : Bool) -> String raise OptionError

    Python's get_choice_opt.

    get_int_opt

    fn get_int_opt(options : Map[String, String], name : String, default : Int) -> Int raise OptionError

    Python's get_int_opt.

    get_list_opt

    fn get_list_opt(options : Map[String, String], name : String, default : Array[String]) -> Array[String]

    Python's get_list_opt: the value split at whitespace.

    html_doctype_matches

    fn html_doctype_matches(text : String) -> Bool

    Python's html_doctype_matches.

    is_space

    fn is_space(c : Char) -> Bool

    Python's str.isspace for one character.

    looks_like_xml

    fn looks_like_xml(text : String) -> Bool

    Python's looks_like_xml.

    matches

    fn matches(pattern :
    Regex
    , text : String) -> Bool

    Python's bool(re.match(pattern, text)).

    no_analyse

    fn no_analyse(_text : String) -> Double

    analyse_text of lexers that do not define one.

    py_slice

    fn py_slice(s : String, start : Int, end : Int) -> String

    Python slicing s[start:end] with clamping (negative end counts from the end, as in Python).
    fn re(pattern : String, flags? : Int) ->
    Regex

    Compiles a pattern known to be valid (aborts otherwise).

    run_context

    fn run_context(ctx : Ctx) -> Unit raise

    Runs a RegexLexer or ExtendedRegexLexer over an existing context (Python: get_tokens_unprocessed(text, context)).

    run_regex

    fn run_regex(lexer : Lexer, def : RegexDef, text : String, stack : Array[String], callbacks? : Map[String, (Ctx,
    Match
    , Array[CallbackArg]) -> Unit raise]) -> Array[Token] raise

    Runs a RegexLexer (or ExtendedRegexLexer) over text, starting with the state stack stack. callbacks resolves Action::Call names.
    fn search(pattern :
    Regex
    , text : String) -> Bool

    Python's bool(re.search(pattern, text, flags)), for analyse_text.

    shebang_matches

    fn shebang_matches(text : String, regex : String) -> Bool

    Python's shebang_matches: whether the last component of the shebang line (ignoring -flags) fully matches regex (case-insensitively).

    split_whitespace

    fn split_whitespace(s : String) -> Array[String]

    Python's str.split() without arguments.

    text_tokenizer

    fn text_tokenizer(_lexer : Lexer, text : String, _stack : Array[String]) -> Array[Token]

    Tokenizer of TextLexer: the whole input as one Text token.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io