Implement single-line C++-style comments.
authorDavid Given <dg@cowlark.com>
Sun, 2 Sep 2018 10:14:59 +0000 (12:14 +0200)
committerDavid Given <dg@cowlark.com>
Sun, 2 Sep 2018 10:14:59 +0000 (12:14 +0200)
Fixes: #118

lang/cem/cemcom.ansi/LLlex.c
lang/cem/cemcom.ansi/domacro.c
lang/cem/cemcom.ansi/skip.c
lang/cem/cpp.ansi/LLlex.c
lang/cem/cpp.ansi/domacro.c
lang/cem/cpp.ansi/preprocess.c
lang/cem/cpp.ansi/skip.c

index 331a08e..1c69f35 100644 (file)
@@ -53,6 +53,7 @@ static struct token LexStack[MAX_LL_DEPTH];
 static LexSP = 0;
 
 void skipcomment();
+void skiplinecomment();
 
 /*     In PushLex() the actions are taken in order to initialise or
     re-initialise the lexical scanner.
@@ -168,12 +169,13 @@ go_on: /* rescan, the following character has been read   */
                                                goto firstline;
                                        }
                                }
-                               else if (ch == '/')
+                               else if ((ch == '/') && !InputLevel)
                                {
-                                       if ((GetChar() == '*') && !InputLevel)
-                                       {
+                                       int nch = GetChar();
+                                       if (nch == '*')
                                                skipcomment();
-                                       }
+                                       else if (nch == '/')
+                                               skiplinecomment();
                                        else
                                        {
                                                UnGetChar();
@@ -284,10 +286,18 @@ go_on: /* rescan, the following character has been read   */
                                        break;
                                case '/':
 #ifndef NOPP
-                                       if (nch == '*' && !InputLevel)
+                                       if (!InputLevel)
                                        {
-                                               skipcomment();
-                                               goto again;
+                                               if (nch == '*')
+                                               {
+                                                       skipcomment();
+                                                       goto again;
+                                               }
+                                               else if (nch == '/')
+                                               {
+                                                       skiplinecomment();
+                                                       goto again;
+                                               }
                                        }
 #endif
                                        if (nch == '=')
@@ -539,6 +549,22 @@ void skipcomment()
 #endif /* LINT */
        NoUnstack--;
 }
+
+void skiplinecomment(void)
+{
+       /*      The last character read has been the '/' of '//'. We read
+           and discard all characters up to but not including the next
+               NL. */
+       
+       for (;;) {
+               int c = GetChar();
+               if ((class(c) == STNL) || (c == EOI))
+               {
+                       UnGetChar();
+                       break;
+               }
+       }
+}
 #endif /* NOPP */
 
 arith char_constant(nm) char* nm;
index 959630e..cce775f 100644 (file)
@@ -190,13 +190,18 @@ void skip_block(to_endif) int to_endif;
                        if (ch == '/')
                        {
                                ch = GetChar();
-                               if (ch != '*')
-                                       UnGetChar();
-                               else
+                               if (ch == '/')
+                               {
+                                       skiplinecomment();
+                                       continue;
+                               }
+                               else if (ch == '*')
                                {
                                        skipcomment();
                                        continue;
                                }
+                               else
+                                       UnGetChar();
                        }
                        else
                                UnGetChar();
@@ -748,6 +753,13 @@ int* length;
                                c = GetChar();
                                continue;
                        }
+                       else if (c == '/')
+                       {
+                               skiplinecomment();
+                               blank++;
+                               c = GetChar();
+                               continue;
+                       }
                        if (blank)
                        {
                                blank = 0;
index a3eabe2..ac10e58 100644 (file)
@@ -48,6 +48,11 @@ int skipspaces(ch, skipnl) register int ch;
                                skipcomment();
                                ch = GetChar();
                        }
+                       else if (ch == '/' && !InputLevel)
+                       {
+                               skiplinecomment();
+                               ch = GetChar();
+                       }
                        else
                        {
                                UnGetChar();
@@ -96,13 +101,22 @@ SkipToNewLine()
                }
                else if (ch == '/')
                {
-                       if (GetChar() == '*' && !InputLevel)
+                       if (!InputLevel)
                        {
-                               skipcomment();
-                               continue;
+                               int nch = GetChar();
+                               if (nch == '*')
+                               {
+                                       skipcomment();
+                                       continue;
+                               }
+                               else if (nch == '/')
+                               {
+                                       skiplinecomment();
+                                       continue;
+                               }
+                               else
+                                       UnGetChar();
                        }
-                       else
-                               UnGetChar();
                }
                else if (ch == TOKSEP && InputLevel)
                {
index 0fc9ec8..40c6c57 100644 (file)
@@ -36,6 +36,7 @@ extern arith char_constant();
 #define FLG_DOTSEEN 0x02 /* certainly a floating point number */
 
 void skipcomment();
+void skiplinecomment(void);
 
 int LLlex()
 {
@@ -165,10 +166,18 @@ again: /* rescan the input after an error or replacement  */
                                        UnGetChar();
                                        return ptok->tk_symb = ch;
                                case '/':
-                                       if (nch == '*' && !InputLevel)
+                                       if (!InputLevel)
                                        {
-                                               skipcomment();
-                                               goto again;
+                                               if (nch == '*')
+                                               {
+                                                       skipcomment();
+                                                       goto again;
+                                               }
+                                               else if (nch == '/')
+                                               {
+                                                       skiplinecomment();
+                                                       goto again;
+                                               }
                                        }
                                        else if (nch == '=')
                                                return ptok->tk_symb = DIVAB;
@@ -412,6 +421,22 @@ void skipcomment()
        NoUnstack--;
 }
 
+void skiplinecomment(void)
+{
+       /*      The last character read has been the '/' of '//'. We read
+           and discard all characters up to but not including the next
+               NL. */
+       
+       for (;;) {
+               int c = GetChar();
+               if ((class(c) == STNL) || (c == EOI))
+               {
+                       UnGetChar();
+                       break;
+               }
+       }
+}
+
 arith char_constant(nm) char* nm;
 {
        register arith val = 0;
index c84ace4..2376173 100644 (file)
@@ -187,13 +187,18 @@ void skip_block(to_endif) int to_endif;
                        }
                        if (ch == '/')
                        {
-                               if (ch != '*')
-                                       UnGetChar();
-                               else
+                               if (ch == '*')
                                {
                                        skipcomment();
                                        continue;
                                }
+                               else if (ch == '/')
+                               {
+                                       skiplinecomment();
+                                       continue;
+                               }
+                               else
+                                       UnGetChar();
                        }
                        else
                                UnGetChar();
@@ -769,6 +774,13 @@ int* length;
                                c = GetChar();
                                continue;
                        }
+                       else if (c == '/')
+                       {
+                               skiplinecomment();
+                               blank++;
+                               c = GetChar();
+                               continue;
+                       }
                        if (blank)
                        {
                                blank = 0;
index e13797e..ed8119c 100644 (file)
@@ -81,15 +81,21 @@ do_pragma()
                }
                else if (c == '/')
                {
-                       if ((c = GetChar()) != '*' || InputLevel)
+                       if (!InputLevel)
                        {
+                               c = GetChar();
+                               if (c == '*')
+                               {
+                                       skipcomment();
+                                       continue;
+                               }
+                               else if (c == '/')
+                               {
+                                       skiplinecomment();
+                                       continue;
+                               }
                                *c_ptr++ = '/';
                        }
-                       else
-                       {
-                               skipcomment();
-                               continue;
-                       }
                }
                *c_ptr++ = c;
                c = GetChar();
@@ -238,6 +244,12 @@ void preprocess(fn) char* fn;
                                                        c = GetChar();
                                                        continue;
                                                }
+                                               else if (c == '/')
+                                               {
+                                                       skiplinecomment();
+                                                       c = GetChar();
+                                                       continue;
+                                               }
                                                UnGetChar();
                                                c = '/';
                                        }
@@ -290,6 +302,12 @@ void preprocess(fn) char* fn;
                                        c = GetChar();
                                        continue;
                                }
+                               else if (c == '/')
+                               {
+                                       skiplinecomment();
+                                       c = GetChar();
+                                       continue;
+                               }
                                echo('/');
                                continue;
                        }
index 329cef0..1783562 100644 (file)
@@ -46,6 +46,11 @@ int skipspaces(ch, skipnl) register int ch;
                                skipcomment();
                                ch = GetChar();
                        }
+                       else if (ch == '/' && !InputLevel)
+                       {
+                               skiplinecomment();
+                               ch = GetChar();
+                       }
                        else
                        {
                                UnGetChar();
@@ -90,13 +95,22 @@ SkipToNewLine()
                }
                else if (ch == '/')
                {
-                       if (GetChar() == '*' && !InputLevel)
+                       if (!InputLevel)
                        {
-                               skipcomment();
-                               continue;
+                               int nch = GetChar();
+                               if (nch == '*')
+                               {
+                                       skipcomment();
+                                       continue;
+                               }
+                               else if (nch == '/')
+                               {
+                                       skiplinecomment();
+                                       continue;
+                               }
+                               else
+                                       UnGetChar();
                        }
-                       else
-                               UnGetChar();
                }
                else if (ch == TOKSEP && InputLevel)
                {