PrismJS / prism

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

Odin Programming Language #3204

Closed gingerBill closed 2 years ago

gingerBill commented 2 years ago

Language Odin is a general-purpose programming language with distinct typing, built for high performance, modern systems, and built-in data-oriented data types. The Odin Programming Language, the C alternative for the joy of programming. The Data-Oriented Language for Sane Software Development.

Additional resources https://odin-lang.org/ https://odin-lang.org/docs/overview/ https://github.com/odin-lang/Odin

Syntax:

Prism.languages.odin = Prism.languages.extend('clike', {
    'string': {
        pattern: /(["'`])(?:\\[\s\S]|(?!\1)[^\\])*\1/,
        greedy: true
    },
    'keyword': /\b(?:asm|auto_cast|bit_set|break|case|cast|context|continue|defer|distinct|do|dynamic|else|enum|fallthrough|for|foreign|if|import|in|map|matrix|not_in|or_else|or_return|package|proc|return|struct|switch|transmute|typeid|union|using|when|where)\b/,
    'boolean': /\b(?:_|false|true|nil)\b/,
    'number': /(?:\b0[xh][0-9a-fA-F_]+|(?:\b0b[01_])|(?:\b0o[0-7_])|(?:\b0d[0-9_])|(?:\b0z[0-9abAB_])|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[-+]?\d+)?)[ijk]?/i,
    'operator': /[+-\/%~!=<>]=?|%%=?|---|\?|\|(?:=|\|=?)?|&(?:=|&=?|\~=?)?|>(?:>=?|=)?|<(?:<=?|=)?|:\s*=|:\s*:|\.\.[=<]?|->|[@#$]/,
    'builtin': /\b(?:abs|align_of|cap|clamp|complex|conj|expand_to_tuple|hadamard_product|imag|jmag|kmag|len|matrix_flatten|max|min|offset_of|offset_of_by_string|outer_product|quaternion|real|size_of|soa_unzip|soa_zip|swizzle|transpose|type_info_of|type_of|typeid_of|any|b(?:8|16|32|64)|bool|byte|c?string|complex(?:32|64|128)|f(?:16|32|64)(?:le|be)?|quaternion(?:64|128|256)|rawptr|rune|[ui](?:8|16|32|64|128)(?:le|be)?|u?int(?:ptr)?\b/
});
delete Prism.languages.odin['class-name'];
RunDevelopment commented 2 years ago

Hi @gingerBill!

The language definition you suggested looks quite good. Do you want to make a PR?

edukisto commented 2 years ago

Hi, @gingerBill and @RunDevelopment!

Just a few notes about the code.

  1. 'boolean'. Is nil a Boolean? And I don’t get what the Boolean _ is.

  2. 'operator':

    • [+-\/%~!=<>]=? captures ,= and .=, but these are not operators. [+-\/] (or [-+,./]) should be replaced with [-+/];

    • since [+-\/%~!=<>]=? captures all the hyphens, --- and -> find nothing;

    • the pattern splits %%, %%=, ++, --, ---, ->, .., ..<, ..=, <<, <<=, >>, and >>=;

    • the pattern doesn’t find (, ), *, *=, :, ;, [, ], ^, {, };

    • reserved/forbidden operators (++ and --) are not highlighted in a special way.

  3. The Odin strings are more complex than shown here. "Strings" and 'characters' support escaped characters, but don’t allow to escape newlines. `Raw strings` don’t support escaping. 'Characters' are escaped sequences themselves.

In addition, there are some ambiguities in the documentation.

  1. Although not documented, octal 4‑bit characters require the \0 prefix.
  2. 24‑bit Unicode characters (both "\UNNNNNN" and '\UNNNNNN') don’t work. However, it’s possible that I’ve misapplied them.
  3. matrix is a keyword, but it is not classified as a keyword in the docs.

Do you mind if I try to make a PR?

RunDevelopment commented 2 years ago
  1. 'boolean'. Is nil a Boolean? And I don’t get what the Boolean _ is.

Some languages have an extended boolean algebra that includes the bottom type (null, nil, undefined, etc).

And I don’t get what the Boolean _ is.

That's a good point. _ is something like a discard, right? It should probably be a keyword.

  • [+-\/%~!=<>]=? captures ,= and .=, but these are not operators. [+-\/] (or [-+,./]) should be replaced with [-+/];

Very good find. Our ESLint rule regexp/no-obscure-range would have also caught this.

  • since [+-\/%~!=<>]=? captures all the hyphens, --- and -> find nothing;
  • the pattern splits %%, %%=, ++, --, ---, ->, .., ..<, ..=, <<, <<=, >>, and >>=;

Good point. Seems like operators still need some polish.

  • the pattern doesn’t find (, ), *, *=, :, ;, [, ], ^, {, };

Most of these are highlighted as punctuation.

  • reserved/forbidden operators (++ and --) are not highlighted in a special way.

Prism grammars generally don't care about invalid syntax, it's pretty much undefined behavior. I would suggest to just highlight them as regular operators. Prism isn't a parser or compiler, we don't valid code, we just produce some pretty colors.

  1. The Odin strings are more complex than shown here. "Strings" support escaped characters, but don’t allow to escape newlines. `Raw strings` don’t support escaping. 'Characters' are escaped sequences themselves.

String are always difficult in modern languages. I'm just thankful that there is no string interpolation syntax to make this even more difficult.

Do you mind if I try to make a PR?

I'm looking forward to it. Thank you!