syntax: add Gleam language support#4045
syntax: add Gleam language support#4045HiwarkhedePrasad wants to merge 5 commits intomicro-editor:masterfrom
Conversation
Andriamanitra
left a comment
There was a problem hiding this comment.
Good start but needs some work. I would consider adding highlighting for attributes (@external/@deprecated).
runtime/syntax/gleam.yaml
Outdated
| start: "\"" | ||
| end: "\"" | ||
| skip: "\\\\." | ||
| - special: "\\b[0-9]+\\.?[0-9]*\\b" |
There was a problem hiding this comment.
Numbers should be constant.number. Gleam also supports a pretty wide range of number literals 1 that this regex doesn't cover. The formats seem to be similar to eg. Python so we can probably just copy them from the Python syntax:
micro/runtime/syntax/python3.yaml
Lines 28 to 32 in 42d0ddf
Footnotes
runtime/syntax/gleam.yaml
Outdated
| end: "\"" | ||
| skip: "\\\\." | ||
| - special: "\\b[0-9]+\\.?[0-9]*\\b" | ||
| - type: "\\b[A-Z][a-zA-Z0-9_]*\\b" No newline at end of file |
There was a problem hiding this comment.
This overlaps with the definitions for identifier and constant above so those will get colored as type. To fix it you can put this rule before them (later rules are applied on top of earlier ones). It would also make sense to me to change this rule to be identifier, and use type for the built-in types (Int, Float, etc.) that are currently identifier.
runtime/syntax/gleam.yaml
Outdated
|
|
||
| rules: | ||
| - statement: "\\b(fn|let|case|if|use|import|pub|type|opaque|const|as|assert|panic|todo|echo)\\b" | ||
| - identifier: "\\b(Int|Float|String|Bool|List|Option|Result|Nil|BitArray)\\b" |
There was a problem hiding this comment.
I would remove Nil from here since it's also in constant.
runtime/syntax/gleam.yaml
Outdated
| - statement: "\\b(fn|let|case|if|use|import|pub|type|opaque|const|as|assert|panic|todo|echo)\\b" | ||
| - identifier: "\\b(Int|Float|String|Bool|List|Option|Result|Nil|BitArray)\\b" | ||
| - constant: "\\b(True|False|Nil)\\b" | ||
| - operator: "(\\|>|->|<>|==|!=|<=|>=|&&|\\|\\|)" |
There was a problem hiding this comment.
Most syntaxes choose to use statement for operators (to color them red in the default colorscheme). Most built-in colorschemes color operator with the default (white) color. The list is missing assignment and arithmetic operators.
There was a problem hiding this comment.
Hi @Andriamanitra , thanks for the great feedback! I've pushed a new commit that addresses all your points:
Rule Overlap: Moved the base identifier rule to the top, so the specific type and constant rules below it can correctly overwrite it.
Built-in Types: Removed Nil from the built-in types list (kept it under constants).
Operators: Moved operators to statement so they highlight correctly, and added the missing arithmetic, assignment, and Gleam-specific float operators.
Numbers: Implemented the robust number regexes from the Python syntax file to handle binary, octal, hex, and scientific notation floats.
Attributes: Added a preproc rule (@[a-zA-Z0-9_]+) to highlight attributes like @external and @deprecated.
Andriamanitra
left a comment
There was a problem hiding this comment.
It's working pretty good now but I noticed a couple more issues while testing the syntax with the Gleam code from https://learnxinyminutes.com/gleam
- Number literals with underscores
- Float comparisons (
<.,<=., etc.) - Escapes in strings (eg.
"\"") - Tuples (eg.
#(1, 2))
I don't think commenting the file is necessary, the comments are mostly redundant or irrelevant.
runtime/syntax/gleam.yaml
Outdated
| - statement: "\\b(fn|let|case|if|use|import|pub|type|opaque|const|as|assert|panic|todo|echo)\\b" | ||
|
|
||
| # 5. Attributes / Decorators (Added for @external, @deprecated, etc.) | ||
| - preproc: "@[a-zA-Z0-9_]+" |
There was a problem hiding this comment.
I think this is too permissive. I'm not sure if Gleam even has anything besides @external and @deprecated right now but if we follow vim's example we should only allow lowercase here: 1
| - preproc: "@[a-zA-Z0-9_]+" | |
| - preproc: "@[a-z][a-z_]*" |
Footnotes
runtime/syntax/gleam.yaml
Outdated
| # 7. Numbers (Copied directly from Python's robust micro syntax as the maintainer requested) | ||
| - constant.number: "\\b0b[01]+\\b" | ||
| - constant.number: "\\b0o[0-7]+\\b" | ||
| - constant.number: "\\b0x[0-9a-fA-F]+\\b" | ||
| - constant.number: "\\b[0-9]+\\.[0-9]*([eE][+-]?[0-9]+)?\\b" | ||
| - constant.number: "\\b[0-9]+\\b" |
There was a problem hiding this comment.
Contrary to what the comment claims these are not copied from Python, they're missing support for underscores.
| skip: "\\\\." | ||
|
|
There was a problem hiding this comment.
| skip: "\\\\." | |
| skip: "\\\\." | |
| rules: | |
| - constant.specialChar: "\\\\." |
There was a problem hiding this comment.
Hi @Andriamanitra, thanks again for the detailed feedback! I've made all the suggested changes:
- Numbers now support underscores (e.g., 1_000_000)
- Added float comparison operators (<., >., <=., >=.)
- String escapes are now highlighted as special characters
- Added tuple syntax #(...) highlighting
- Attributes are now restricted to lowercase only
Andriamanitra
left a comment
There was a problem hiding this comment.
The last commit broke the whole thing by removing the required filetype:.
|
Sorry about that, fixed in the latest commit — restored the filetype: gleam field and also cleaned up the inline comments while I was at it. |
Andriamanitra
left a comment
There was a problem hiding this comment.
Everything seems to work fine now. The tuple highlighting looks a bit weird because the opening paren is colored but the closing one is not but I guess it's fine.
Maybe we could try it with a region (like in the - default:
start: "#\\("
end: "\\)"
limit-group: special
rules:
- constant.number: "\\b0b[01](_?[01])*\\b"
- constant.number: "\\b0o[0-7](_?[0-7])*\\b"
- constant.number: "\\b0x[0-9a-fA-F](_?[0-9a-fA-F])*\\b"
- constant.number: "\\b[0-9](_?[0-9])*(\\.[0-9](_?[0-9])*)?([eE][+-]?[0-9](_?[0-9])*)?\\b"
- constant.string:
start: '"'
end: '"'
skip: "\\\\."
rules:
- constant.specialChar: "\\\\."Yes, it duplicates rules, but we can't include the same type, because this would lead to recursion. BTW: |
Adds syntax highlighting for the Gleam programming language.
Supports: