Repository navigation
syntax: add Mojo language support - #4265
anatolvitold wants to merge 2 commits into
Conversation
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
left a comment
There was a problem hiding this comment.
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.
|
They are close enough for now (as of Mojo 1.1.0). Though they might drift apart in the future. |
|
So, I tried rewriting
Source for mojo.yaml with 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: [] |
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.
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
left a comment
There was a problem hiding this comment.
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.
|
Thanks for testing! I've added Mojo to the comment plugin, both the comment.lua table and the list in its help page. |
|
@Andriamanitra Please let me know if you need anything else to merge the PR. |
|
From my point of view it's good to go now but the decision to merge is up to the maintainers. |
|
@Andriamanitra I see. Thank you! |
Adds syntax highlighting for Mojo (
.mojoand.🔥files).The keywords come from the Mojo lexer's keyword table (
TokenKinds.def) and the Mojo keyword reference.Highlighted:
comptime,struct,trait,var,refandSelfraises,capturingandwhere;where(...)as a function call stays plainmut,out,deinitandimm, only when an argument name follows them, sovar out = 0stays plaintype_of,origin_ofandconforms_to, dunder methods, and decorators_separators, the0b/0o/0xprefixes, and floats such as1.5e-3,.5and1.t"Hello {name}",rt"...", single-line or triple-quoted) also highlight{...}interpolation and the{{}}escapes, while other prefixed forms such asr"..."are highlighted as plain strings`struct`, MLIR operation names) are left plain