Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add odin mode #5169

Merged
merged 5 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions demo/kitchen-sink/docs/odin.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import "core:fmt"

main :: proc() {
program := "+ + * 😃 - /"
accumulator := 0

for token in program {
switch token {
case '+': accumulator += 1
case '-': accumulator -= 1
case '*': accumulator *= 2
case '/': accumulator /= 2
case '😃': accumulator *= accumulator
case: // Ignore everything else
}
}

fmt.printf("The program \"%s\" calculates the value %d\n", program, accumulator)
}
1 change: 1 addition & 0 deletions src/ext/modelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ var supportedModes = {
Nunjucks: ["nunjucks|nunjs|nj|njk"],
ObjectiveC: ["m|mm"],
OCaml: ["ml|mli"],
Odin: ["odin"],
PartiQL: ["partiql|pql"],
Pascal: ["pas|p"],
Perl: ["pl|pm"],
Expand Down
52 changes: 52 additions & 0 deletions src/mode/odin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var OdinHighlightRules =
require("./odin_highlight_rules").OdinHighlightRules;
var MatchingBraceOutdent =
require("./matching_brace_outdent").MatchingBraceOutdent;
var CStyleFoldMode = require("./folding/cstyle").FoldMode;

var Mode = function () {
this.HighlightRules = OdinHighlightRules;
this.$outdent = new MatchingBraceOutdent();
this.foldingRules = new CStyleFoldMode();
this.$behaviour = this.$defaultBehaviour;
};
oop.inherits(Mode, TextMode);

(function () {
this.lineCommentStart = "//";
this.blockComment = { start: "/*", end: "*/" };

this.getNextLineIndent = function (state, line, tab) {
var indent = this.$getIndent(line);

var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
var tokens = tokenizedLine.tokens;

if (tokens.length && tokens[tokens.length - 1].type == "comment") {
return indent;

Check warning on line 28 in src/mode/odin.js

View check run for this annotation

Codecov / codecov/patch

src/mode/odin.js#L28

Added line #L28 was not covered by tests
}

if (state == "start") {
var match = line.match(/^.*[\{\(\[:]\s*$/);
if (match) {
indent += tab;
}
}

return indent;
}; //end getNextLineIndent

this.checkOutdent = function (state, line, input) {
return this.$outdent.checkOutdent(line, input);
};

this.autoOutdent = function (state, doc, row) {
this.$outdent.autoOutdent(doc, row);
};

this.$id = "ace/mode/odin";
}).call(Mode.prototype);

exports.Mode = Mode;
207 changes: 207 additions & 0 deletions src/mode/odin_highlight_rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
var oop = require("../lib/oop");
var DocCommentHighlightRules =
require("./doc_comment_highlight_rules").DocCommentHighlightRules;
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;

var OdinHighlightRules = function () {
var keywords =
"using|transmute|cast|distinct|opaque|where|" +
"struct|enum|union|bit_field|bit_set|" +
"if|when|else|do|switch|case|break|fallthrough|" +
"size_of|offset_of|type_info_if|typeid_of|type_of|align_of|" +
"or_return|or_else|inline|no_inline|" +
"import|package|foreign|defer|auto_cast|map|matrix|proc|" +
"for|continue|not_in|in";

const cartesian = (...a) =>
a
.reduce((a, b) => a.flatMap((d) => b.map((e) => [d, e].flat())))
.map((parts) => parts.join(""));

var builtinTypes = [
"int",
"uint",
"uintptr",
"typeid",
"rawptr",
"string",
"cstring",
"i8",
"u8",
"any",
"byte",
"rune",
"bool",
"b8",
"b16",
"b32",
"b64",
...cartesian(["i", "u"], ["16", "32", "64", "128"], ["", "le", "be"]),
...cartesian(["f"], ["16", "32", "64"], ["", "le", "be"]),
...cartesian(["complex"], ["32", "64", "128"]),
...cartesian(["quaternion"], ["64", "128", "256"])
].join("|");

var operators = [
"\\*",
"/",
"%",
"%%",
"<<",
">>",
"&",
"&~",
"\\+",
"\\-",
"~",
"\\|",
">",
"<",
"<=",
">=",
"==",
"!="
]
.concat(":")
.map((operator) => operator + "=")
.concat("=", ":=", "::", "->", "\\^", "&", ":")
.join("|");

var builtinFunctions = "new|cap|copy|panic|len|make|delete|append|free";
var builtinConstants = "nil|true|false";

var keywordMapper = this.createKeywordMapper(
{
keyword: keywords,
"constant.language": builtinConstants,
"support.function": builtinFunctions,
"support.type": builtinTypes
},
""
);

var stringEscapeRe =
"\\\\(?:[0-7]{3}|x\\h{2}|u{4}|U\\h{6}|[abfnrtv'\"\\\\])".replace(
/\\h/g,
"[a-fA-F\\d]"
);

this.$rules = {
start: [
{
token: "comment",
regex: /\/\/.*$/
},
DocCommentHighlightRules.getStartRule("doc-start"),
{
token: "comment.start", // multi line comment
regex: "\\/\\*",
next: "comment"
},
{
token: "string", // single line
regex: /"(?:[^"\\]|\\.)*?"/
},
{
token: "string", // raw
regex: "`",
next: "bqstring"
},
{
token: "support.constant",
regex: /#[a-z_]+/
},
{
token: "constant.numeric", // rune
regex:
"'(?:[^\\'\uD800-\uDBFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|" +
stringEscapeRe.replace('"', "") +
")'"
},
{
token: "constant.numeric", // hex
regex: "0[xX][0-9a-fA-F]+\\b"
},
{
token: "constant.numeric", // float
regex: "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
},
{
token: [
"entity.name.function",
"text",
"keyword.operator",
"text",
"keyword"
],
regex: "([a-zA-Z_$][a-zA-Z0-9_$]*)(\\s+)(::)(\\s+)(proc)\\b"
},
{
token: function (val) {
if (val[val.length - 1] == "(") {
return [
{
type: keywordMapper(val.slice(0, -1)) || "support.function",
value: val.slice(0, -1)
},
{
type: "paren.lparen",
value: val.slice(-1)
}
];
}

return keywordMapper(val) || "identifier";
},
regex: "[a-zA-Z_$][a-zA-Z0-9_$]*\\b\\(?"
},
{
token: "keyword.operator",
regex: operators
},
{
token: "punctuation.operator",
regex: "\\?|\\,|\\;|\\."
},
{
token: "paren.lparen",
regex: "[[({]"
},
{
token: "paren.rparen",
regex: "[\\])}]"
},
{
token: "text",
regex: "\\s+"
}
],
comment: [
{
token: "comment.end",
regex: "\\*\\/",
next: "start"
},
{
defaultToken: "comment"
}
],
bqstring: [
{
token: "string",
regex: "`",
next: "start"
},
{
defaultToken: "string"
}
]
};

this.embedRules(DocCommentHighlightRules, "doc-", [
DocCommentHighlightRules.getEndRule("start")
]);
};
oop.inherits(OdinHighlightRules, TextHighlightRules);

exports.OdinHighlightRules = OdinHighlightRules;
52 changes: 52 additions & 0 deletions src/mode/odin_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
if (typeof process !== "undefined") {
require("amd-loader");
}

"use strict";

var EditSession = require("../edit_session").EditSession;
var OdinMode = require("./odin").Mode;
var assert = require("../test/assertions");

module.exports = {
setUp : function() {
this.mode = new OdinMode();
},

"test: indent after opening function": function() {
assert.equal(" ", this.mode.getNextLineIndent("start", "main :: proc() {", " "));
},

"test: indent after opening block": function() {
assert.equal(" ", this.mode.getNextLineIndent("start", "{", " "));
},

"test: indent after opening array": function() {
assert.equal(" ", this.mode.getNextLineIndent("start", "foo := [", " "));
},

"test: indent after opening parentheses": function() {
assert.equal(" ", this.mode.getNextLineIndent("start", "foo := (", " "));
},

"test: indent after case:": function() {
assert.equal(" ", this.mode.getNextLineIndent("start", "case bar:", " "));
},

"test: auto outdent should indent the line with the same indent as the line with the matching opening brace" : function() {
var session = new EditSession([" main :: proc() {", " bla", " }"], this.mode);
this.mode.autoOutdent("start", session, 2);
assert.equal(" }", session.getLine(2));
},

"test: no auto outdent if no matching brace is found" : function() {
var session = new EditSession([" main :: proc()", " bla", " }"], this.mode);
this.mode.autoOutdent("start", session, 2);
assert.equal(" }", session.getLine(2));
}
};


if (typeof module !== "undefined" && module === require.main) {
require("asyncjs").test.testcase(module.exports).exec();

Check warning on line 51 in src/mode/odin_test.js

View check run for this annotation

Codecov / codecov/patch

src/mode/odin_test.js#L51

Added line #L51 was not covered by tests
}