private Token ReadDivideCommentOrRegularExpression()
{
// Comment or divide or regular expression.
int c2 = this.reader.Peek();
if (c2 == '*')
{
// Multi-line comment.
// Skip the asterisk.
ReadNextChar();
return ReadMultiLineComment();
}
else if (c2 == '/')
{
// Single-line comment.
// Skip the slash.
ReadNextChar();
return ReadSingleLineComment();
}
else
{
// Divide or regular expression.
// Determine from the context whether the token is a regular expression
// or a division operator.
bool isDivisionOperator;
switch (this.ParserExpressionState)
{
case ParserExpressionState.Literal:
isDivisionOperator = false;
break;
case ParserExpressionState.Operator:
isDivisionOperator = true;
break;
default:
// If the parser context is unknown, the token before the slash is
// what determines whether the token is a divide operator or a
// regular expression literal.
isDivisionOperator =
this.lastSignificantToken is IdentifierToken ||
this.lastSignificantToken is LiteralToken ||
this.lastSignificantToken == PunctuatorToken.RightParenthesis ||
this.lastSignificantToken == PunctuatorToken.Increment ||
this.lastSignificantToken == PunctuatorToken.Decrement ||
this.lastSignificantToken == PunctuatorToken.RightBracket ||
this.lastSignificantToken == PunctuatorToken.RightBrace;
break;
}
if (isDivisionOperator)
{
// Two division operators: "/" and "/=".
if (c2 == '=')
{
ReadNextChar();
return PunctuatorToken.CompoundDivide;
}
else
return PunctuatorToken.Divide;
}
else
{
// Regular expression.
return ReadRegularExpression();
}
}
}