Skip to content

Instantly share code, notes, and snippets.

@hyrious
Created January 28, 2025 06:28
Show Gist options
  • Save hyrious/0eaccc0f316cebb9291c32b3bf09f689 to your computer and use it in GitHub Desktop.
Save hyrious/0eaccc0f316cebb9291c32b3bf09f689 to your computer and use it in GitHub Desktop.
Basic MoonBit syntax highlighting support in Sublime Text
%YAML 1.2
---
# - https://www.sublimetext.com/docs/syntax.html
# - https://docs.moonbitlang.com/en/latest/language/introduction.html
# - https://github.com/moonbitlang/moonbit-docs/blob/main/next/_ext/lexer.py
file_extensions:
- mbt
first_line_match: |-
(?xi:
^ \s* // .*? -\*- .*? \bmoonbit\b .*? -\*- # editorconfig
)
name: MoonBit
scope: source.moonbit
version: 2
variables:
# https://docs.moonbitlang.com/en/latest/language/introduction.html#naming-conventions
ident_lower: '[a-z][\w_]*'
ident_upper: '[A-Z][\w_]*'
escaped_char: '\\(?:[nrtb\"''\\]|x[0-7]\h|o[0-3][0-7]{2}|u\{(?:\h_*){1,6}\})'
int_suffixes: '[uUlL]+'
dec_literal: '\d[\d_]*'
hex_literal: '\h[\h_]*'
float_exponent: '[eE][+-]?{{dec_literal}}'
float_power: '[pP][+-]?{{dec_literal}}'
contexts:
prototype:
- include: comments
main:
- include: statements
comments:
- include: line-comments
line-comments:
- match: ///
scope: punctuation.definition.comment.moonbit
push: doc-comment-body
- match: //
scope: punctuation.definition.comment.moonbit
push: line-comment-body
# https://docs.moonbitlang.com/en/latest/language/docs.html
doc-comment-body:
- meta_include_prototype: false
- meta_scope: comment.block.documentation.moonbit
- match: $\n?
pop: true
- match: '@\w+'
scope: entity.other.attribute-name.documentation.moonbit
line-comment-body:
- meta_include_prototype: false
- meta_scope: comment.line.double-slash.moonbit
- match: $\n?
pop: true
statements:
- include: expressions
expressions:
- include: function-identifiers
- include: strings
- include: keywords
- include: numbers
- include: operators
function-identifiers:
- match: '{{ident_lower}}(?=!?\s*\()'
scope: variable.function.moonbit
keywords:
- include: bool
- match: \b(Eq|Compare|Hash|Show|Default|ToJson|FromJson)\b
scope: support.type.builtin.moonbit
- match: \b(Array|FixedArray|Int|Int64|UInt|UInt64|Option|Result|Byte|Bool|Unit|String|Float|Double)\b
scope: support.type.builtin.moonbit
- match: \b(extern|test|init|as)\b
scope: keyword.other.moonbit
- match: \bself\b
scope: keyword.other.moonbit
- match: \bSelf\b
scope: storage.type.moonbit
- match: \blet\b
scope: keyword.declaration.variable.moonbit
- match: \b(const|mut)\b
scope: storage.modifier.moonbit
- match: \b(pub\(all\)|pub\(open\)|pub\(readonly\)|pub|priv)\b
scope: storage.modifier.moonbit
- match: \bfn\b
scope: keyword.declaration.function.moonbit
- match: '\b(impl|with|derive)\b'
scope: keyword.declaration.impl.moonbit
- match: \b(type!?|enum|struct|trait|typealias)\b
scope: keyword.declaration.type.moonbit
- match: \b(guard|else|for|if|loop|match|while|in)\b
scope: keyword.control.moonbit
- match: \b(try|catch!?|raise)\b
scope: keyword.control.moonbit
- match: \b(break|continue)\b
scope: keyword.control.moonbit
- match: \breturn\b
scope: keyword.control.moonbit
# https://docs.moonbitlang.com/en/latest/language/fundamentals.html#boolean
bool:
- match: \bfalse\b
scope: constant.language.boolean.false.moonbit
- match: \btrue\b
scope: constant.language.boolean.true.moonbit
# https://docs.moonbitlang.com/en/latest/language/fundamentals.html#number
numbers:
- include: floats
- include: integers
floats:
- match: '\b0x{{hex_literal}}\.(?:{{hex_literal}})?(?:{{float_exponent}})?(?:{{float_power}})?'
scope: constant.numeric.float.moonbit
- match: '\b{{dec_literal}}\.(?:{{dec_literal}})?(?:{{float_exponent}})?(?:{{float_power}})?'
scope: constant.numeric.float.moonbit
integers:
- match: '\b({{dec_literal}})({{int_suffixes}})?\b'
captures:
1: constant.numeric.integer.decimal.moonbit
2: storage.type.numeric.moonbit
- match: '\b(0x{{hex_literal}})({{int_suffixes}})?\b'
captures:
1: constant.numeric.integer.hexadecimal.moonbit
2: storage.type.numeric.moonbit
- match: '\b(0o[0-7][0-7_]*)({{int_suffixes}})?\b'
captures:
1: constant.numeric.integer.octal.moonbit
2: storage.type.numeric.moonbit
- match: '\b(0b[0-1][0-1_]*)({{int_suffixes}})?\b'
captures:
1: constant.numeric.integer.binary.moonbit
2: storage.type.numeric.moonbit
# https://docs.moonbitlang.com/en/latest/language/fundamentals.html#string
strings:
- match: '"'
scope: punctuation.definition.string.begin.moonbit
push: string-content
- match: '#\|'
scope: punctuation.definition.string.begin.moonbit
push: raw-string-content
- match: '\$\|'
scope: punctuation.definition.string.begin.moonbit
push: escape-string-content
escape-string-content:
- meta_include_prototype: false
- meta_scope: string.unquoted.moonbit
- match: $\n?
pop: true
- include: string-interpolations
- include: escaped-chars
raw-string-content:
- meta_include_prototype: false
- meta_scope: string.unquoted.moonbit
- match: $\n?
pop: true
string-content:
- meta_include_prototype: false
- meta_scope: string.quoted.double.moonbit
- match: '"'
scope: punctuation.definition.string.end.moonbit
pop: true
- include: string-interpolations
- include: escaped-chars
string-interpolations:
- match: '\\{'
scope: punctuation.section.interpolation.begin.moonbit
push: string-interpolation-content
string-interpolation-content:
- clear_scopes: true
- meta_scope: meta.interpolation.moonbit
- meta_content_scope: source.moonbit.embedded
- match: '}'
scope: punctuation.section.interpolation.end.moonbit
pop: true
- include: expressions
escaped-chars:
- match: '{{escaped_char}}'
scope: constant.character.escape.moonbit
operators:
- match: '=>'
scope: keyword.control.moonbit
- match: '->'
scope: storage.type.function.arrow.moonbit
- match: ':(?!:)'
scope: punctuation.separator.moonbit
push: maybe-types
- match: '=(?!=)'
scope: keyword.operator.assignment.moonbit
- match: '\.\.<|\.\.=|\.\.(?!\.)'
scope: keyword.operator.range.moonbit
- match: ','
scope: punctuation.separator.moonbit
- match: '{'
scope: punctuation.section.block.begin.moonbit
- match: '}'
scope: punctuation.section.block.end.moonbit
- match: '\+|-|\*|\/|%|\|>|>>|<<|&&|\|\||\&|\||<|>|=='
scope: keyword.operator.moonbit
- match: \b(not|lsl|lsr|asr|op_add|op_sub|op_div|op_mul|op_mod|\.\.\.)\b
scope: keyword.operator.word.moonbit
maybe-types:
- match: '(?=,|\)|\]|\}|\|)'
pop: true
- match: '{{ident_upper}}'
scope: storage.type.moonbit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment