lexer

    Lexer for the MoonBit programming language

    Download zip
    Version
    0.3.16
    License
    Apache-2.0
    Last updated
    yesterday
    Downloads
    30K

    #moonbitlang/lexer

    moonbitlang/lexer turns MoonBit source text into token streams used by the parsers in this repository.

    The main entry points are tokens_from_string and tokens_from_string_with_utf16_location. Both return a LexResult containing tokens, lexical errors, and docstring/comment metadata.

    tokens_from_string uses the lexer default location accounting. tokens_from_string_with_utf16_location tokenizes the same source but reports locations in UTF-16 code units, which is useful for editor and LSP integrations.

    #Examples

    ///|
    test "tokenize source text" {
    let result = @lexer.tokens_from_string("let answer = 42", comment=false)
    assert_eq(result.errors.length(), 0)
    debug_inspect(
    result.tokens.map(triple => triple.0),
    content=(
    #|[LET, LIDENT("answer"), EQUAL, INT("42"), EOF]
    ),
    )
    }

    ///|
    test "utf16 location mode keeps the same tokens" {
    let source = "let x = \"😀\""
    let normal = @lexer.tokens_from_string(source, comment=false)
    let utf16 = @lexer.tokens_from_string_with_utf16_location(
    source,
    comment=false,
    )
    debug_inspect(
    normal.tokens.map(triple => triple.0),
    content=(
    #|[LET, LIDENT("x"), EQUAL, STRING("😀"), EOF]
    ),
    )
    debug_inspect(
    utf16.tokens.map(triple => triple.0),
    content=(
    #|[LET, LIDENT("x"), EQUAL, STRING("😀"), EOF]
    ),
    )
    }

    LexicalError

    pub enum LexicalError {
    IllegalCharacter(Char)
    UnterminatedString
    UnterminatedStringInVariableInterploation
    InterpInvalidMultilineString
    InterpInvalidComment
    InterpInvalidAttribute
    InterpMissingExpression
    InvalidDotInt(String)
    MissingIdentifierAfterDot
    InvalidByteLiteral(String)
    Reserved_keyword(String)
    InvalidMetavarSyntax(String)
    }

    tokens_from_string

    fn tokens_from_string(name? : String, start_pos? :
    Position
    , is_interpolation? : Bool, enable_metavar? : Bool, comment~ : Bool, str : StringView) -> LexResult

    tokens_from_string_with_utf16_location

    fn tokens_from_string_with_utf16_location(name? : String, start_pos? :
    Position
    , is_interpolation? : Bool, enable_metavar? : Bool, comment~ : Bool, str : StringView) -> LexResult

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io