Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for multi-line comments #453

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 47 additions & 13 deletions Source/Parser/Expressions/ExpressionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,32 +95,66 @@ internal virtual void AppendStringLiteral(StringBuilder builder)
AppendString(builder);
}


internal static void SkipWhitespace(PositionalTokenizer tokenizer)
{
tokenizer.SkipWhitespace();
while (tokenizer.Match("//"))

bool matchedSingle = tokenizer.MatchSubstring("//") == 2;
bool matchedMulti = tokenizer.MatchSubstring("/*") == 2;

while (matchedSingle || matchedMulti)
{
var expressionTokenizer = tokenizer as ExpressionTokenizer;
if (expressionTokenizer != null)

if (matchedSingle)
{
int line = tokenizer.Line;
int column = tokenizer.Column - 2;
if (expressionTokenizer != null)
{
int line = tokenizer.Line;
int column = tokenizer.Column;

var comment = tokenizer.ReadTo('\n');
if (comment.Length > 0 && comment[comment.Length - 1] == '\r')
comment = comment.SubToken(0, comment.Length - 1);
var comment = tokenizer.ReadTo('\n');

expressionTokenizer.AddComment(new CommentExpression("//" + comment.ToString())
expressionTokenizer.AddComment(new CommentExpression(comment.ToString())
{
Location = new TextRange(line, column, line, column + comment.Length + 1)
});
}
else
{
Location = new TextRange(line, column, line, column + comment.Length + 1)
});
tokenizer.ReadTo('\n');
}
}
else
else // matchedMulti
{
tokenizer.ReadTo('\n');
}
if (expressionTokenizer != null)
{
int startLine = tokenizer.Line;
int startColumn = tokenizer.Column;

var comment = tokenizer.ReadTo("*/");
tokenizer.Advance(2);

int endLine = tokenizer.Line;
int endColumn = tokenizer.Column;

expressionTokenizer.AddComment(new CommentExpression(comment.ToString() + "*/")
{
Location = new TextRange(startLine, startColumn, endLine, endColumn)
});
}
else
{
tokenizer.ReadTo("*/");
tokenizer.Advance(2);
}
}

tokenizer.SkipWhitespace();

matchedSingle = tokenizer.MatchSubstring("//") == 2;
matchedMulti = tokenizer.MatchSubstring("/*") == 2;
}
Jamiras marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
32 changes: 32 additions & 0 deletions Tests/Parser/Expressions/ExpressionBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,38 @@ public void TestSkipWhitespaceMultiComment()
Assert.That(tokenizer.NextChar, Is.EqualTo('h'));
}

[Test]
public void TestSkipWhitespaceMultilineComment()
{
var tokenizer = new PositionalTokenizer(Tokenizer.CreateTokenizer(" /* multi-line\r\n comment\r\n */\r\nhi"));
ExpressionBase.SkipWhitespace(tokenizer);
Assert.That(tokenizer.NextChar, Is.EqualTo('h'));
}

[Test]
public void TestSkipWhitespaceMultipleMultilineComment()
{
var tokenizer = new PositionalTokenizer(Tokenizer.CreateTokenizer(" /* multi-line\r\n comment\r\n */\r\n/* second */\r\nhi"));
ExpressionBase.SkipWhitespace(tokenizer);
Assert.That(tokenizer.NextChar, Is.EqualTo('h'));
}

[Test]
public void TestSkipWhitespaceMultilineCommentUnclosed()
{
var tokenizer = new PositionalTokenizer(Tokenizer.CreateTokenizer(" /* multi-line\r\n comment\r\nhi"));
ExpressionBase.SkipWhitespace(tokenizer);
Assert.That(tokenizer.NextChar, Is.EqualTo('\0'));
}

[Test]
public void TestSkipWhitespaceMultilineMixedComment()
{
var tokenizer = new PositionalTokenizer(Tokenizer.CreateTokenizer(" // single-line\r\n/* multi-line\r\n comment\r\n */\r\n// second single line\r\n/* second multiline */\r\nhi"));
ExpressionBase.SkipWhitespace(tokenizer);
Assert.That(tokenizer.NextChar, Is.EqualTo('h'));
}

private static PositionalTokenizer CreateTokenizer(string input)
{
return new PositionalTokenizer(Tokenizer.CreateTokenizer(input));
Expand Down
Loading