Code Monkey home page Code Monkey logo

Comments (3)

dawsonc623 avatar dawsonc623 commented on June 12, 2024 1

So, it worked on my full grammar (well, mostly, but I think where it is acting up is unrelated to this). I extended the test grammar and input to cover more nesting and cyclical cases, too, which worked.

The fix was indeed to step back from the whileCaptures approach and instead have while match just the captured level of indentation from the begin and a look-ahead to see if the next character was another whitespace. Then, putting patterns at the top level worked. The extended example:

Grammar:

{
  "name": "Foo",
  "scopeName": "source.foo",
  "patterns": [
    { "include": "#foo" }
  ],
  "repository": {
    "foo": {
      "name": "meta.foo",
      "begin": "(\\s*)foo.*",
      "while": "\\1(?=\\s)",
      "patterns": [{ "include": "#bar" }, { "include": "#hmm" }]
    },
    "bar": {
      "name": "meta.bar",
      "begin": "(\\s*)bar.*",
      "while": "\\1(?=\\s)",
      "patterns": [{ "include": "#baz" }]
    },
    "baz": {
      "name": "meta.baz",
      "begin": "(\\s*)baz.*",
      "while": "\\1(?=\\s)",
      "patterns": [{ "include": "#foo" }, { "include": "#hmm" }]
    },
    "hmm": {
      "name": "meta.hmm",
      "begin": "(\\s*)hmm.*",
      "while": "\\1(?=\\s)"
    }
  }
}

Input:

  foo
    bar
      baz
        foo
          bar
            baz
              hmm
      baz
      baz
        hmm
        hmm
        hmm
    bar
      baz
      baz
  biz
  foo
    hmm

The token inspector confirmed this works as I would expect, so regardless of what is going on with my full grammar I think the original issue was more in terms of my understanding of whileCaptures (or apparently lack thereof) than the overall nested concept. Because of that, I am closing this issue under the assumption the wonkiness in my full grammar is not quite this issue either.

from vscode-textmate.

jeff-hykin avatar jeff-hykin commented on June 12, 2024

I agree, I believe what's happening here is the while is eating all the characters, e.g. #3 in the stuff below. (I'm just a random grammar/syntax maker BTW) That said I think it is still possible to nest them.

Here's a copy-paste of my personal documentation on while, which I think partly confirms what you saw in the source code.

The textmate "while" key has almost no documentation. I'm writing this to explain what little I know about it.

The good part

The "while" key is stronger than the "end" pattern, as soon as the while is over, it stops and most importantly, it cuts off any ranges that are still open. This is incredibly important because almost nothing else in textmate does this, and it is useful for stopping broken syntax.

I believe it was designed to match things like the python intentation-based block.

The bad part(s)

However, there are some caveats.

  1. The "while" pattern is line-based, not character-based. If you match a single character on a line, then the whole line is considered to be inside the pattern-range
  2. On each line, nothing will start being matched until the while pattern has been fully matched
  3. Once the while pattern matches, everything after the while pattern will be tagged using the patterns inside of the pattern-range (the included patterns)

(end)

I have no idea if any of that is intended behavior. That said, I think your issue is the first while consumes all the text on the line, so there's nothing left for the included patterns to even match against.

I would guess that changing it to match just the white space of one indent level, and then including bar as a regular include (rather than a whileCapture) would fix it. I don't think I've ever gotten the while captures to work, I usually just use a lookahead in the while, and then include a pattern that does the actual matching

from vscode-textmate.

dawsonc623 avatar dawsonc623 commented on June 12, 2024

Thanks for the response. Interestingly, Python does not use while for this case (at least not the built-in support). Only a handful of built-ins use while at all, and Markdown is the only one that does extensively (apart from searchResult, which I assume is, well, related to the search feature).

Anyway, I went with capturing the full line and passing it into the whileCaptures because generally how I have seen that work in other constructs (say, match/captures) is that the includes can process the incoming as they need. That said, some of your thoughts triggered a different line of thinking, and I was able to do some adjustments to the test grammar to match the test input as I would expect. I will attempt to port that over to my real grammar and report back if it works.

from vscode-textmate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.