Announcement

Collapse
No announcement yet.

Making C++ comments important but trailing whitespace not important

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Making C++ comments important but trailing whitespace not important

    Dear Sirs:

    I am trying to get BC3 to consider white space at the end of C++ //-style comments to be unimportant. However, I need the rest of the comment to be important.

    In other words, I want changes to //-style comments to be considered important, except for changes to any white space that is at the end of the line.

    -------------

    First, I tried making a new Grammar Item that declares white space at the end of lines to be unimportant:

    Grammar Item Element Name = "end of line white space"
    Category = Delimited
    Text from = \s+$
    to =
    Check marks are in "Stop at end of line", "Regular expressions", and "This element is case sensitive".

    This grammar item is at the TOP of the list on the "Grammar" tab, and is NOT checked in the "Importance" tab.

    This scheme works correctly for non-comment lines, like the following line. White spaces at the end of that line are correctly matched to my new grammar item.
    if (GetType()==TYPE_CHEVY)

    Unfortunately, this scheme does NOT work for //-style comment lines, like the following line. White spaces at the end of that line are declared to be part of the comment, rather than part of my new grammar item.
    // This is a test

    I don't understand this behavior. The white space at the end of the comment is matched by both the "Comment" grammar item and my new "end of line white space" grammar item. Since my "end of line white space" grammar item is at the top of the list, shouldn't it win?

    -------------

    Since my first scheme did not work, I deleted my new grammar item and modified the "built in" C++ "Comment" grammar item which defines //-style comments, to be the following...

    Grammar Item Element Name = "Comment"
    Category = Delimited
    Text from = //
    to = (\s*[^\s]+)*
    Check marks are in "Stop at end of line", "Regular expressions", and "This element is case sensitive".

    My "to" RegEx is intended to be (zero or more whitespaces followed by one or more non-whitespace-chars) repeated zero or more times. In other words, if there is white space, it has to be followed by non-whitespace in order to be matched.

    Unfortunately, every single character (including the trailing white space) in all of the following lines is declared to be "Comment" by BC3. I would expect BC3 to declare everything from the "//" to the last "a" (inclusive) to be "Comment" and the trailing spaces to be "Default text". This does not happen, though...

    //
    //a
    // a
    //a a a
    // a a a
    //a a aa
    // a a aa

    -------------

    Is my RegEx wrong, or am I trying to do something that is impossible?

    Thank you very much for any help you can give me in this matter!

    I am running version 3.1.11 build 12204.

    - KevinC

  • #2
    Hello Kevin,

    Thanks for the feedback. Unfortunately, defining the grammar item will not work as you expect.

    The quick answer is to easily do this, you can remove Comments as a grammar from your File Format, reverting that text to Everything Else text. Default whitespace can then be controlled in the Session Settings -> Importance tab (check Leading and Embedded, uncheck Trailing).

    Adding similar controls to individual grammar elements is on our Customer Wishlist.

    The grammar priority list is not absolute. While scanning the line we take into account various optimizations to try and find a match quickly. Your workaround definition of (\s*[^\s]+)* looks reasonable. I'll investigate that idea here.
    Aaron P Scooter Software

    Comment


    • #3
      I talked with one of the developers. How does the "Basic" grammar type:
      Code:
      //(\w|(\s+\w))*
      work for you?

      Or the delimited:
      //
      to
      (\w|(\s+\w))+
      with Stop at End of Line enabled.
      Aaron P Scooter Software

      Comment


      • #4
        Thanks for the speedy reply!

        The "Basic" one works perfectly! Awesome!!!

        The "Delimited" one works in most cases - but when the comment "contains" only whitespace (example = "// "), the whole comment (including the whitespace) is declared to be "Comment", rather than just the "//".

        So, I am going to use the "Basic" one.

        Thank you *VERY* much!

        - KevinC

        Comment

        Working...
        X