Skip to content

Commit

Permalink
[ShellScript] Refine pattern matching contexts
Browse files Browse the repository at this point in the history
This commit ...

1. introduce a `pattern-common` context for common patterns,
   which are valid in all sorts and locations of patterns.

2. modifies how `meta.group.regexp` is applied to reduce requirement of
   duplicated scope names in punctuation patterns.
  • Loading branch information
deathaxe committed Aug 10, 2024
1 parent f741d75 commit 6d0cac0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 82 deletions.
60 changes: 34 additions & 26 deletions ShellScript/Bash.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -2259,32 +2259,36 @@ contexts:
- include: eol-pop

pattern-groups:
- match: ([?*+@!])(\()
captures:
1: string.unquoted.shell keyword.operator.quantifier.regexp.shell
2: meta.group.regexp.shell string.unquoted.shell punctuation.section.group.begin.regexp.shell
push: pattern-group-body
- match: '[?*+@!](?=\()'
scope: keyword.operator.quantifier.regexp.shell
push: pattern-group

pattern-group:
- meta_include_prototype: false
- match: \(
scope: punctuation.section.group.begin.regexp.shell
set: pattern-group-body

pattern-group-body:
- clear_scopes: 1 # clear `string.unquoted`
- meta_include_prototype: false
- meta_content_scope: meta.group.regexp.shell string.unquoted.shell
- meta_scope: meta.group.regexp.shell string.unquoted.shell
- include: pattern-group-end
- include: pattern-group-content

pattern-group-content:
- include: string-unquoted-content
- include: pattern-groups
- include: pattern-group-charsets
- include: pattern-operators
- include: pattern-wildcards

pattern-group-end:
- match: \)
scope: meta.group.regexp.shell string.unquoted.shell punctuation.section.group.end.regexp.shell
scope: punctuation.section.group.end.regexp.shell
pop: 1
- include: line-continuations

pattern-group-content:
- include: string-unquoted-content
- include: pattern-groups
- include: pattern-group-charsets
- include: pattern-group-operators
- include: pattern-common

pattern-group-charsets:
- match: (?=\[)
branch_point: pattern-group-charset
Expand All @@ -2308,11 +2312,11 @@ contexts:
- include: pattern-charset-end
- include: pattern-charset-content

pattern-operators:
pattern-group-operators:
- match: \|
scope: keyword.operator.logical.regexp.shell

pattern-wildcards:
pattern-common:
- match: \*
scope: constant.other.wildcard.asterisk.shell
- match: \?
Expand Down Expand Up @@ -2439,7 +2443,7 @@ contexts:
expansions-patterns:
- include: pattern-groups
- include: pattern-charsets
- include: pattern-wildcards
- include: pattern-common

expansions-variables:
- include: expansions-arithmetic
Expand Down Expand Up @@ -2764,7 +2768,7 @@ contexts:
- include: string-unquoted-content
- include: expansion-parameter-pattern-charsets
- include: expansion-parameter-pattern-groups
- include: pattern-wildcards
- include: pattern-common

expansion-parameter-pattern-charsets:
- match: (?=\[)
Expand All @@ -2790,22 +2794,26 @@ contexts:
- include: pattern-charset-content

expansion-parameter-pattern-groups:
- match: ([?*+@!])(\()
captures:
1: string.unquoted.shell keyword.operator.quantifier.regexp.shell
2: meta.group.regexp.shell string.unquoted.shell punctuation.section.group.begin.regexp.shell
push: expansion-parameter-pattern-group-body
- match: '[?*+@!]?(?=\()'
scope: keyword.operator.quantifier.regexp.shell
push: expansion-parameter-pattern-group

expansion-parameter-pattern-group:
- meta_include_prototype: false
- match: \(
scope: punctuation.section.group.begin.regexp.shell
set: expansion-parameter-pattern-group-body

expansion-parameter-pattern-group-body:
- clear_scopes: 1 # clear `string.unquoted`
- meta_include_prototype: false
- meta_content_scope: meta.group.regexp.shell string.unquoted.shell
- meta_scope: meta.group.regexp.shell string.unquoted.shell
- include: pattern-group-end
- include: string-unquoted-content
- include: expansion-parameter-pattern-groups
- include: expansion-parameter-pattern-group-charsets
- include: pattern-operators
- include: pattern-wildcards
- include: pattern-group-operators
- include: pattern-common
- include: brace-pop

expansion-parameter-pattern-group-charsets:
Expand Down
63 changes: 7 additions & 56 deletions ShellScript/Zsh.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -318,60 +318,22 @@ contexts:
- string-path-pattern-body
- maybe-tilde-interpolation

string-path-pattern-content:
- meta_prepend: true
- include: zsh-glob-flags
- include: zsh-glob-operators
- include: zsh-glob-ranges

###[ PATH PATTERN MATCHING ]###################################################

path-pattern-content:
- meta_prepend: true
- include: zsh-glob-flags
- include: zsh-glob-operators
- include: zsh-glob-ranges

###[ SHELL PATTERN MATCHING ]##################################################

pattern-main-content:
- meta_prepend: true
- include: zsh-glob-flags
- include: zsh-glob-operators
- include: zsh-glob-ranges

pattern-groups:
- include: zsh-glob-flags
# prefer EXTENDED_GLOB over optional KSH_GLOB
- match: (?=\()
branch_point: pattern-group
branch_point: zsh-glob-qualifier
branch:
- zsh-glob-qualifier
- pattern-group

pattern-group:
- match: \(
scope: punctuation.section.group.begin.regexp.shell
set: pattern-group-body

pattern-group-body:
- clear_scopes: 1 # clear `string.unquoted`
- meta_include_prototype: false
- meta_scope: meta.group.regexp.shell string.unquoted.shell
- include: pattern-group-end
- include: pattern-group-content

pattern-group-content:
pattern-common:
- meta_prepend: true
- include: zsh-glob-flags
- include: zsh-glob-operators
- include: zsh-glob-ranges

pattern-group-end:
- match: \)
scope: punctuation.section.group.end.regexp.shell
pop: 1
- include: line-continuations

###[ ARITHMETIC EXPANSIONS ]###################################################

expansions-arithmetic:
Expand Down Expand Up @@ -462,24 +424,13 @@ contexts:
- expansion-parameter-pattern
- maybe-tilde-interpolation

expansion-parameter-pattern:
- meta_prepend: true
- include: zsh-glob-flags
- include: zsh-glob-operators
- include: zsh-glob-ranges

expansion-parameter-pattern-groups:
- include: zsh-glob-flags
# prefer EXTENDED_GLOB over optional KSH_GLOB
- match: \(
scope: meta.group.regexp.shell string.unquoted.shell punctuation.section.group.begin.regexp.shell
scope: punctuation.section.group.begin.regexp.shell
push: expansion-parameter-pattern-group-body

expansion-parameter-pattern-group-body:
- meta_prepend: true
- include: zsh-glob-flags
- include: zsh-glob-operators
- include: zsh-glob-ranges

zsh-parameter-subscription:
- meta_include_prototype: false
- meta_scope: meta.interpolation.parameter.shell
Expand Down Expand Up @@ -606,11 +557,11 @@ contexts:
- match: '{{word_break}}'
pop: 1
- match: ''
fail: pattern-group
fail: zsh-glob-qualifier

zsh-glob-qualifier-fail:
- match: (?=\S)
fail: pattern-group
fail: zsh-glob-qualifier

zsh-glob-qualifier-content:
- include: comma-separators
Expand Down

0 comments on commit 6d0cac0

Please sign in to comment.