Skip to content

Commit 9692aa1

Browse files
authored
[julia mode] Improve macros and symbols matching
1 parent 49a7fc4 commit 9692aa1

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

mode/julia/julia.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,29 @@
1212
"use strict";
1313

1414
CodeMirror.defineMode("julia", function(config, parserConf) {
15-
function wordRegexp(words, end) {
15+
function wordRegexp(words, end, pre) {
16+
if (typeof pre === "undefined") { pre = ""; }
1617
if (typeof end === "undefined") { end = "\\b"; }
17-
return new RegExp("^((" + words.join(")|(") + "))" + end);
18+
return new RegExp("^" + pre + "((" + words.join(")|(") + "))" + end);
1819
}
1920

2021
var octChar = "\\\\[0-7]{1,3}";
2122
var hexChar = "\\\\x[A-Fa-f0-9]{1,2}";
2223
var sChar = "\\\\[abefnrtv0%?'\"\\\\]";
2324
var uChar = "([^\\u0027\\u005C\\uD800-\\uDFFF]|[\\uD800-\\uDFFF][\\uDC00-\\uDFFF])";
2425

26+
var asciiOperatorsList = [
27+
"[<>]:", "[<>=]=", "<<=?", ">>>?=?", "=>", "--?>", "<--[->]?", "\\/\\/",
28+
"\\.{2,3}", "[\\.\\\\%*+\\-<>!\\/^|&]=?", "\\?", "\\$", "~", ":"
29+
];
2530
var operators = parserConf.operators || wordRegexp([
26-
"[<>]:", "[<>=]=", "<<=?", ">>>?=?", "=>", "->", "\\/\\/",
27-
"[\\\\%*+\\-<>!=\\/^|&\\u00F7\\u22BB]=?", "\\?", "\\$", "~", ":",
28-
"\\u00D7", "\\u2208", "\\u2209", "\\u220B", "\\u220C", "\\u2218",
29-
"\\u221A", "\\u221B", "\\u2229", "\\u222A", "\\u2260", "\\u2264",
30-
"\\u2265", "\\u2286", "\\u2288", "\\u228A", "\\u22C5",
31-
"\\b(in|isa)\\b(?!\.?\\()"], "");
31+
"[<>]:", "[<>=]=", "<<=?", ">>>?=?", "=>", "--?>", "<--[->]?", "\\/\\/",
32+
"[\\\\%*+\\-<>!\\/^|&\\u00F7\\u22BB]=?", "\\?", "\\$", "~", ":",
33+
"\\u00D7", "\\u2208", "\\u2209", "\\u220B", "\\u220C", "\\u2218",
34+
"\\u221A", "\\u221B", "\\u2229", "\\u222A", "\\u2260", "\\u2264",
35+
"\\u2265", "\\u2286", "\\u2288", "\\u228A", "\\u22C5",
36+
"\\b(in|isa)\\b(?!\.?\\()"
37+
], "");
3238
var delimiters = parserConf.delimiters || /^[;,()[\]{}]/;
3339
var identifiers = parserConf.identifiers ||
3440
/^[_A-Za-z\u00A1-\u2217\u2219-\uFFFF][\w\u00A1-\u2217\u2219-\uFFFF]*!*/;
@@ -57,10 +63,13 @@ CodeMirror.defineMode("julia", function(config, parserConf) {
5763
var keywords = wordRegexp(keywordsList);
5864
var builtins = wordRegexp(builtinsList);
5965

60-
var macro = /^@[_A-Za-z][\w]*/;
66+
var macro = /^@[_A-Za-z\u00A1-\uFFFF][\w\u00A1-\uFFFF]*!*/;
6167
var symbol = /^:[_A-Za-z\u00A1-\uFFFF][\w\u00A1-\uFFFF]*!*/;
6268
var stringPrefixes = /^(`|([_A-Za-z\u00A1-\uFFFF]*"("")?))/;
6369

70+
var macroOperators = wordRegexp(asciiOperatorsList, "", "@");
71+
var symbolOperators = wordRegexp(asciiOperatorsList, "", ":");
72+
6473
function inArray(state) {
6574
return (state.nestedArrays > 0);
6675
}
@@ -165,8 +174,7 @@ CodeMirror.defineMode("julia", function(config, parserConf) {
165174
}
166175

167176
// Handle symbols
168-
if (!leavingExpr && stream.match(symbol) ||
169-
stream.match(/:([<>]:|<<=?|>>>?=?|->|\/\/|\.{2,3}|[\.\\%*+\-<>!\/^|&]=?|[~\?\$])/)) {
177+
if (!leavingExpr && (stream.match(symbol) || stream.match(symbolOperators))) {
170178
return "builtin";
171179
}
172180

@@ -212,7 +220,7 @@ CodeMirror.defineMode("julia", function(config, parserConf) {
212220
return state.tokenize(stream, state);
213221
}
214222

215-
if (stream.match(macro)) {
223+
if (stream.match(macro) || stream.match(macroOperators)) {
216224
return "meta";
217225
}
218226

0 commit comments

Comments
 (0)