Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax Dev - Backreferences show as supported but make entire context anonymous #6577

Open
axemonk opened this issue Jan 3, 2025 · 1 comment

Comments

@axemonk
Copy link

axemonk commented Jan 3, 2025

Description of the bug

(EDIT (1/9/2025): Repo contents have changed dramatically in structure since this report. For a more succinct version of the repo that the below still applies to, switch to tag 2.5.2)

Link to repository in question. Latest release contains the code depicted below, but is probably not yet in PackageControl at time of writing.

Some strange behavior @michaelblyons and I have run into regarding backreferences in sublime syntax definitions. First finding was that using \n to enumerate backreferences like ({{thing}})(\1) is compatible with the new regex engine (edit: at least according to syntax compatibility check through Sublime), but ({{thing}})(\k<1>) is not. So far so good. Just use \n . The catch here is that if any pattern in a context uses \n , the whole context becomes anonymous:
image

Relevant patterns shown below (links to actual repo code can be found below snippets). The second pattern in the dynamic-reference context is what gets the capture pictured above, but the first pattern contains a \n backreference, causing the whole context to become anonymous. This also applies when a \n backreference appears after the capturing pattern.

variables:
  cell: (?:\$?[A-Za-z]+\$?\d+)
    # Push to dynamic context to isolate forced anonymity to just dynamic refs
    - match: (?=(?:@?{{cell}})(?:(?:#?:{{cell}}#)|(?:#:{{cell}}#?)))
      push: dynamic-reference
  dynamic-reference:
    # Zero-dimensional cell range w/ dynamics
    # Verified as sregex compatible with \n but not with \k<n>
    # A1#:A1#
    # A1:A1#
    # @A1#:A1#
    # @A1:A1#
    - match: (@?)({{cell}})(#?)(:)(\2)(#)
      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
      set: maybe-union-operator

    # Cell range with dynamics
    # A1#:B2
    # A1#:B2#
    # @A1#:B2
    # @A1#:B2#
    - match: (@?)({{cell}})(#)(:)({{cell}})(#?)
      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
      set: maybe-union-operator

    # Bail if nothing matches
    - include: immediately-pop

Code links corresponding to the above (not showing as embedded for some reason, apologies):
https://github.com/axemonk/Excel-formula/blob/53ba9bb1f4d3e52088c1928b257e75a1e6224279/Excel%20formula.sublime-syntax#L19-L20
https://github.com/axemonk/Excel-formula/blob/53ba9bb1f4d3e52088c1928b257e75a1e6224279/Excel%20formula.sublime-syntax#L321-L323
https://github.com/axemonk/Excel-formula/blob/53ba9bb1f4d3e52088c1928b257e75a1e6224279/Excel%20formula.sublime-syntax#L407-L440

The job of the backrefenced pattern is to match strings like this:
image
https://github.com/axemonk/Excel-formula/blob/53ba9bb1f4d3e52088c1928b257e75a1e6224279/syntax_test_.Excel%20Examples.txt#L203-L209

Sublime showing \n as not unsupported:
image

Sublime showing \k<n> as not supported:
image

Steps to reproduce

  1. If Package Control hasn't grabbed latest release, add Excel formula.sublime-syntax to User packages folder from repo (or do some LCL magic after cloning the repo, I'm a bit of a noob personally). Enable Excel formula syntax in blank file.
  2. Type =A1#:A2# and =A1:A1# on new lines and use scope inspector to inspect scopes. Note how both have an anonymous context.
  3. Open Excel formula.sublime-syntax and run syntax compatibility check. Note that the check reports all green.
  4. Comment out just the code with the backrefenced pattern in the dynamic-reference context and compare scopes again. Note how =A1#:A2# scopes correctly with no anonymity.
  5. Finally, uncomment backreferenced code and replace the backreference with \k<2> and compare scopes again. Note how both =A1#:A2# and =A1:A1# are scoped correctly with no anonymity. Then run syntax compatibility check. Note that check reports incompatibility.

Expected behavior

Either one of the following:
\n should cause compatibility check to fail.
\n should work as normal and not cause anonymity, assuming that it is actually supported.

Actual behavior

Both of the following
\n passes compatibility check
\n forces all scopes applied from a context to become anonymous when viewed through the scope inspector.

Sublime Text build number

4189

Operating system & version

Windows 11

(Linux) Desktop environment and/or window manager

No response

Additional information

No response

OpenGL context information

No response

@axemonk axemonk changed the title Syntax Dev - Backreferences show as supported, but make entire context anonymous Syntax Dev - Backreferences show as supported but make entire context anonymous Jan 3, 2025
@axemonk
Copy link
Author

axemonk commented Jan 9, 2025

Repo contents have changed dramatically in structure since this report. For a more succinct version of the repo that the above still applies to, switch to tag 2.5.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants