PrismJS / prism

Lightweight, robust, elegant syntax highlighting.
https://prismjs.com
MIT License
12.19k stars 1.29k forks source link

Add support for Gleam #3573

Open aslilac opened 1 year ago

aslilac commented 1 year ago

Language

Gleam is a statically typed programming language that can compile to JavaScript and Erlang

import gleam/io

fn main() {
  io.println("hello, computer!")
}

Additional resources

Website: https://gleam.run Source: https://github.com/gleam-lang/gleam Tree sitter grammar: https://github.com/gleam-lang/tree-sitter-gleam VSCode extension (w/ TextMate grammar): https://github.com/gleam-lang/vscode-gleam

It's also supported by Github's linguist (which just uses the VSCode/TextMate grammar)

Nicd commented 1 year ago

Here's a quick WIP I whipped up:

Prism.languages.gleam = {
  doc: {
    pattern: /\/\/\/\/?.*/,
    greedy: true,
  },
  comment: {
    pattern: /\/\/.*/,
    greedy: true,
  },
  keyword:
    /\b(use|case|if|external|fn|import|let|assert|try|pub|type|opaque|const|todo)\b/,
  operator: {
    pattern:
      /(<-|->|\|>|<>|\.\.|<=\.?|>=\.?|==\.?|!=\.?|<\.?|>\.?|&&|\|\||\+\.?|-\.?|\/\.?|\*\.?|%\.?|=)/,
    greedy: true,
  },
  string: {
    pattern: /"(?:\\(?:\r\n|[\s\S])|(?!")[^\\\r\n])*"/,
    greedy: true,
    inside: {
      punctuation: /\\./,
    },
  },
  punctuation: /[.\\:,]/,
  number:
    /\b(?:0b[0-1]+|0o[0-7]+|[[:digit:]][[:digit:]_]*(\\.[[:digit:]]*)?|0x[[:xdigit:]]+)\b/,
  boolean: /\b(?:True|False)\b/,
};

I'll need to test on some fuller Gleam example, though.

Nicd commented 1 year ago

Here is an updated version of the grammar, though it accepts uppercase characters in function names which Gleam doesn't (need to fix): pastebin.

I see PrismJS isn't accepting PRs at the moment. I'll still fix this up and use it on my pastebin at https://nicd.gitlab.io/t/ in the meantime, so it'll be easy to take into use once v2 is out and it's accepting PRs again.

aslilac commented 1 year ago

For anyone that finds theirself here in the mean time, I've published a slightly modified version of the grammar that @Nicd came up with to npm as prismjs-gleam. You can use it via unpkg or whatever npm-CDN you prefer.

Source