Skip to content

Commit

Permalink
Fix number scopes (SublimeText#187)
Browse files Browse the repository at this point in the history
This commit...

1. removes `_` separators in digits as both Windows Powershell and PWSH 7.4
   raise errors if present.
2. adjusts number related scopes to ST core syntaxes
3. ensures `.` after numbers is scoped member accessor.
  • Loading branch information
deathaxe authored Jan 10, 2025
1 parent bea280e commit f21161d
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 145 deletions.
96 changes: 52 additions & 44 deletions PowerShell.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ first_line_match: |-
)
variables:
dec_digits: (?:[\d_]*\d)
dec_exponent: (?:[eE][-+]?{{dec_digits}})
float_suffix: (?i:[fdm](?!b))
integer_suffix: (?i:ul?|lu?)
bytes_unit: (?i:[kmgtp]b)
dec_exponent: (?:[eE][-+]?\d*)
dec_suffix: '[dDlL]'
double_suffix: '[dD]'
long_suffix: '[lL]'
unit_suffix: (?i:[kmgtp]b)
kebab_break: (?![\w-])

contexts:
Expand Down Expand Up @@ -548,48 +548,56 @@ contexts:
pop: true

numeric-constant:
- match: \b((0[xX])[\h_]*\h({{integer_suffix}})?)({{bytes_unit}})?\b
captures:
1: constant.numeric.integer.hexadecimal.powershell
2: punctuation.definition.numeric.base.powershell
3: storage.type.numeric.powershell
4: keyword.other.unit.powershell
- match: \b((0[bB])[01_]*[01]({{integer_suffix}})?)({{bytes_unit}})?\b
captures:
1: constant.numeric.integer.binary.powershell
2: punctuation.definition.numeric.base.powershell
3: storage.type.numeric.powershell
4: keyword.other.unit.powershell
- match: \b(0[bB])([01]*)({{long_suffix}}?{{unit_suffix}}?)\b
scope: meta.number.integer.binary.powershell
captures:
1: constant.numeric.base.powershell
2: constant.numeric.value.powershell
3: constant.numeric.suffix.powershell
push: members
- match: \b(0[xX])(\h*)({{long_suffix}}?{{unit_suffix}}?)\b
scope: meta.number.integer.hexadecimal.powershell
captures:
1: constant.numeric.base.powershell
2: constant.numeric.value.powershell
3: constant.numeric.suffix.powershell
push: members
- match: |-
(?x:
(
\b{{dec_digits}}
(?:
(?:
(?:(\.(?!\.))\d*) # No `_` after the `.`
{{dec_exponent}}?
| {{dec_exponent}}
)
({{float_suffix}})?
| ({{float_suffix}})
)
| \b{{dec_digits}}\.(?!\.)
| (\.)\d+
)
({{bytes_unit}}\b)?
(?x)
(
# .10 .10e5
(\.)\d+{{dec_exponent}}?
# 1.2 1.2e-3 1.e2 1e2
| \d+ (?: (\.) \d+ {{dec_exponent}}? | (\.)? {{dec_exponent}} )
)
captures:
1: constant.numeric.float.decimal.powershell
( {{dec_suffix}}? {{unit_suffix}}? )\b
|
# 10.l 10.lGB 10.GB
( \d+ (\.) )
( {{dec_suffix}} {{unit_suffix}}? | {{unit_suffix}} )\b
scope: meta.number.float.decimal.powershell
captures:
1: constant.numeric.value.powershell
2: punctuation.separator.decimal.powershell
3: storage.type.numeric.powershell
4: storage.type.numeric.powershell
5: punctuation.separator.decimal.powershell
6: keyword.other.unit.powershell
- match: \b((?:0|[1-9]{{dec_digits}}?)({{integer_suffix}})?)({{bytes_unit}})?\b
captures:
1: constant.numeric.integer.decimal.powershell
2: storage.type.numeric.powershell
3: keyword.other.unit.powershell
3: punctuation.separator.decimal.powershell
4: punctuation.separator.decimal.powershell
5: constant.numeric.suffix.powershell
6: constant.numeric.value.powershell
7: punctuation.separator.decimal.powershell
8: constant.numeric.suffix.powershell
push: members
- match: \b(\d+)({{double_suffix}}{{unit_suffix}}?)\b
scope: meta.number.float.decimal.powershell
captures:
1: constant.numeric.value.powershell
2: constant.numeric.suffix.powershell
push: members
- match: \b(\d+)({{long_suffix}}?{{unit_suffix}}?)\b
scope: meta.number.integer.decimal.powershell
captures:
1: constant.numeric.value.powershell
2: constant.numeric.suffix.powershell
push: members

script-block:
- match: (%)?(\{)
Expand Down
14 changes: 7 additions & 7 deletions Tests/syntax_test_Function.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function Verb-Noun {
#^^^^^^^^^^^^^^^^^^^^^^ meta.attribute
# ^^^^^^^^ variable.parameter.attribute
# ^ keyword.operator.assignment
# ^ constant.numeric.integer
# ^ constant.numeric.value
# ^ punctuation.separator
ParameterSetName = 'Parameter Set 1')]
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.attribute
Expand Down Expand Up @@ -177,9 +177,9 @@ function Verb-Noun {
# <- punctuation.section.brackets.begin
# ^^^^^^^^^^^^ support.function.attribute
# ^ punctuation.section.group.begin
# ^ constant.numeric.integer
# ^ constant.numeric.value
# ^ punctuation.separator
# ^ constant.numeric.integer
# ^ constant.numeric.value
# ^ punctuation.section.group.end
# ^ punctuation.section.brackets.end
[ValidateSet("sun", "moon", "earth")]
Expand Down Expand Up @@ -256,9 +256,9 @@ function Verb-Noun {
# <- meta.attribute punctuation.section.brackets.begin
# ^^^^^^^^^^^^ support.function.attribute
# ^ punctuation.section.group.begin
# ^ constant.numeric.integer
# ^ constant.numeric.value
# ^ punctuation.separator
# ^ constant.numeric.integer
# ^ constant.numeric.value
# ^ punctuation.section.group.end
# ^ punctuation.section.brackets.end
[int32]
Expand Down Expand Up @@ -294,9 +294,9 @@ function Verb-Noun {
# <- meta.attribute punctuation.section.brackets.begin
# ^ meta.attribute support.function.attribute
# ^ meta.attribute punctuation.section.group.begin
# ^ meta.attribute constant.numeric.integer
# ^ meta.attribute meta.number.integer.decimal constant.numeric.value
# ^ punctuation.separator
# ^^ meta.attribute constant.numeric.integer
# ^^ meta.attribute meta.number.integer.decimal constant.numeric.value
# ^ meta.attribute punctuation.section.group.end
# ^ meta.attribute punctuation.section.brackets.end
[String]
Expand Down
Loading

0 comments on commit f21161d

Please sign in to comment.