Home Download Buy Blog Forum Support

tmLanguage regex failing

tmLanguage regex failing

Postby craig on Sun Oct 21, 2012 5:31 pm

This has been driving me crazy. I have been playing around with trying to adopt goto functionality for Sylus http://learnboost.github.com/stylus/

Given this sample code:

Code: Select all
header
    padding 12px 0px
    background #eee
    border-bottom 1px solid #ccc
    linearGradient(#f8f8f8, #e6e6e6)

    nav
        font-size 15px
        width 400px
        position absolute
        max-height 40px
        right 0px
        bottom 5px
        text-align right

    nav .portrait
            position relative
            top 9px
            margin-right 5px
            left 0px

            img
                border 1px solid #ccc
                width 30px
                height 30px
                transition border-color 0.25s

                &:hover
                    border-color #676767


I am trying to match the selector groups (header, nav, nav .portrait, img, &:hover) but not match any of the other lines. I am aware I could match every line and then process submatches on that to determine if it is in fact a valid selector group, but if possible I would like to avoid that and just match the selector groups in one swoop.

I am pretty good at regex and I got it working perfectly on http://rubular.com/r/oF8b6TzArG, but when I try to do it in a tmLanguage file it does not seem to work at all. Perhaps it is some limitation with backreferences that could be a variable number of spaces?

Here is what I am working with

Code: Select all
<dict>
    <key>match</key>
    <string>^( *?)([&amp;\:\.\#a-zA-Z](.*?))(?=\n\1 +)</string>
    <key>captures</key>
    <dict>
        <key>2</key>
        <dict>
            <key>name</key>
            <string>meta.selector.stylus</string>
        </dict>
    </dict>
</dict>


I am also aware if we assume one selector per line without spaces I could change it to ^( *?)([&amp;\:\.\#a-zA-Z](\S*?))(?=\n) and that will work in Sublime.

Any help would be greatly appreciated.

Also don't worry about the whole tabs vs. spaces thing. After I get it working I can easily adapt it to work with both.
craig
 
Posts: 13
Joined: Thu Mar 01, 2012 3:03 am
Location: New York, NY

Re: tmLanguage regex failing

Postby nick. on Mon Oct 22, 2012 8:09 pm

To do multi-line regexs, you need to use the begin and end keys. See here
nick.
 
Posts: 266
Joined: Wed Jan 18, 2012 3:45 am

Re: tmLanguage regex failing

Postby craig on Tue Oct 23, 2012 12:46 am

Thanks for the reply. I tried using begin and end too, but didn't have any luck. I will try again.
craig
 
Posts: 13
Joined: Thu Mar 01, 2012 3:03 am
Location: New York, NY

Re: tmLanguage regex failing

Postby craig on Tue Oct 23, 2012 1:21 am

Yeah, no luck. For reference here is the code I tried

Code: Select all
<dict>
    <key>begin</key>
    <string>^( *)</string>
    <key>end</key>
    <string>\n\1 +</string>
    <key>match</key>
    <string>[&amp;:.#a-zA-Z](.*)</string>
    <key>name</key>
    <string>meta.selector.stylus</string>
</dict>


Also what I am trying to match is a single line so it shouldn't require using begin and end. Positive lookaheads for \n still seem to work when using single line matching.
craig
 
Posts: 13
Joined: Thu Mar 01, 2012 3:03 am
Location: New York, NY

Re: tmLanguage regex failing

Postby GreLI on Mon Nov 12, 2012 2:22 pm

As you can see per language grammar:
Note that the regular expressions are matched against only a single line of the document at a time. That means it is not possible to use a pattern that matches multiple lines.
GreLI
 
Posts: 7
Joined: Mon Jan 23, 2012 6:00 pm

Re: tmLanguage regex failing

Postby craig on Wed Nov 14, 2012 1:38 am

Yes, and the pattern I am using is trying to match just a single line. Matching against a single line still lets you do a positive look ahead onto the next line. I don't think the problem has to do with single line vs. multi line because it still fails when using the begin and end tags.

It seems to me that the problem is that the backreference fails to match a variable number of spaces correctly to detect indentation.
craig
 
Posts: 13
Joined: Thu Mar 01, 2012 3:03 am
Location: New York, NY


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 8 guests

cron