Debian Codegraph @

codemirror-legacy-modes/mode/yacas.js

raw
1 function words(str) {
2 var obj = {}, words = str.split(" ");
3 for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
4 return obj;
5 }
6
7 var bodiedOps = words("Assert BackQuote D Defun Deriv For ForEach FromFile " +
8 "FromString Function Integrate InverseTaylor Limit " +
9 "LocalSymbols Macro MacroRule MacroRulePattern " +
10 "NIntegrate Rule RulePattern Subst TD TExplicitSum " +
11 "TSum Taylor Taylor1 Taylor2 Taylor3 ToFile " +
12 "ToStdout ToString TraceRule Until While");
13
14 // patterns
15 var pFloatForm = "(?:(?:\\.\\d+|\\d+\\.\\d*|\\d+)(?:[eE][+-]?\\d+)?)";
16 var pIdentifier = "(?:[a-zA-Z\\$'][a-zA-Z0-9\\$']*)";
17
18 // regular expressions
19 var reFloatForm = new RegExp(pFloatForm);
20 var reIdentifier = new RegExp(pIdentifier);
21 var rePattern = new RegExp(pIdentifier + "?_" + pIdentifier);
22 var reFunctionLike = new RegExp(pIdentifier + "\\s*\\(");
23
24 function tokenBase(stream, state) {
25 var ch;
26
27 // get next character
28 ch = stream.next();
29
30 // string
31 if (ch === '"') {
32 state.tokenize = tokenString;
33 return state.tokenize(stream, state);
34 }
35
36 // comment
37 if (ch === '/') {
38 if (stream.eat('*')) {
39 state.tokenize = tokenComment;
40 return state.tokenize(stream, state);
41 }
42 if (stream.eat("/")) {
43 stream.skipToEnd();
44 return "comment";
45 }
46 }
47
48 // go back one character
49 stream.backUp(1);
50
51 // update scope info
52 var m = stream.match(/^(\w+)\s*\(/, false);
53 if (m !== null && bodiedOps.hasOwnProperty(m[1]))
54 state.scopes.push('bodied');
55
56 var scope = currentScope(state);
57
58 if (scope === 'bodied' && ch === '[')
59 state.scopes.pop();
60
61 if (ch === '[' || ch === '{' || ch === '(')
62 state.scopes.push(ch);
63
64 scope = currentScope(state);
65
66 if (scope === '[' && ch === ']' ||
67 scope === '{' && ch === '}' ||
68 scope === '(' && ch === ')')
69 state.scopes.pop();
70
71 if (ch === ';') {
72 while (scope === 'bodied') {
73 state.scopes.pop();
74 scope = currentScope(state);
75 }
76 }
77
78 // look for ordered rules
79 if (stream.match(/\d+ *#/, true, false)) {
80 return 'qualifier';
81 }
82
83 // look for numbers
84 if (stream.match(reFloatForm, true, false)) {
85 return 'number';
86 }
87
88 // look for placeholders
89 if (stream.match(rePattern, true, false)) {
90 return 'variableName.special';
91 }
92
93 // match all braces separately
94 if (stream.match(/(?:\[|\]|{|}|\(|\))/, true, false)) {
95 return 'bracket';
96 }
97
98 // literals looking like function calls
99 if (stream.match(reFunctionLike, true, false)) {
100 stream.backUp(1);
101 return 'variableName.function';
102 }
103
104 // all other identifiers
105 if (stream.match(reIdentifier, true, false)) {
106 return 'variable';
107 }
108
109 // operators; note that operators like @@ or /; are matched separately for each symbol.
110 if (stream.match(/(?:\\|\+|\-|\*|\/|,|;|\.|:|@|~|=|>|<|&|\||_|`|'|\^|\?|!|%|#)/, true, false)) {
111 return 'operator';
112 }
113
114 // everything else is an error
115 return 'error';
116 }
117
118 function tokenString(stream, state) {
119 var next, end = false, escaped = false;
120 while ((next = stream.next()) != null) {
121 if (next === '"' && !escaped) {
122 end = true;
123 break;
124 }
125 escaped = !escaped && next === '\\';
126 }
127 if (end && !escaped) {
128 state.tokenize = tokenBase;
129 }
130 return 'string';
131 };
132
133 function tokenComment(stream, state) {
134 var prev, next;
135 while((next = stream.next()) != null) {
136 if (prev === '*' && next === '/') {
137 state.tokenize = tokenBase;
138 break;
139 }
140 prev = next;
141 }
142 return 'comment';
143 }
144
145 function currentScope(state) {
146 var scope = null;
147 if (state.scopes.length > 0)
148 scope = state.scopes[state.scopes.length - 1];
149 return scope;
150 }
151
152 export const yacas = {
153 name: "yacas",
154 startState: function() {
155 return {
156 tokenize: tokenBase,
157 scopes: []
158 };
159 },
160 token: function(stream, state) {
161 if (stream.eatSpace()) return null;
162 return state.tokenize(stream, state);
163 },
164 indent: function(state, textAfter, cx) {
165 if (state.tokenize !== tokenBase && state.tokenize !== null)
166 return null;
167
168 var delta = 0;
169 if (textAfter === ']' || textAfter === '];' ||
170 textAfter === '}' || textAfter === '};' ||
171 textAfter === ');')
172 delta = -1;
173
174 return (state.scopes.length + delta) * cx.unit;
175 },
176
177 languageData: {
178 electricInput: /[{}\[\]()\;]/,
179 commentTokens: {line: "//", block: {open: "/*", close: "*/"}}
180 }
181 };