-
Notifications
You must be signed in to change notification settings - Fork 3
/
mathjs.js
214 lines (182 loc) · 6.79 KB
/
mathjs.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { parse } from "mathjs"
/**
* Create mathjs syntax highlighting for CodeMirror
*
* TODO: this is using CodeMirror v5 functionality, upgrade this to v6
*
* @param {function} getWorkerState - A function that gets a mathjs instance properties for the parser.
* @param {function} getParserState - A function that returns a mathjs scope.
* @returns {object} An object with properties and methods for mathjs syntax highlighting and autocompletion in CodeMirror.
*/
export function mathjs(getWorkerState, getParserState) {
const workerState = getWorkerState()
const parserState = getParserState()
function wordRegexp(words) {
return new RegExp('^((' + words.join(')|(') + '))\\b')
}
const singleOperators = new RegExp("^[-+*/&|^~<>!%']")
const singleDelimiters = new RegExp('^[([{},:=;.?]')
const doubleOperators = new RegExp('^((==)|(!=)|(<=)|(>=)|(<<)|(>>)|(\\.[-+*/^]))')
const doubleDelimiters = new RegExp('^((!=)|(^\\|))')
const tripleDelimiters = new RegExp('^((>>>)|(<<<))')
const expressionEnd = new RegExp('^[\\])]')
const identifiers = new RegExp('^[_A-Za-z\xa1-\uffff][_A-Za-z0-9\xa1-\uffff]*')
const mathFunctions = workerState.functions
const mathPhysicalConstants = workerState.physicalConstants
const numberLiterals = workerState.numberLiterals
// generates a list of all valid units in mathjs
let listOfUnits = []
for (const unit in workerState.units) {
listOfUnits.push(...workerState.units[unit].map(prefixIndex => workerState.prefixes[prefixIndex] + unit))
}
// remove duplicates
listOfUnits = Array.from(new Set(listOfUnits))
const builtins = wordRegexp(mathFunctions)
const keywords = wordRegexp(['to', 'in', 'and', 'not', 'or', 'xor', 'mod'])
const units = wordRegexp(Array.from(new Set(listOfUnits)))
const physicalConstants = wordRegexp(mathPhysicalConstants)
// tokenizers
function tokenTranspose(stream, state) {
if (!stream.sol() && stream.peek() === "'") {
stream.next()
state.tokenize = tokenBase
return 'operator'
}
state.tokenize = tokenBase
return tokenBase(stream, state)
}
function tokenComment(stream, state) {
if (stream.match(/^.*#}/)) {
state.tokenize = tokenBase
return 'comment'
}
stream.skipToEnd()
return 'comment'
}
function tokenBase(stream, state) {
// whitespaces
if (stream.eatSpace()) return null
// Handle one line Comments
if (stream.match('#{')) {
state.tokenize = tokenComment
stream.skipToEnd()
return 'comment'
}
if (stream.match(/^#/)) {
stream.skipToEnd()
return 'comment'
}
// Handle Number Literals
if (stream.match(/^[0-9.+-]/, false)) {
if (stream.match(/^[+-]?0x[0-9a-fA-F]+[ij]?/)) {
stream.tokenize = tokenBase
return 'number'
}
if (stream.match(/^[+-]?\d*\.\d+([EeDd][+-]?\d+)?[ij]?/)) {
return 'number'
}
if (stream.match(/^[+-]?\d+([EeDd][+-]?\d+)?[ij]?/)) {
return 'number'
}
}
if (stream.match(wordRegexp(numberLiterals))) {
return 'number'
}
// Handle Strings
let m = stream.match(/^"(?:[^"]|"")*("|$)/) || stream.match(/^'(?:[^']|'')*('|$)/)
if (m) {
return m[1] ? 'string' : 'string error'
}
// Handle words
if (stream.match(keywords)) {
return 'keyword'
}
if (stream.match(builtins)) {
return 'builtin'
}
if (stream.match(physicalConstants)) {
return 'tag'
}
if (stream.match(units)) {
return 'attribute'
}
if (stream.match(identifiers)) {
return 'variable'
}
if (stream.match(singleOperators) || stream.match(doubleOperators)) {
return 'operator'
}
if (
stream.match(singleDelimiters) ||
stream.match(doubleDelimiters) ||
stream.match(tripleDelimiters)
) {
return null
}
if (stream.match(expressionEnd)) {
state.tokenize = tokenTranspose
return null
}
// Handle non-detected items
stream.next()
return 'error'
}
return {
name: 'mathjs',
startState: function () {
return {
tokenize: tokenBase
}
},
token: function (stream, state) {
const style = state.tokenize(stream, state)
if (style === 'number' || style === 'variable') {
state.tokenize = tokenTranspose
}
return style
},
languageData: {
commentTokens: { line: '#' },
autocomplete: (context) => myCompletions(context, parserState)
}
}
function myCompletions(context) {
let word = context.matchBefore(/\w*/)
if (word.from == word.to && !context.explicit) return null
let options = []
mathFunctions.forEach((func) => options.push({ label: func, type: 'function' }))
mathPhysicalConstants.forEach((constant) => options.push({ label: constant, type: 'constant' }))
numberLiterals.forEach((number) => options.push({ label: number, type: 'variable' }))
Object.keys(getParserState()).forEach(symbol => options.push({ label: symbol, type: 'property' }))
const matchingUnits = []
for (const prefix of workerState.prefixes) {
if (prefix.startsWith(word.text)) {
matchingUnits.push(prefix)
} else if (word.text.startsWith(prefix)) {
const unitKeyword = word.text.substring(prefix.length)
for (const n in workerState.units) {
const fullUnit = prefix + n
if (hasOwnPropertySafe(workerState.units, n)) {
if (
!matchingUnits.includes(fullUnit) &&
n.startsWith(unitKeyword) &&
listOfUnits.includes(fullUnit)
) {
matchingUnits.push(fullUnit)
}
}
}
}
}
matchingUnits.forEach((unit) => options.push({ label: unit, type: 'enum' }))
return {
from: word.from,
options
}
}
}
// helper function to safely check whether an object has a property
// copy from the function in object.js which is ES6
function hasOwnPropertySafe(object, property) {
return object && Object.hasOwnProperty.call(object, property)
}