#847 Trigraph line continuation character in C/C++

Won't_Implement
open
Scintilla (358)
2
2011-08-25
2011-08-25
Loreia2
No

Hi

Scintilla easily recognizes '\\' as line continuation character, but lacks support for trigraphs. And C defines this trigraph sequence "??/" as replacement for backslash '\\'.

Therefore, this is recognized correctly (both lines are colorized as comment)
// save file to c:\\ int i = 0;

But this is not recognized as comment continuation (even if it is essentially the same thing)
// Will the next line be executed????????????????/
int i = 0;

LexCpp.cxx should be expanded to catch the trigraph case too.

On a bigger scale, perhaps LexCPP.cxx could be expanded to give full support for Digraphs and Trigraphs:
http://en.wikipedia.org/wiki/Digraphs_and_trigraphs

Best regards,
Loreia

Discussion

  • Neil Hodgson

    Neil Hodgson - 2011-08-25

    Digraphs and trigraphs are a pretty much useless feature and gcc disables trigraphs by default. The example does not appear to be from any conscious use of trigraphs but is merely an accident.

    If you really want to implement trigraphs then go ahead but I will not be spending any time on this.

     
  • Neil Hodgson

    Neil Hodgson - 2011-08-25
    • assigned_to: nobody --> nyamatongwe
    • priority: 5 --> 2
    • milestone: --> Won't_Implement
     
  • Loreia2

    Loreia2 - 2011-08-26

    Hi Niel,

    I agree that this feature is rarely used and a bad practice. But in Visual studio this is not turned off by default, and you can accidentally comment out entire line without realizing it. Correct coloring would be most helpful in finding the cause of error. It will be triggered rarely but it might save hours (if not days) when someone runs into this and for no obvious reason correct line of code stops executing. In those situations this would be a life-saver feature.

    As far as implementation goes, it is as simple as changing this line:
    // Handle line continuation generically.
    if (sc.ch == '\\') {
    into this:
    // Handle line continuation generically.
    if (sc.ch == '\\' || (sc.ch == '/' && sc.chPrev == '?' && styler.SafeGetCharAt(sc.currentPos-2, 0) == '?')) {

    Also, at startPos new code would be:
    // Set continuationLine if last character of previous line is '\' or "??/"
    if (lineCurrent > 0) {
    int chBack = styler.SafeGetCharAt(startPos-1, 0);
    int chBack2 = styler.SafeGetCharAt(startPos-2, 0);
    int lineEndChar = styler.SafeGetCharAt(startPos-3, 0);
    int chBack4 = styler.SafeGetCharAt(startPos-4, 0);
    int chBack5 = '!';
    if (chBack2 == '\r' && chBack == '\n') {
    chBack5 = styler.SafeGetCharAt(startPos-5, 0);
    } else if (chBack == '\n' || chBack == '\r') {
    chBack5 = chBack4;
    chBack4 = lineEndChar;
    lineEndChar = chBack2;
    }
    continuationLine = lineEndChar == '\\' || (lineEndChar == '/' && chBack4 == '?' && chBack5 == '?');
    }

    I just compiled this, and it works.
    As I said, this might be user rarely, but it will be worth a million for those that run into this ugly "feature" of C.

    Thank you and best regards,
    Loreia

     
  • Neil Hodgson

    Neil Hodgson - 2011-08-27

    That patch is too much code for just doing one aspect of trigraphs.

     
  • Loreia2

    Loreia2 - 2011-08-27

    Hi Niel,

    I took some time to analyze the issue (of digraphs and trigraphs) a bit more thoroughly, and since all characters that digraph and trigraphs are made of, are already operators in C/C++, coloring is already implemented, and the only thing left to do is to:
    a) add support for line comment continuation trigraph ("?/")
    b) add support for code folding when "??<" (replacement for '{') and "??>" (replacement for '}') trigraphs (and corresponding digraphs ("<%" and "%>")) are found in source code.

    So, I created three patch files:
    1) patch_01 contains implementation for case a) line continuation
    2) patch_02 extends patch_01 by adding support for code folding when digraphs and trigraphs are found in source code (but not in comments)
    <%
    this is foldable
    %>
    //??<
    this is not foldable
    //??>
    3) patch_03 extends patch_02 by adding support for code folding in line comments.
    <%
    this is foldable
    %>
    //??<
    in patch_03 even this is foldable
    //??>

    I fixed the code you rejected yesterday, it it more concise now.
    Also, I believe patch_02 might be the best choice. Making //??< fold code might surprise user if he creates such comment accidentally, and if he/she wants to use it, they can always explicitly state so by setting "fold.cpp.explicit.start" and "fold.cpp.explicit.end"

    Just to illustrate it, this is a CPP build with latest Scintilla 2.28
    http://i54.tinypic.com/n3pxcn.png

    Thank you and best regards,
    Loreia

     
  • Loreia2

    Loreia2 - 2011-08-27
     
  • Loreia2

    Loreia2 - 2011-08-27
     
  • Loreia2

    Loreia2 - 2011-08-27
     
  • Neil Hodgson

    Neil Hodgson - 2011-08-31

    Since trigraphs are normally errors, it may be better to introduce a new style for trigraphs and display that in red.

     

Log in to post a comment.