Community
    • Login

    Perl language syntax highlighting troubles (bug or limitation ?)

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    112 Posts 6 Posters 43.9k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • EkopalypseE
      Ekopalypse @Gilles Maisonneuve
      last edited by Ekopalypse

      @Gilles-Maisonneuve

      [1] informs the python script, that only the results from sub match group 1 should be colored in white (255,255,255)
      sub match group 1 is the result of (<<)

      In order to make it painting all you can use [0]

      I’m still confused about the 2 to 3 match groups.
      Am I incorrect when saying that
      (\s*(<<)\s*("{0,1}.+"{0,1})\s*;.*?\3)
      (<<)
      ("{0,1}.+"{0,1})
      are three match groups?

      Maybe the confusion comes from the fact that references matches within a
      regular expression starts by 1 but python starts counting match results by 0.

      Sorry, but I have to stay up early tomorrow and it is already 1am but I’m really
      interested in solving our (mis)understanding today later (maybe in ~16-18hours)?

      1 Reply Last reply Reply Quote 1
      • Gilles MaisonneuveG
        Gilles Maisonneuve
        last edited by

        ok, tomorrow is another day
        ‘see’ you tomorrow.
        have a good night.
        g

        1 Reply Last reply Reply Quote 1
        • EkopalypseE
          Ekopalypse
          last edited by

          you too - see you

          Gilles MaisonneuveG 1 Reply Last reply Reply Quote 1
          • Gilles MaisonneuveG
            Gilles Maisonneuve @Ekopalypse
            last edited by

            @Ekopalypse

            OK, so the

            (\s*(<<)\s*("{0,1}.+"{0,1})\s*;.*?\3)
            

            is a regex group, not a function call surrounded by parenthèses or a logical group provided by the ‘r’ keyword. My mistake.
            BUT THEN, it is possible in Python to enclose an instruction such as ?\3 which means (as far as I understood what you explained to me earlier) recursive reference to a regexp named ‘3’) ??? The ‘3’ name being given in the expression regexes[(3, (255,255,255))] is that correct ? SO you can reference an expression within itself while it has not be closed yet: the last parenthese of the expression 3 is after the \3). Is that what it means ?

            Python syntax is a bit complicated to me.

            Alan KilbornA 1 Reply Last reply Reply Quote 0
            • Alan KilbornA
              Alan Kilborn @Gilles Maisonneuve
              last edited by

              @Gilles-Maisonneuve said:

              Python syntax is a bit complicated to me

              It’s not Python syntax, it’s regular expression syntax. It’s just not Perl regular expression syntax. :)

              And, BTW, nobody in the history of the world, especially someone coming from a Perl background, has ever uttered the phrase you typed.

              Gilles MaisonneuveG 1 Reply Last reply Reply Quote 1
              • guy038G
                guy038
                last edited by guy038

                Hello @gilles-maisonneuve, @eko-palypse and All,

                Gilles, could you verify that the two lines, below, work, with yours Red, Green and Blue colors ?

                regexes[(3, (R,G,B))] = (r'(?s-i)(<<)(['"]?)(\w+?)\2\h*;.*?\3', [1])
                regexes[(4, (R,G,B))] = (r'(?s-i)(<<)\h+('|")(\w+?)\2\h*;.*?\3', [1,3])
                

                For these two regexes :

                • Group 1 = << = double inferior than sign

                • Group 2 = ['"]? = an optional single or double quote, for regex id 3

                • Group 2 = '|" = a mandatory single or double quote ,separated from the << characters with blank characters, for regex id 4

                • Group 3 = \w+? = the shortest area of word characters, after the << sign, between possible quotes
                  and before a semicolon character ;, with possible blank characters, before and/or after the quote characters

                Notes :

                • In regex id 3, only the << string is highlighted ( Group 1 )

                • In regex id 4, the << and the text between quotes are highlighted ( Groups 1 and 3 )

                • I added the -i in-line modifier ( => (?s-i) leading syntax ) to be sure that the ending boundary of the block corresponds exactly with the text, between quotes ( search is sensitive to case ! )


                So my regex (?s-i)(<<)(['"]?)(\w+?)\2\h*;.*?\3 ( id 3 ) matches any of these six cases, below :

                $x=<<TEXT;
                Plain text here
                TEXT
                
                $x=<<'TEXT';
                Plain text here
                TEXT
                
                $x=<<"TEXT";
                Plain text here
                TEXT
                
                $x=<<TEXT ;
                Plain text here
                TEXT
                
                $x=<<'TEXT' ;
                Plain text here
                TEXT
                
                $x=<<"TEXT" ;
                Plain text here
                TEXT
                

                And my regex (?s-i)(<<)\h+('|")(\w+?)\2\h*;.*?\3 ( id 4 ) matches these 4 cases, below :

                $x=<< 'TEXT';
                Plain text here
                TEXT
                
                $x=<< "TEXT";
                Plain text here
                TEXT
                
                $x=<< 'TEXT' ;
                Plain text here
                TEXT
                
                $x=<< "TEXT" ;
                Plain text here
                TEXT
                

                Best Regards,

                guy038

                Gilles MaisonneuveG 2 Replies Last reply Reply Quote 3
                • Gilles MaisonneuveG
                  Gilles Maisonneuve @guy038
                  last edited by

                  @guy038

                  Hello Guy,

                  Could not make it work, sorry.

                  I mean:

                  • added (replaced original ones) in the EnhancePerlLexer.py from Ekopalypse the following lines (according to what you gave me:

                    regexes[(3, (224,0,0))] = (r’(?s-i)(<<)([‘"]?)(\w+?)\2\h*;.?\3’, [1])
                    regexes[(4, (0,0,224))] = (r’(?s-i)(<<)\h+('|")(\w+?)\2\h
                    ;.*?\3’, [1,3])

                  • saved it and restarted npp

                  • list itemstill have the same coloring, not working.

                  BUT, good news:

                  python console:
                  Traceback (most recent call last):
                  File "C:\Users\gm\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\startup.py", line 1, in <module>
                      import EnhancePerlLexer
                  File "C:\Users\gm\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\EnhancePerlLexer.py", line 36
                      regexes[(3, (224,0,0))] = (r'(?s-i)(<<)(['"]?)(\w+?)\2\h*;.*?\3', [1])
                                                                                          ^
                  SyntaxError: EOL while scanning string literal
                  Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)]
                  Initialisation took 110ms
                  Ready.
                  

                  Can you tell me what did I did wrong ?
                  (When I comment out the two lines I get back a valid coloring for the ‘q*’ syntaxes (yes, forgot to tell you, this had vanished too…)

                  1 Reply Last reply Reply Quote 1
                  • Gilles MaisonneuveG
                    Gilles Maisonneuve @guy038
                    last edited by

                    @guy038

                    Well, I commented out the rule 3 and kept rule 4.
                    Same kind of error:

                     regexes[(4, (0,0,224))] = (r'(?s-i)(<<)\h+('|")(\w+?)\2\h*;.*?\3', [1,3])
                                                                                            ^
                     SyntaxError: EOL while scanning string literal
                    
                    Gilles MaisonneuveG 1 Reply Last reply Reply Quote 0
                    • Gilles MaisonneuveG
                      Gilles Maisonneuve @Gilles Maisonneuve
                      last edited by

                      if I modify the rule like:

                      regexes[(4, (0,0,224))] = (r'(?s-i)((<<)\h+([\'"])(\w+?)\2\h*;.*?\3)', [1,3])
                      

                      I don’t get any longer a syntax error in Python BUT I get no coloring for the here doc either…

                      Any idea ?

                      1 Reply Last reply Reply Quote 0
                      • Gilles MaisonneuveG
                        Gilles Maisonneuve @Alan Kilborn
                        last edited by

                        @Alan-Kilborn

                        chcp 1250 >NUL: & perl -e "$var=q(Alan Kilborn est déplaisant dans sa façon de s'exprimer mais il a raison.); for my $p ('\t','\s') {print qq{\$p=$p},$var=~m/($p)déplaisant\1/x?$var:qq{n'en déplaise},qq{\n} ;};" & chcp 850 >NUL:
                        
                        $p=\tn'en déplaise
                        $p=\sAlan Kilborn est déplaisant dans sa façon de s'exprimer mais il a raison.
                        
                        Gilles MaisonneuveG 1 Reply Last reply Reply Quote 0
                        • Gilles MaisonneuveG
                          Gilles Maisonneuve @Gilles Maisonneuve
                          last edited by

                          J’ai tellement l’habitude d’utiliser $1, $2, …, qui, eux, ne fonctionnent pas dans un simple ‘match’ mais uniquement dans un ‘substitute’, que je ne connaissais pas cette façon de répéter les ‘patterns’ de ‘matching’. J’ai appris quelque chose.
                          Dont acte.

                          EkopalypseE 1 Reply Last reply Reply Quote 0
                          • EkopalypseE
                            Ekopalypse @Gilles Maisonneuve
                            last edited by Ekopalypse

                            @Gilles-Maisonneuve

                            Lunch break :-)

                            First, I’m sorry not to telling you that the single quote has to be escaped as it was
                            used to denote a python string - good, you figured it already out.

                            Let me break down the parts of that python code

                            regexes = OrderedDict()
                            regexes[(3, (255,0,0))] = (r'(?s)(\s*(<<)\s*("{0,1}.+"{0,1})\s*;.*?\3)', [0])
                            

                            regexes is variable, containing an OrderedDict class instance.
                            OrderedDict is more or less the same as a perl associative array or hash

                            regexes[] is the python way to access a key in that hash, like in perl regexes{}
                            regexes[()] the round bracket denotes a python tuple, in perl a list I guess (immutable)
                            the python tuple contains the items 3 and (255,0,0) <- this is again a tuple
                            The number 3 is here to create an unique key - has nothing to do with the regex itself.
                            So, regexes[(3, (255,0,0))] means, get me the value for key (3, (255,0,0)) from dict(hash) regexes

                            The value is (r’(?s)(\s*(<<)\s*(“{0,1}.+”{0,1})\s*;.*?\3)‘, [0])
                            Again, a python tuple containing the items r’…’ (raw string) and a list [] (in perl an array = mutable)
                            Everything within the raw string is the regex to be searched for and the list contains the information
                            which match group should be used for coloring
                            [0] is always the overall match of the complete regex and [1] would be the result from group 1,
                            [2] from group 2 and [1,2] from group 1 and group 2

                            So, in terms of regular expressions only the value part of the regexes hash/dict is of interest.
                            For searching only the raw string and for coloring which part was defined in the list [].

                            Does this makes sense to you?

                            The reason why this regex

                            regexes[(4, (0,0,224))] = (r'(?s-i)((<<)\h+([\'"])(\w+?)\2\h*;.*?\3)', [1,3])
                            

                            doesn’t do what you want is that you use 4 groups now whereas @guy038 has
                            removed the outer matching group brackets.

                            (?s-i)(<<)(['"]?)(\w+?)\2\h*;.*?\3

                            In order to make it work either use

                            regexes[(4, (0,0,224))] = (r'(?s-i)(<<)\h+([\'"])(\w+?)\2\h*;.*?\3', [1,3])
                            or
                            regexes[(4, (0,0,224))] = (r'(?s-i)((<<)\h+([\'"])(\w+?)\3\h*;.*?\4)', [1,3])

                            Gilles MaisonneuveG 1 Reply Last reply Reply Quote 2
                            • Alan KilbornA
                              Alan Kilborn
                              last edited by

                              No idea what the “chcp 1250…” posting was supposed to be saying to me. :)

                              This thread gets my vote for the biggest jumbled mess in the history of the community. :)

                              Meta ChuhM 1 Reply Last reply Reply Quote 4
                              • Meta ChuhM
                                Meta Chuh moderator @Alan Kilborn
                                last edited by

                                maybe @Ekopalypse will write a resuming manual, once this is over … i refuse :)

                                1 Reply Last reply Reply Quote 3
                                • EkopalypseE
                                  Ekopalypse
                                  last edited by

                                  You mean a short manager summary I guess :-D

                                  Meta ChuhM 1 Reply Last reply Reply Quote 1
                                  • Meta ChuhM
                                    Meta Chuh moderator @Ekopalypse
                                    last edited by

                                    if a short manager summary is, in your eyes, a fully featured guide, covering all eventualities, based on all caveats of the whole topic … then yes 😉

                                    1 Reply Last reply Reply Quote 3
                                    • EkopalypseE
                                      Ekopalypse
                                      last edited by

                                      LOL - back to business

                                      1 Reply Last reply Reply Quote 2
                                      • Gilles MaisonneuveG
                                        Gilles Maisonneuve @Ekopalypse
                                        last edited by Gilles Maisonneuve

                                        @Ekopalypse
                                        Replying at the message “Lunch break…” with all the explainations.

                                        1. THANK YOU ! I start enjoying Python since I read you.
                                          Well, I’ll never be a disciple, because not fan of OO and have difficulty to accept a language where the tabulations and spaces define the code syntax… reminds me too much of my youth with the punch cards and the punched paper roll (and yes, I’m that old), but it’s kind of fun to read when one understand it better.
                                          Your analogies with Perl made it very comprehensible, very kind of you.

                                        2. ** Y Y Y Y E E E E S S S S ! ! ! ! ! ! **
                                          It works. I just changed a ‘+’ into a ‘*’ after the first ‘\h’ to allow for no horizontal space between the preceding keyword / Perl-separator and the here doc starter (‘<<’). So my (yours with my ridiculous pinch of salt) regexp is now:

                                          regexes[(4, (0,0,224))] = (r’(?s-i)((<<)\h*([‘"])(\w+?)\3\h*;.*?\4)’, [1,3])
                                          ^^

                                        Thank to you and your patience (and for the readers, pissed at my garbage, this includes you Alan, and BTW ignore my perl joke, I was upset by the tone you used and perhaps, even further by the fact that you were right [chcp needed to get the accents on the French vowels on a Windows Perl console]).

                                        I was without-a-clue and you saved my day. And now…

                                        Already several adventures have begun to take shape which can be solved by no-one else. Right, Eko ?
                                        Right you are, Meta Chuh.
                                        And so, without further ado… …I hereby declare this case… …closed.

                                        {to sum up the solution provided by Ekopalypse in this thread of discussion}:

                                        regexes[(1, (255,0,128))] = (r'\bq[rwqx]{0,1}\b([^\h]).*?\1|(\bq[rwqx]{0,1}\b\h+(\w).*?\3)', [0])
                                        regexes[(2, (255,0,128))] = (r'\bq[rwqx]{0,1}\b\h*(\(.+?\)|\[.+?\]|\{.+?\})', [0])
                                        regexes[(4, (0,112,112))] = (r'(?s-i)((<<)\h*([\'"])(\w+?)\3\h*;.*?\4)', [1,3])
                                        

                                        Allow you to colorize your Perl ‘q*’ keywords and args with color RGB 255,0,128 and your here-docs with color RGB 0,112,112 in Notepad-plus-plus, using “Python Script” plugin. By so, you correct the coloring limitation of Scintilla Library for those keywords in Perl.

                                        Hope this summary will be satisfying enough for “Meta Chuh al.”.

                                        Have nice week (end of) all of you.

                                        Gilles

                                        Alan KilbornA EkopalypseE 2 Replies Last reply Reply Quote 3
                                        • Alan KilbornA
                                          Alan Kilborn @Gilles Maisonneuve
                                          last edited by

                                          @Gilles-Maisonneuve said:

                                          pissed at my garbage, this includes you Alan

                                          Oh, not at all…at least after 98 messages a positive outcome!

                                          1 Reply Last reply Reply Quote 1
                                          • EkopalypseE
                                            Ekopalypse @Gilles Maisonneuve
                                            last edited by Ekopalypse

                                            @Gilles-Maisonneuve

                                            Thank you for your kind words.
                                            I have enjoyed developing the script with you and the
                                            discussion afterwards was also helpful and intersting
                                            as it pointed out that the script comments aren’t 100% bulletproof.
                                            And please don’t hesitate to post here if you find something that
                                            doesn’t work the way you expected it - just let us know, in the end
                                            we can all benefit from it.

                                            Regarding the off topic comments, these were not meant to be insulting.
                                            Sometimes there is a sound in it that is only understood if you read through
                                            several other answers - they are mostly nice but sometimes they do provoke
                                            but they are still meant to be nice or at least helpful.
                                            The one with the summaries, for example, was from another thread where
                                            I caused a confusion, because of my recklessness, that was only cleared up
                                            a few posts later and then I gave a “manager summary” to among other things,
                                            to make life easier for future readers. Seems to become a running gag now :-)

                                            I agree with you, Python is in the beginning odd, especially
                                            when coming from a different language.
                                            It took some time getting used to it and I had my difficulties too,
                                            but now I find the language super - especially Python3, which is
                                            by the way not supported by the PythonScript plugin, :-(
                                            has syntax constructs and language extensions that I really like.

                                            So, have a nice weekend too.

                                            Gilles MaisonneuveG 1 Reply Last reply Reply Quote 2
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors