-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
mathematica.js
143 lines (130 loc) · 3.78 KB
/
mathematica.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Language: Wolfram Language
Description: The Wolfram Language is the programming language used in Wolfram Mathematica, a modern technical computing system spanning most areas of technical computing.
Authors: Patrick Scheibe <patrick@halirutan.de>, Robert Jacobson <robertjacobson@acm.org>
Website: https://www.wolfram.com/mathematica/
Category: scientific
*/
/*
The ./lib/mathematica.js file was created with Wolfram Mathematica 12.1.1 under Linux
with the following code:
$outputFile="/path/to/highlight.js/src/languages/lib/mathematica.js";
getStrippedContextNames[context_String]:=Block[
{
$ContextPath={context}
},
Union[Names@RegularExpression@StringJoin[context,"\\$?[A-Z]\\w*"]]
];
$symbols=getStrippedContextNames["System`"];
Export[$outputFile,
With[
{
quoted="\""<>#<>"\""&/@$symbols
},
StringTemplate["export const SYSTEM_SYMBOLS = [\n`syms`\n];\n"][
<|"syms"->StringRiffle[Map[" " <> #&,quoted],",\n"]|>
]
],"String"]
*/
import * as Mathematica from './lib/mathematica.js';
/** @type LanguageFn */
export default function(hljs) {
const regex = hljs.regex;
/*
This rather scary looking matching of Mathematica numbers is carefully explained by Robert Jacobson here:
https://wltools.github.io/LanguageSpec/Specification/Syntax/Number-representations/
*/
const BASE_RE = /([2-9]|[1-2]\d|[3][0-5])\^\^/;
const BASE_DIGITS_RE = /(\w*\.\w+|\w+\.\w*|\w+)/;
const NUMBER_RE = /(\d*\.\d+|\d+\.\d*|\d+)/;
const BASE_NUMBER_RE = regex.either(regex.concat(BASE_RE, BASE_DIGITS_RE), NUMBER_RE);
const ACCURACY_RE = /``[+-]?(\d*\.\d+|\d+\.\d*|\d+)/;
const PRECISION_RE = /`([+-]?(\d*\.\d+|\d+\.\d*|\d+))?/;
const APPROXIMATE_NUMBER_RE = regex.either(ACCURACY_RE, PRECISION_RE);
const SCIENTIFIC_NOTATION_RE = /\*\^[+-]?\d+/;
const MATHEMATICA_NUMBER_RE = regex.concat(
BASE_NUMBER_RE,
regex.optional(APPROXIMATE_NUMBER_RE),
regex.optional(SCIENTIFIC_NOTATION_RE)
);
const NUMBERS = {
className: 'number',
relevance: 0,
begin: MATHEMATICA_NUMBER_RE
};
const SYMBOL_RE = /[a-zA-Z$][a-zA-Z0-9$]*/;
const SYSTEM_SYMBOLS_SET = new Set(Mathematica.SYSTEM_SYMBOLS);
/** @type {Mode} */
const SYMBOLS = { variants: [
{
className: 'builtin-symbol',
begin: SYMBOL_RE,
// for performance out of fear of regex.either(...Mathematica.SYSTEM_SYMBOLS)
"on:begin": (match, response) => {
if (!SYSTEM_SYMBOLS_SET.has(match[0])) response.ignoreMatch();
}
},
{
className: 'symbol',
relevance: 0,
begin: SYMBOL_RE
}
] };
const NAMED_CHARACTER = {
className: 'named-character',
begin: /\\\[[$a-zA-Z][$a-zA-Z0-9]+\]/
};
const OPERATORS = {
className: 'operator',
relevance: 0,
begin: /[+\-*/,;.:@~=><&|_`'^?!%]+/
};
const PATTERNS = {
className: 'pattern',
relevance: 0,
begin: /([a-zA-Z$][a-zA-Z0-9$]*)?_+([a-zA-Z$][a-zA-Z0-9$]*)?/
};
const SLOTS = {
className: 'slot',
relevance: 0,
begin: /#[a-zA-Z$][a-zA-Z0-9$]*|#+[0-9]?/
};
const BRACES = {
className: 'brace',
relevance: 0,
begin: /[[\](){}]/
};
const MESSAGES = {
className: 'message-name',
relevance: 0,
begin: regex.concat("::", SYMBOL_RE)
};
return {
name: 'Mathematica',
aliases: [
'mma',
'wl'
],
classNameAliases: {
brace: 'punctuation',
pattern: 'type',
slot: 'type',
symbol: 'variable',
'named-character': 'variable',
'builtin-symbol': 'built_in',
'message-name': 'string'
},
contains: [
hljs.COMMENT(/\(\*/, /\*\)/, { contains: [ 'self' ] }),
PATTERNS,
SLOTS,
MESSAGES,
SYMBOLS,
NAMED_CHARACTER,
hljs.QUOTE_STRING_MODE,
NUMBERS,
OPERATORS,
BRACES
]
};
}