Skip to content

Commit 9cb5c7c

Browse files
committed
Support numeric literal in field/getter/setter/method declarations
Fixes #604
1 parent 9ffe69a commit 9cb5c7c

6 files changed

+415
-90
lines changed

TypeScript.YAML-tmLanguage

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,23 @@ variables:
1414
constantIdentifier: '[[:upper:]][_$[:digit:][:upper:]]*'
1515
quotedStrings: (\'([^\'\\]|\\\'|\\)*\')|(\"([^\"\\]|\\\"|\\)*\")
1616
nonIdentifierPropertyName: '{{quotedStrings}}|(\[([^\[\]]|\[[^\[\]]*\])+\])'
17-
propertyName: (({{identifier}})|{{nonIdentifierPropertyName}})
17+
hexNumber: \b(?<!\$)0(x|X)[0-9a-fA-F][0-9a-fA-F_]*\b(?!\$)
18+
binaryNumber: \b(?<!\$)0(b|B)[01][01_]*\b(?!\$)
19+
octalNumber: \b(?<!\$)0(o|O)?[0-7][0-7_]*\b(?!\$)
20+
decimalNumber: |-
21+
(?<!\$)(?:
22+
(?:\b[0-9][0-9_]*(\.)[0-9][0-9_]*[eE][+-]?[0-9][0-9_]*\b)| # 1.1E+3
23+
(?:\b[0-9][0-9_]*(\.)[eE][+-]?[0-9][0-9_]*\b)| # 1.E+3
24+
(?:\B(\.)[0-9][0-9_]*[eE][+-]?[0-9][0-9_]*\b)| # .1E+3
25+
(?:\b[0-9][0-9_]*[eE][+-]?[0-9][0-9_]*\b)| # 1E+3
26+
(?:\b[0-9][0-9_]*(\.)[0-9][0-9_]*\b)| # 1.1
27+
(?:\b[0-9][0-9_]*(\.)\B)| # 1.
28+
(?:\B(\.)[0-9][0-9_]*\b)| # .1
29+
(?:\b[0-9][0-9_]*\b(?!\.)) # 1
30+
)(?!\$)
31+
anyNumber: ({{hexNumber}})|({{binaryNumber}})|({{octalNumber}})|({{decimalNumber}})
32+
# any use of property name needs to ignore line breaks in regular expression because of decimal number regex (maintained for readability)
33+
propertyName: ({{anyNumber}}|({{identifier}})|{{nonIdentifierPropertyName}})
1834
constantVar: ({{constantIdentifier}})(?![_$[:alnum:]])
1935
endOfStatement: ';|\babstract\b|\basync\b|\bclass\b|\bconst\b|\bdeclare\b|\benum\b|\bexport\b|\bfunction\b|\bimport\b|\binterface\b|\blet\b|\bmodule\b|\bnamespace\b|\breturn\b|\btype\b|\bvar\b'
2036
nonPropertyLookBehind: '[^\._$[:alnum:]]'
@@ -269,7 +285,7 @@ repository:
269285
object-binding-element:
270286
patterns:
271287
- include: '#comment'
272-
- begin: (?={{propertyName}}\s*(:))
288+
- begin: (?x)(?={{propertyName}}\s*(:))
273289
end: (?=,|\})
274290
patterns:
275291
- include: '#object-binding-element-propertyName'
@@ -280,13 +296,14 @@ repository:
280296
- include: '#punctuation-comma'
281297

282298
object-binding-element-propertyName:
283-
begin: (?={{propertyName}}\s*(:))
299+
begin: (?x)(?={{propertyName}}\s*(:))
284300
end: (:)
285301
endCaptures:
286302
'0': { name: punctuation.destructuring.ts }
287303
patterns:
288304
- include: '#string'
289305
- include: '#array-literal'
306+
- include: '#numeric-literal'
290307
- name: variable.object.property.ts
291308
match: ({{identifier}})
292309

@@ -374,7 +391,7 @@ repository:
374391
parameter-object-binding-element:
375392
patterns:
376393
- include: '#comment'
377-
- begin: (?={{propertyName}}\s*(:))
394+
- begin: (?x)(?={{propertyName}}\s*(:))
378395
end: (?=,|\})
379396
patterns:
380397
- include: '#object-binding-element-propertyName'
@@ -423,18 +440,19 @@ repository:
423440

424441
field-declaration:
425442
name: meta.field.declaration.ts
426-
begin: (?<!\()(?:{{startOfIdentifier}}(readonly)\s+)?(?={{propertyName}}\s*(\?\s*)?(=|:))
443+
begin: (?x)(?<!\()(?:{{startOfIdentifier}}(readonly)\s+)?(?={{propertyName}}\s*(\?\s*)?(=|:))
427444
beginCaptures:
428445
'1': { name: storage.modifier.ts }
429-
end: (?=\}|;|,|$|(^(?!{{propertyName}}\s*(\?\s*)?(=|:))))|(?<=\})
446+
end: (?x)(?=\}|;|,|$|(^(?!{{propertyName}}\s*(\?\s*)?(=|:))))|(?<=\})
430447
patterns:
431448
- include: '#variable-initializer'
432-
- begin: (?={{propertyName}}\s*(\?\s*)?(=|:))
433-
end: (?=[};,=]|$|(^(?!{{propertyName}}\s*(\?\s*)?(=|:))))|(?<=\})
449+
- begin: (?x)(?={{propertyName}}\s*(\?\s*)?(=|:))
450+
end: (?x)(?=[};,=]|$|(^(?!{{propertyName}}\s*(\?\s*)?(=|:))))|(?<=\})
434451
patterns:
435452
- include: '#type-annotation'
436453
- include: '#string'
437454
- include: '#array-literal'
455+
- include: '#numeric-literal'
438456
- include: '#comment'
439457
# function assignment |
440458
# typeannotation is fn type: < | () | (... | (param: | (param, | (param? | (param= | (param) =>
@@ -511,7 +529,7 @@ repository:
511529
patterns:
512530
# method, accessor
513531
- name: meta.method.declaration.ts
514-
begin: '{{startOfIdentifier}}(?:\b(public|private|protected)\s+)?(?:\b(abstract)\s+)?(?:\b(async)\s+)?(?:\b(get|set)\s+)(?:(\*)\s*)?(?=({{propertyName}}\s*(\??))\s*[\(\<])'
532+
begin: '(?x){{startOfIdentifier}}(?:\b(public|private|protected)\s+)?(?:\b(abstract)\s+)?(?:\b(async)\s+)?(?:\b(get|set)\s+)(?:(\*)\s*)?(?=({{propertyName}}\s*(\??))\s*[\(\<])'
515533
beginCaptures:
516534
'1': { name: storage.modifier.ts } # captures keyword (public or private or protected)
517535
'2': { name: storage.modifier.ts } # captures keyword (abstract)
@@ -524,7 +542,7 @@ repository:
524542
- include: '#function-body'
525543
# new, constructor or call signature
526544
- name: meta.method.declaration.ts
527-
begin: '{{startOfIdentifier}}(?:\b(public|private|protected)\s+)?(?:\b(abstract)\s+)?(?:\b(async)\s+)?(?:(?:\b(?:(new)|(constructor))\b(?!:){{endOfIdentifier}})|(?:(\*)\s*)?(?=({{propertyName}}\s*(\??))?\s*[\(\<]))'
545+
begin: '(?x){{startOfIdentifier}}(?:\b(public|private|protected)\s+)?(?:\b(abstract)\s+)?(?:\b(async)\s+)?(?:(?:\b(?:(new)|(constructor))\b(?!:){{endOfIdentifier}})|(?:(\*)\s*)?(?=({{propertyName}}\s*(\??))?\s*[\(\<]))'
528546
beginCaptures:
529547
'1': { name: storage.modifier.ts } # captures keyword (public or private or protected)
530548
'2': { name: storage.modifier.ts } # captures keyword (abstract)
@@ -539,7 +557,7 @@ repository:
539557

540558
object-literal-method-declaration:
541559
name: meta.method.declaration.ts
542-
begin: '{{startOfIdentifier}}(?:\b(async)\s+)?(?:\b(get|set)\s+)?(?:(\*)\s*)?(?=({{propertyName}}\s*(\??))\s*[\(\<])'
560+
begin: '(?x){{startOfIdentifier}}(?:\b(async)\s+)?(?:\b(get|set)\s+)?(?:(\*)\s*)?(?=({{propertyName}}\s*(\??))\s*[\(\<])'
543561
beginCaptures:
544562
'1': { name: storage.modifier.async.ts } # captures keyword (async)
545563
'2': { name: storage.type.property.ts } # captures keyword (get|set)
@@ -548,7 +566,7 @@ repository:
548566
patterns:
549567
- include: '#method-declaration-name'
550568
- include: '#function-body'
551-
- begin: '{{startOfIdentifier}}(?:\b(async)\s+)?(?:\b(get|set)\s+)?(?:(\*)\s*)?(?=({{propertyName}}\s*(\??))\s*[\(\<])'
569+
- begin: '(?x){{startOfIdentifier}}(?:\b(async)\s+)?(?:\b(get|set)\s+)?(?:(\*)\s*)?(?=({{propertyName}}\s*(\??))\s*[\(\<])'
552570
beginCaptures:
553571
'1': { name: storage.modifier.async.ts } # captures keyword (async)
554572
'2': { name: storage.type.property.ts } # captures keyword (get|set)
@@ -558,11 +576,12 @@ repository:
558576
- include: '#method-declaration-name'
559577

560578
method-declaration-name:
561-
begin: (?={{propertyName}}\s*(\??)\s*[\(\<])
579+
begin: (?x)(?={{propertyName}}\s*(\??)\s*[\(\<])
562580
end: (?=\(|\<)
563581
patterns:
564582
- include: '#string'
565583
- include: '#array-literal'
584+
- include: '#numeric-literal'
566585
- name: meta.definition.method.ts entity.name.function.ts
567586
match: '{{identifier}}'
568587
- name: keyword.operator.optional.ts
@@ -1390,23 +1409,14 @@ repository:
13901409
numeric-literal:
13911410
patterns:
13921411
- name: constant.numeric.hex.ts
1393-
match: \b(?<!\$)0(x|X)[0-9a-fA-F][0-9a-fA-F_]*\b(?!\$)
1412+
match: '{{hexNumber}}'
13941413
- name: constant.numeric.binary.ts
1395-
match: \b(?<!\$)0(b|B)[01][01_]*\b(?!\$)
1414+
match: '{{binaryNumber}}'
13961415
- name: constant.numeric.octal.ts
1397-
match: \b(?<!\$)0(o|O)?[0-7][0-7_]*\b(?!\$)
1416+
match: '{{octalNumber}}'
13981417
- match: |-
13991418
(?x)
1400-
(?<!\$)(?:
1401-
(?:\b[0-9][0-9_]*(\.)[0-9][0-9_]*[eE][+-]?[0-9][0-9_]*\b)| # 1.1E+3
1402-
(?:\b[0-9][0-9_]*(\.)[eE][+-]?[0-9][0-9_]*\b)| # 1.E+3
1403-
(?:\B(\.)[0-9][0-9_]*[eE][+-]?[0-9][0-9_]*\b)| # .1E+3
1404-
(?:\b[0-9][0-9_]*[eE][+-]?[0-9][0-9_]*\b)| # 1E+3
1405-
(?:\b[0-9][0-9_]*(\.)[0-9][0-9_]*\b)| # 1.1
1406-
(?:\b[0-9][0-9_]*(\.)\B)| # 1.
1407-
(?:\B(\.)[0-9][0-9_]*\b)| # .1
1408-
(?:\b[0-9][0-9_]*\b(?!\.)) # 1
1409-
)(?!\$)
1419+
{{decimalNumber}}
14101420
captures:
14111421
'0': {name: constant.numeric.decimal.ts}
14121422
'1': {name: meta.delimiter.decimal.period.ts}

0 commit comments

Comments
 (0)