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; static LexSP = 0;
void skipcomment(); void skipcomment();
void skiplinecomment();
/* In PushLex() the actions are taken in order to initialise or /* In PushLex() the actions are taken in order to initialise or
re-initialise the lexical scanner. re-initialise the lexical scanner.
@ -168,12 +169,13 @@ go_on: /* rescan, the following character has been read */
goto firstline; goto firstline;
} }
} }
else if (ch == '/') else if ((ch == '/') && !InputLevel)
{
if ((GetChar() == '*') && !InputLevel)
{ {
int nch = GetChar();
if (nch == '*')
skipcomment(); skipcomment();
} else if (nch == '/')
skiplinecomment();
else else
{ {
UnGetChar(); UnGetChar();
@ -284,11 +286,19 @@ go_on: /* rescan, the following character has been read */
break; break;
case '/': case '/':
#ifndef NOPP #ifndef NOPP
if (nch == '*' && !InputLevel) if (!InputLevel)
{
if (nch == '*')
{ {
skipcomment(); skipcomment();
goto again; goto again;
} }
else if (nch == '/')
{
skiplinecomment();
goto again;
}
}
#endif #endif
if (nch == '=') if (nch == '=')
return ptok->tk_symb = DIVAB; return ptok->tk_symb = DIVAB;
@ -539,6 +549,22 @@ void skipcomment()
#endif /* LINT */ #endif /* LINT */
NoUnstack--; 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 */ #endif /* NOPP */
arith char_constant(nm) char* nm; arith char_constant(nm) char* nm;

View file

@ -190,13 +190,18 @@ void skip_block(to_endif) int to_endif;
if (ch == '/') if (ch == '/')
{ {
ch = GetChar(); ch = GetChar();
if (ch != '*') if (ch == '/')
UnGetChar(); {
else skiplinecomment();
continue;
}
else if (ch == '*')
{ {
skipcomment(); skipcomment();
continue; continue;
} }
else
UnGetChar();
} }
else else
UnGetChar(); UnGetChar();
@ -748,6 +753,13 @@ int* length;
c = GetChar(); c = GetChar();
continue; continue;
} }
else if (c == '/')
{
skiplinecomment();
blank++;
c = GetChar();
continue;
}
if (blank) if (blank)
{ {
blank = 0; blank = 0;

View file

@ -48,6 +48,11 @@ int skipspaces(ch, skipnl) register int ch;
skipcomment(); skipcomment();
ch = GetChar(); ch = GetChar();
} }
else if (ch == '/' && !InputLevel)
{
skiplinecomment();
ch = GetChar();
}
else else
{ {
UnGetChar(); UnGetChar();
@ -96,14 +101,23 @@ SkipToNewLine()
} }
else if (ch == '/') else if (ch == '/')
{ {
if (GetChar() == '*' && !InputLevel) if (!InputLevel)
{
int nch = GetChar();
if (nch == '*')
{ {
skipcomment(); skipcomment();
continue; continue;
} }
else if (nch == '/')
{
skiplinecomment();
continue;
}
else else
UnGetChar(); UnGetChar();
} }
}
else if (ch == TOKSEP && InputLevel) else if (ch == TOKSEP && InputLevel)
{ {
continue; continue;

View file

@ -36,6 +36,7 @@ extern arith char_constant();
#define FLG_DOTSEEN 0x02 /* certainly a floating point number */ #define FLG_DOTSEEN 0x02 /* certainly a floating point number */
void skipcomment(); void skipcomment();
void skiplinecomment(void);
int LLlex() int LLlex()
{ {
@ -165,11 +166,19 @@ again: /* rescan the input after an error or replacement */
UnGetChar(); UnGetChar();
return ptok->tk_symb = ch; return ptok->tk_symb = ch;
case '/': case '/':
if (nch == '*' && !InputLevel) if (!InputLevel)
{
if (nch == '*')
{ {
skipcomment(); skipcomment();
goto again; goto again;
} }
else if (nch == '/')
{
skiplinecomment();
goto again;
}
}
else if (nch == '=') else if (nch == '=')
return ptok->tk_symb = DIVAB; return ptok->tk_symb = DIVAB;
UnGetChar(); UnGetChar();
@ -412,6 +421,22 @@ void skipcomment()
NoUnstack--; 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; arith char_constant(nm) char* nm;
{ {
register arith val = 0; register arith val = 0;

View file

@ -187,13 +187,18 @@ void skip_block(to_endif) int to_endif;
} }
if (ch == '/') if (ch == '/')
{ {
if (ch != '*') if (ch == '*')
UnGetChar();
else
{ {
skipcomment(); skipcomment();
continue; continue;
} }
else if (ch == '/')
{
skiplinecomment();
continue;
}
else
UnGetChar();
} }
else else
UnGetChar(); UnGetChar();
@ -769,6 +774,13 @@ int* length;
c = GetChar(); c = GetChar();
continue; continue;
} }
else if (c == '/')
{
skiplinecomment();
blank++;
c = GetChar();
continue;
}
if (blank) if (blank)
{ {
blank = 0; blank = 0;

View file

@ -81,15 +81,21 @@ do_pragma()
} }
else if (c == '/') else if (c == '/')
{ {
if ((c = GetChar()) != '*' || InputLevel) if (!InputLevel)
{ {
*c_ptr++ = '/'; c = GetChar();
} if (c == '*')
else
{ {
skipcomment(); skipcomment();
continue; continue;
} }
else if (c == '/')
{
skiplinecomment();
continue;
}
*c_ptr++ = '/';
}
} }
*c_ptr++ = c; *c_ptr++ = c;
c = GetChar(); c = GetChar();
@ -238,6 +244,12 @@ void preprocess(fn) char* fn;
c = GetChar(); c = GetChar();
continue; continue;
} }
else if (c == '/')
{
skiplinecomment();
c = GetChar();
continue;
}
UnGetChar(); UnGetChar();
c = '/'; c = '/';
} }
@ -290,6 +302,12 @@ void preprocess(fn) char* fn;
c = GetChar(); c = GetChar();
continue; continue;
} }
else if (c == '/')
{
skiplinecomment();
c = GetChar();
continue;
}
echo('/'); echo('/');
continue; continue;
} }

View file

@ -46,6 +46,11 @@ int skipspaces(ch, skipnl) register int ch;
skipcomment(); skipcomment();
ch = GetChar(); ch = GetChar();
} }
else if (ch == '/' && !InputLevel)
{
skiplinecomment();
ch = GetChar();
}
else else
{ {
UnGetChar(); UnGetChar();
@ -90,14 +95,23 @@ SkipToNewLine()
} }
else if (ch == '/') else if (ch == '/')
{ {
if (GetChar() == '*' && !InputLevel) if (!InputLevel)
{
int nch = GetChar();
if (nch == '*')
{ {
skipcomment(); skipcomment();
continue; continue;
} }
else if (nch == '/')
{
skiplinecomment();
continue;
}
else else
UnGetChar(); UnGetChar();
} }
}
else if (ch == TOKSEP && InputLevel) else if (ch == TOKSEP && InputLevel)
{ {
continue; continue;