Debian Codegraph @

codemirror-legacy-modes/mode/mathematica.js

raw
1 // used pattern building blocks
2 var Identifier = '[a-zA-Z\\$][a-zA-Z0-9\\$]*';
3 var pBase = "(?:\\d+)";
4 var pFloat = "(?:\\.\\d+|\\d+\\.\\d*|\\d+)";
5 var pFloatBase = "(?:\\.\\w+|\\w+\\.\\w*|\\w+)";
6 var pPrecision = "(?:`(?:`?"+pFloat+")?)";
7
8 // regular expressions
9 var reBaseForm = new RegExp('(?:'+pBase+'(?:\\^\\^'+pFloatBase+pPrecision+'?(?:\\*\\^[+-]?\\d+)?))');
10 var reFloatForm = new RegExp('(?:' + pFloat + pPrecision + '?(?:\\*\\^[+-]?\\d+)?)');
11 var reIdInContext = new RegExp('(?:`?)(?:' + Identifier + ')(?:`(?:' + Identifier + '))*(?:`?)');
12
13 function tokenBase(stream, state) {
14 var ch;
15
16 // get next character
17 ch = stream.next();
18
19 // string
20 if (ch === '"') {
21 state.tokenize = tokenString;
22 return state.tokenize(stream, state);
23 }
24
25 // comment
26 if (ch === '(') {
27 if (stream.eat('*')) {
28 state.commentLevel++;
29 state.tokenize = tokenComment;
30 return state.tokenize(stream, state);
31 }
32 }
33
34 // go back one character
35 stream.backUp(1);
36
37 // look for numbers
38 // Numbers in a baseform
39 if (stream.match(reBaseForm, true, false)) {
40 return 'number';
41 }
42
43 // Mathematica numbers. Floats (1.2, .2, 1.) can have optionally a precision (`float) or an accuracy definition
44 // (``float). Note: while 1.2` is possible 1.2`` is not. At the end an exponent (float*^+12) can follow.
45 if (stream.match(reFloatForm, true, false)) {
46 return 'number';
47 }
48
49 /* In[23] and Out[34] */
50 if (stream.match(/(?:In|Out)\[[0-9]*\]/, true, false)) {
51 return 'atom';
52 }
53
54 // usage
55 if (stream.match(/([a-zA-Z\$][a-zA-Z0-9\$]*(?:`[a-zA-Z0-9\$]+)*::usage)/, true, false)) {
56 return 'meta';
57 }
58
59 // message
60 if (stream.match(/([a-zA-Z\$][a-zA-Z0-9\$]*(?:`[a-zA-Z0-9\$]+)*::[a-zA-Z\$][a-zA-Z0-9\$]*):?/, true, false)) {
61 return 'string.special';
62 }
63
64 // this makes a look-ahead match for something like variable:{_Integer}
65 // the match is then forwarded to the mma-patterns tokenizer.
66 if (stream.match(/([a-zA-Z\$][a-zA-Z0-9\$]*\s*:)(?:(?:[a-zA-Z\$][a-zA-Z0-9\$]*)|(?:[^:=>~@\^\&\*\)\[\]'\?,\|])).*/, true, false)) {
67 return 'variableName.special';
68 }
69
70 // catch variables which are used together with Blank (_), BlankSequence (__) or BlankNullSequence (___)
71 // Cannot start with a number, but can have numbers at any other position. Examples
72 // blub__Integer, a1_, b34_Integer32
73 if (stream.match(/[a-zA-Z\$][a-zA-Z0-9\$]*_+[a-zA-Z\$][a-zA-Z0-9\$]*/, true, false)) {
74 return 'variableName.special';
75 }
76 if (stream.match(/[a-zA-Z\$][a-zA-Z0-9\$]*_+/, true, false)) {
77 return 'variableName.special';
78 }
79 if (stream.match(/_+[a-zA-Z\$][a-zA-Z0-9\$]*/, true, false)) {
80 return 'variableName.special';
81 }
82
83 // Named characters in Mathematica, like \[Gamma].
84 if (stream.match(/\\\[[a-zA-Z\$][a-zA-Z0-9\$]*\]/, true, false)) {
85 return 'character';
86 }
87
88 // Match all braces separately
89 if (stream.match(/(?:\[|\]|{|}|\(|\))/, true, false)) {
90 return 'bracket';
91 }
92
93 // Catch Slots (#, ##, #3, ##9 and the V10 named slots #name). I have never seen someone using more than one digit after #, so we match
94 // only one.
95 if (stream.match(/(?:#[a-zA-Z\$][a-zA-Z0-9\$]*|#+[0-9]?)/, true, false)) {
96 return 'variableName.constant';
97 }
98
99 // Literals like variables, keywords, functions
100 if (stream.match(reIdInContext, true, false)) {
101 return 'keyword';
102 }
103
104 // operators. Note that operators like @@ or /; are matched separately for each symbol.
105 if (stream.match(/(?:\\|\+|\-|\*|\/|,|;|\.|:|@|~|=|>|<|&|\||_|`|'|\^|\?|!|%)/, true, false)) {
106 return 'operator';
107 }
108
109 // everything else is an error
110 stream.next(); // advance the stream.
111 return 'error';
112 }
113
114 function tokenString(stream, state) {
115 var next, end = false, escaped = false;
116 while ((next = stream.next()) != null) {
117 if (next === '"' && !escaped) {
118 end = true;
119 break;
120 }
121 escaped = !escaped && next === '\\';
122 }
123 if (end && !escaped) {
124 state.tokenize = tokenBase;
125 }
126 return 'string';
127 };
128
129 function tokenComment(stream, state) {
130 var prev, next;
131 while(state.commentLevel > 0 && (next = stream.next()) != null) {
132 if (prev === '(' && next === '*') state.commentLevel++;
133 if (prev === '*' && next === ')') state.commentLevel--;
134 prev = next;
135 }
136 if (state.commentLevel <= 0) {
137 state.tokenize = tokenBase;
138 }
139 return 'comment';
140 }
141
142 export const mathematica = {
143 name: "mathematica",
144 startState: function() {return {tokenize: tokenBase, commentLevel: 0};},
145 token: function(stream, state) {
146 if (stream.eatSpace()) return null;
147 return state.tokenize(stream, state);
148 },
149 languageData: {
150 commentTokens: {block: {open: "(*", close: "*)"}}
151 }
152 }
153