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

Bug with reusing patterns #4

Open
LPeter1997 opened this issue Sep 7, 2022 · 0 comments
Open

Bug with reusing patterns #4

LPeter1997 opened this issue Sep 7, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@LPeter1997
Copy link
Member

LPeter1997 commented Sep 7, 2022

The following:

let g: TextMateGrammar = {
    name: 'A',
    extensions: [],
    modes: [
        cat(CARDINALITY, rep0(Space), SEP, rep0(Space), cardinalityValue, rep0(Space), DOTDOT, rep0(Space), cardinalityValue),
    ]
}

Generates this JSON:

{
  "$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
  "name": "A",
  "scopeName": "source.a",
  "fileTypes": [],
  "patterns": [
    {
      "match": "(@Cardinality\\b)\\s*(:)\\s*(?:([0-9]+(?:\\.[0-9]+)?)|(\\*))\\s*(\\.\\.)\\s*(?:([0-9]+(?:\\.[0-9]+)?)|(\\*))",
      "captures": {
        "1": {
          "name": "keyword.other.class.a"
        },
        "2": {
          "name": "punctuation.a"
        },
        "5": {
          "name": "punctuation.a"
        },
        "6": {
          "name": "constant.numeric.a"
        },
        "7": {
          "name": "constant.numeric.a"
        }
      }
    }
  ]
}

Which is incorrect, one of the captures is missing its scope. This is likely because of reusing patterns (cardinalityValue in this case). The likely wrong component is, TagPattern, which blindly overwrites tag values under the same key. Instead, it should likely merge the tags. The extendMap call should be replaced with something more appropriate, that merges tag lists.

The other (maybe even more) likely candidate is the fact that tag creates a capture at construction, not at regex-generation. This is super problematic, as the capture will be reused. The if (!(this.element instanceof CapturePattern)) throw new Error('Tagging requires an underlying capture'); should be removed and wrapping should happen there instead.

Most likely: captures should map to a list of indices instead of one index, since a capture can appear multiple places.

@LPeter1997 LPeter1997 added the bug Something isn't working label Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant