Implement single-line C++-style comments.

Fixes: #118
This commit is contained in:
David Given 2018-09-02 12:14:59 +02:00
parent 0e9736fdca
commit f8fc5bc3d8
7 changed files with 153 additions and 32 deletions

View 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 == '/')
{
if ((GetChar() == '*') && !InputLevel)
else if ((ch == '/') && !InputLevel)
{
int nch = GetChar();
if (nch == '*')
skipcomment();
}
else if (nch == '/')
skiplinecomment();
else
{
UnGetChar();
@ -284,11 +286,19 @@ go_on: /* rescan, the following character has been read */
break;
case '/':
#ifndef NOPP
if (nch == '*' && !InputLevel)
if (!InputLevel)
{
if (nch == '*')
{
skipcomment();
goto again;
}
else if (nch == '/')
{
skiplinecomment();
goto again;
}
}
#endif
if (nch == '=')
return ptok->tk_symb = DIVAB;
@ -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;

View 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;

View 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,14 +101,23 @@ SkipToNewLine()
}
else if (ch == '/')
{
if (GetChar() == '*' && !InputLevel)
if (!InputLevel)
{
int nch = GetChar();
if (nch == '*')
{
skipcomment();
continue;
}
else if (nch == '/')
{
skiplinecomment();
continue;
}
else
UnGetChar();
}
}
else if (ch == TOKSEP && InputLevel)
{
continue;

View 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,11 +166,19 @@ again: /* rescan the input after an error or replacement */
UnGetChar();
return ptok->tk_symb = ch;
case '/':
if (nch == '*' && !InputLevel)
if (!InputLevel)
{
if (nch == '*')
{
skipcomment();
goto again;
}
else if (nch == '/')
{
skiplinecomment();
goto again;
}
}
else if (nch == '=')
return ptok->tk_symb = DIVAB;
UnGetChar();
@ -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;

View 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;

View file

@ -81,15 +81,21 @@ do_pragma()
}
else if (c == '/')
{
if ((c = GetChar()) != '*' || InputLevel)
if (!InputLevel)
{
*c_ptr++ = '/';
}
else
c = GetChar();
if (c == '*')
{
skipcomment();
continue;
}
else if (c == '/')
{
skiplinecomment();
continue;
}
*c_ptr++ = '/';
}
}
*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;
}

View 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,14 +95,23 @@ SkipToNewLine()
}
else if (ch == '/')
{
if (GetChar() == '*' && !InputLevel)
if (!InputLevel)
{
int nch = GetChar();
if (nch == '*')
{
skipcomment();
continue;
}
else if (nch == '/')
{
skiplinecomment();
continue;
}
else
UnGetChar();
}
}
else if (ch == TOKSEP && InputLevel)
{
continue;