Skip to content

syntax: add Mojo language support - #4265

Open
anatolvitold wants to merge 2 commits into
micro-editor:masterfrom
anatolvitold:syntax-mojo
Open

anatolvitold wants to merge 2 commits into
micro-editor:masterfrom
anatolvitold:syntax-mojo

Conversation

@anatolvitold

Copy link
Copy Markdown

Adds syntax highlighting for Mojo (.mojo and .🔥 files).

The keywords come from the Mojo lexer's keyword table (TokenKinds.def) and the Mojo keyword reference.

Highlighted:

  • Python's keywords plus comptime, struct, trait, var, ref and Self
  • the soft keywords raises, capturing and where; where(...) as a function call stays plain
  • the argument conventions mut, out, deinit and imm, only when an argument name follows them, so var out = 0 stays plain
  • CamelCase types, ALL_CAPS constants, built-in functions including type_of, origin_of and conforms_to, dunder methods, and decorators
  • numbers with _ separators, the 0b/0o/0x prefixes, and floats such as 1.5e-3, .5 and 1.
  • strings in all four quote styles, with Mojo's escape sequences; template strings (t"Hello {name}", rt"...", single-line or triple-quoted) also highlight {...} interpolation and the {{ }} escapes, while other prefixed forms such as r"..." are highlighted as plain strings
  • backtick-escaped identifiers (`struct`, MLIR operation names) are left plain

Highlights Mojo (.mojo and .🔥 files). Keywords follow the Mojo lexer's
keyword table (TokenKinds.def) and the Mojo keyword reference: Python's
keywords plus comptime, struct, trait, var, ref, Self and the
compile-time origin_of/type_of/conforms_to; the soft keywords raises,
capturing and where; and the argument conventions mut, out, deinit and
imm when an argument name follows them, so a variable named out stays
plain.

Strings in all four quote styles highlight Mojo's escape sequences.
Template strings (t"...", rt"..." and tr"...", single-line or
triple-quoted) also highlight {...} interpolation and the {{ }} escapes;
the other prefixed forms, such as r"..." and t'...', are highlighted as
plain strings. Numbers allow _ separators and the 0b/0o/0x prefixes.
Backtick-escaped identifiers are left plain.

@Andriamanitra Andriamanitra left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Mojo close enough to Python that we could do - include: python instead of repeating all of the rules? It would both make the syntax file quite a bit shorter and ensure future improvements to our Python syntax automatically apply to Mojo as well.

@anatolvitold

Copy link
Copy Markdown
Author

They are close enough for now (as of Mojo 1.1.0). Though they might drift apart in the future.

@anatolvitold

Copy link
Copy Markdown
Author

@Andriamanitra

So, I tried rewriting mojo.yaml with - include: "python". While it shrinks the file from 96 to 50 lines, there are some noticeable drawbacks to this approach. For instance:

  • Mojo code uses type, bytes, list, format and id as ordinary names. Without a way to provide exceptions to include statement, it leads to erroneous highlighting.
  • Built-in functions look like types. range, map and zip get the type color.
  • There is also a peculiarity with @. Namely, @ of decorators gets the operator color instead.

Source for mojo.yaml with - include: "python" statement:

filetype: mojo

detect:
    filename: "\\.(mojo|🔥)$"

rules:
    # micro adds Python's rules after the Mojo rules below
    - include: "python"
    # ALL_CAPS constants; CamelCase types, plus SIMD and the Self keyword
    - constant: "\\b_*[A-Z][A-Z0-9_]*[A-Z][A-Z0-9_]*\\b"
    - type: "\\b_*[A-Z]+[a-zA-Z0-9_]*[a-z]+[a-zA-Z0-9_]*\\b"
    - type: "\\b(SIMD|Self)\\b"
    # built-in functions
    - identifier: "\\b(atof|atol|conforms_to|debug_assert|origin_of|rebind|sort|swap|type_of|__mlir_attr|__mlir_op|__mlir_type)\\b"
    # special method names
    - identifier: "\\b__[A-Za-z0-9_]+__\\b"
    # keywords
    - statement: "\\b(comptime|fn|ref|struct|trait|var|__async|__await|__enum|__extension|__mlir_region)\\b"
    # soft keywords
    - statement: "\\b(capturing|raises)\\b"
    - statement: "\\bwhere(\\s|$)"
    # argument conventions, only when an argument name follows
    - statement: "\\b(deinit|imm|mut|out|read)\\s+\\b"
    # numbers: _ separators, E exponents, .5 and 1.
    - constant.number: "(\\b[0-9][0-9_]*(\\.[0-9_]*)?|\\B\\.[0-9][0-9_]*)([eE][+-]?[0-9][0-9_]*)?"

    # t-strings: t"...", rt"..." and tr"..."; other prefixed forms fall back to plain strings
    - constant.string:
        start: "\\b([tT][rR]?|[rR][tT])\"\"\""
        end: "\"\"\""
        skip: "\\\\."
        rules:
            - constant.specialChar: "\\\\."
            - identifier: "\\{\\{|\\}\\}|\\{[^{}]*\\}"
            - constant.specialChar: "\\{\\{|\\}\\}"

    - constant.string:
        start: "\\b([tT][rR]?|[rR][tT])\""
        end: "(\"|$)"
        skip: "\\\\."
        rules:
            - constant.specialChar: "\\\\."
            - identifier: "\\{\\{|\\}\\}|\\{[^{}]*\\}"
            - constant.specialChar: "\\{\\{|\\}\\}"

    # escaped identifiers: `struct`, `pop.add`
    - default:
        start: "`"
        end: "(`|$)"
        rules: []

@Andriamanitra

Andriamanitra commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

@Andriamanitra

So, I tried rewriting mojo.yaml with - include: "python". While it shrinks the file from 96 to 50 lines, there are some noticeable drawbacks to this approach. For instance:

  • Mojo code uses type, bytes, list, format and id as ordinary names. Without a way to provide exceptions to include statement, it leads to erroneous highlighting.

  • Built-in functions look like types. range, map and zip get the type color.

Ok, if it's not a full superset it's probably better to not include the Python syntax.

I think the idea behind the type coloring is that those builtins are implemented as classes in Python (although the usage looks identical to builtins that are implemented as actual functions). Admittedly it's a bit weird.

  • There is also a peculiarity with @. Namely, @ of decorators gets the operator color instead.

I think this is a bug that we should fix in the Python syntax file, I've opened a PR to do just that: #4271

@Andriamanitra Andriamanitra left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the syntax with various pieces of Mojo code and as far as I can tell everything is working fine. It would be good to also add Mojo to the built-in comment plugin.

@anatolvitold

Copy link
Copy Markdown
Author

Thanks for testing! I've added Mojo to the comment plugin, both the comment.lua table and the list in its help page.

@anatolvitold

Copy link
Copy Markdown
Author

@Andriamanitra Please let me know if you need anything else to merge the PR.

@Andriamanitra

Copy link
Copy Markdown
Collaborator

From my point of view it's good to go now but the decision to merge is up to the maintainers.

@anatolvitold

Copy link
Copy Markdown
Author

@Andriamanitra I see. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants