From: David Given Date: Sun, 2 Sep 2018 10:14:59 +0000 (+0200) Subject: Implement single-line C++-style comments. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=f8fc5bc3d8ccc9546d2810d2dafec4f777d50d6c;p=ack.git Implement single-line C++-style comments. Fixes: #118 --- diff --git a/lang/cem/cemcom.ansi/LLlex.c b/lang/cem/cemcom.ansi/LLlex.c index 331a08e9e..1c69f352d 100644 --- a/lang/cem/cemcom.ansi/LLlex.c +++ b/lang/cem/cemcom.ansi/LLlex.c @@ -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; diff --git a/lang/cem/cemcom.ansi/domacro.c b/lang/cem/cemcom.ansi/domacro.c index 959630e6a..cce775f64 100644 --- a/lang/cem/cemcom.ansi/domacro.c +++ b/lang/cem/cemcom.ansi/domacro.c @@ -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; diff --git a/lang/cem/cemcom.ansi/skip.c b/lang/cem/cemcom.ansi/skip.c index a3eabe291..ac10e5889 100644 --- a/lang/cem/cemcom.ansi/skip.c +++ b/lang/cem/cemcom.ansi/skip.c @@ -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) { diff --git a/lang/cem/cpp.ansi/LLlex.c b/lang/cem/cpp.ansi/LLlex.c index 0fc9ec86c..40c6c57dc 100644 --- a/lang/cem/cpp.ansi/LLlex.c +++ b/lang/cem/cpp.ansi/LLlex.c @@ -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; diff --git a/lang/cem/cpp.ansi/domacro.c b/lang/cem/cpp.ansi/domacro.c index c84ace415..2376173ca 100644 --- a/lang/cem/cpp.ansi/domacro.c +++ b/lang/cem/cpp.ansi/domacro.c @@ -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; diff --git a/lang/cem/cpp.ansi/preprocess.c b/lang/cem/cpp.ansi/preprocess.c index e13797eef..ed8119cf5 100644 --- a/lang/cem/cpp.ansi/preprocess.c +++ b/lang/cem/cpp.ansi/preprocess.c @@ -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; } diff --git a/lang/cem/cpp.ansi/skip.c b/lang/cem/cpp.ansi/skip.c index 329cef07f..1783562bb 100644 --- a/lang/cem/cpp.ansi/skip.c +++ b/lang/cem/cpp.ansi/skip.c @@ -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) {