-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExcel Formula.sublime-syntax
280 lines (257 loc) · 8.3 KB
/
Excel Formula.sublime-syntax
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
%YAML 1.2
---
name: Excel Formula
scope: source.sheet.excel
version: 2
extends: Packages/Spreadsheet Formula/Spreadsheet Formula (Basic).sublime-syntax
file_extensions:
- xls.txt
contexts:
expressions:
- meta_append: true
- match: \t
scope: invalid.illegal.sheet.excel
function-builtins-extra:
- meta_append: true
- match: \b({{function_builtins_excel}})(\()
captures:
1: meta.function-call.excel support.function.excel
2: meta.function-call.arguments.excel punctuation.section.arguments.begin.excel
push: function-call-body
sheet-reference:
- match: (?={{sheet}}!)
# - match: (?={{sheet}}[!|])
push: sheet-reference-body
sheet-reference-body:
- meta_scope: meta.reference.sheet
- match: \!
scope: punctuation.separator.sequence.sheet
pop: 1
# - match: \|
# scope: punctuation.separator.sequence.sheet
# push: sheet-reference-body
- match: \'
scope: punctuation.definition.annotation.begin.sheet
push:
- meta_content_scope: entity.name.struct.sheet
- match: \'\'
scope: constant.character.escape.sheet
# - match: \'(?=\|)
# scope: punctuation.definition.annotation.end.sheet
# push: sheet-reference-body
- match: \'(?=!)
scope: punctuation.definition.annotation.end.sheet
pop: 1
- match: '{{sheet}}'
scope: entity.name.struct.sheet
- include: immediately-pop
cell-or-range-reference:
- meta_append: true
# Push to dynamic context to isolate forced anonymity to just dynamic refs
- match: |-
(?x:(?= # next find a...
(?:@?(?:{{cell}}|{{rc}})) # cell followed by...
(?:
[#]?:(?:{{cell}}|{{rc}})[#] # maybe a # and second cell with #, OR
| [#]:(?:{{cell}}|{{rc}})[#]? # definitely a # and second cell maybe with #
)
))
push: dynamic-reference
# Cell range without dynamics (R1C1)
# See semi-shared references section in excel ref tests
# for full list of matches.
# (@?)R([1]?)C([1]?):R([1]?)C([1]?)
- match: ({{rc}})(:)({{rc}})(?!#)
scope: meta.reference.range.cell.sheet
captures:
1: storage.type.sheet
2: punctuation.separator.sequence.sheet
3: storage.type.sheet
push: after-reference
# Cell range without dynamics
# @A1:B2
- match: (@)(?:({{cell}})(:)({{cell}})|({{rc}})(:)({{rc}}))
scope: meta.reference.range.cell.excel
captures:
1: keyword.operator.reference.excel
2: storage.type.excel
3: punctuation.separator.sequence.excel
4: storage.type.excel
5: storage.type.excel
6: punctuation.separator.sequence.excel
7: storage.type.excel
push: after-reference
# Naked cell reference
# A1#
# @A1
# @A1#
- match: (@?)({{cell}}|{{rc}})(#?)(?![:#])
scope: meta.reference.cell.excel
captures:
1: keyword.operator.reference.excel
2: storage.type.excel
3: keyword.operator.reference.excel
push: after-reference
# Column to column
# @A:B
- match: (@?)(?:({{column}})(:)({{column}})|({{c_}})(:)({{c_}}))(?![0-9])
scope: meta.reference.range.column.excel
captures:
1: keyword.operator.reference.excel
2: storage.type.excel
3: punctuation.separator.sequence.excel
4: storage.type.excel
5: storage.type.excel
6: punctuation.separator.sequence.excel
7: storage.type.excel
push: after-reference
# Row to row
# @1:2
- match: (@)(?:({{row}})(:)({{row}})|({{r_}})(:)({{r_}}))
scope: meta.reference.range.row.excel
captures:
1: keyword.operator.reference.excel
2: storage.type.excel
3: punctuation.separator.sequence.excel
4: storage.type.excel
5: storage.type.excel
6: punctuation.separator.sequence.excel
7: storage.type.excel
push: after-reference
dynamic-reference:
- meta_prepend: true
# Zero-dimensional cell range w/ dynamics
# @A1#:A1#
# @A1:A1#
- match: (@?)({{cell}}|{{rc}})(#?)(:)(\2)(#)
scope: meta.reference.cell.excel
captures:
1: keyword.operator.reference.excel
2: storage.type.excel
3: keyword.operator.reference.excel
4: punctuation.separator.sequence.excel
5: storage.type.excel
6: keyword.operator.reference.excel
push: after-reference
# Cell range with dynamics
# @A1#:B2
# @A1#:B2#
- match: (@?)(?:({{cell}})(#)(:)({{cell}})|({{rc}})(#)(:)({{rc}}))(#?)
scope: meta.reference.range.cell.excel
captures:
1: keyword.operator.reference.excel
2: storage.type.excel
3: keyword.operator.reference.excel
4: punctuation.separator.sequence.excel
5: storage.type.excel
6: storage.type.excel
7: keyword.operator.reference.excel
8: punctuation.separator.sequence.excel
9: storage.type.excel
10: keyword.operator.reference.excel
push: after-reference
# Bail if nothing matches
- include: immediately-pop
after-reference:
- meta_prepend: true
# Match Excel's "space" intersection operator
- match: '[ ]{{before_reference}}'
scope: keyword.operator.set.excel
- match: '[ ]{{before_name}}'
scope: keyword.operator.set.excel
table-name-body:
- meta_prepend: true
- match: '[#@]'
scope: keyword.operator.reference.excel
###############################################################################
variables:
# https://support.microsoft.com/en-us/office/excel-functions-alphabetical-b3944572-255d-4efb-bb96-c6d90033e188
function_builtins_excel: |-
\b(?xi:
AGGREGATE | AMORDEGRC | AREAS | ARRAYTOTEXT |
BAHTTEXT | BESSELI | BESSELJ | BESSELK | BESSELY | BINOM\.DIST\.RANGE |
CALL | CUBEKPIMEMBER | CUBEMEMBER | CUBEMEMBERPROPERTY | CUBERANKEDMEMBER | CUBESET | CUBESETCOUNT | CUBEVALUE |
DBCS | DROP |
EUROCONVERT | EXPAND |
FILTERXML | FORECAST\.ETS | FORECAST\.ETS\.CONFINT | FORECAST\.ETS\.SEASONALITY | FORECAST\.ETS\.STAT |
INFO | ISOMITTED |
JIS |
NUMBERVALUE |
ODDFPRICE | ODDFYIELD | ODDLPRICE | ODDLYIELD |
PHONETIC |
REGISTER\.ID | RTD |
SHEET | SHEETS | SORTBY | STOCKHISTORY |
TAKE | TEXTAFTER | TEXTBEFORE | TEXTSPLIT | TOCOL | TODAY | TOROW |
VALUETOTEXT |
WEBSERVICE |
XMATCH
)\b
name: |-
(?x:
(?# my gut tells me this first class should be '[\w\\&&[^0-9]]+',
but that breaks R1C1 edge cases, and it's currently redundant.
It's supposed to prevent e.g. `4hello` from matching as a valid name,
but `4hello` doesn't match as a name regardless.)
[\w\\]+
[\w?\\]*
)
function: |-
(?x:
[\w\\&&[^0-9]]
[\w?.\\]*
)
table: |-
(?x:
[\w\\&&[^0-9]]
[\w.?\\]*
)
# TODO: Is there a cleaner more compact (and probably more efficient) way to
# do this? `'` are only legal in excel sheet names when padded on
# either end with a legal character matched by [^:*/?\[\]\\] and said
# `'` must come in pairs.
sheet_delimited: |-
(?x:
(?:'[^:*/?\[\]\\]+('')*[^:*/?\[\]\\]+')|
(?:'[^:*/?\[\]\\]+')
)
sheet_normal: '[\w&&[^0-9]]\w*'
sheet: (?:{{sheet_delimited}}|{{sheet_normal}})
# Dummy patterns for that sweet YAML syntax highlighting
# # Not exhaustive. The fact that I'm not testing combinations of spec chars complicates this...
Pattern that Excel will auto escape/delimit with `'`: '^\d+{{sheet_normal}}?'
# RC row/column offset format
r_: |-
(?xi:
R
(?:
\[[-\+]?[1-9][0-9]{0,6}\] |
[1-9][0-9]{0,6}
)
(?:
(?={{c_}})
| (?=C?(?!{{name}}))
)
)
c_: (?i:C(?:\[[-\+]?[1-9][0-9]{0,4}\]|[1-9][0-9]{0,4})(?!{{name}}))
rc: |-
(?xi:
{{r_}}{{c_}}|
{{r_}}C|
{{r_}}|
R{{c_}}|
{{c_}}
)
# Intersection Operator Variables
before_name: (?={{name}})
before_reference: |-
(?x:(?=
@? # Reference operator
(?:
{{cell}}
| (?:{{column}}|{{row}}):
(?:{{column}}|{{row}}|{{cell}})
| {{rc}}:
{{rc}}
| {{rc}}
)
))