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

Add support for capturing single-line comments in JSON. #29

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
33 changes: 33 additions & 0 deletions src/Pasper.Json/Parsing/JsonLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ private bool TryGetNextToken([NotNullWhen(true)] out IToken? token)
if (TryGetStringToken(out token))
return true;

if (TryGetSingleLineCommentToken(out token))
return true;

throw new UnexpectedTokenException(_lineNumber, _columnNumber, currentCharacter.ToString());
}

Expand Down Expand Up @@ -129,4 +132,34 @@ private bool TryGetStringToken([NotNullWhen(true)] out IToken? token)
_currentIndex = endIndex + 1;
return true;
}

private bool TryGetSingleLineCommentToken([NotNullWhen(true)] out IToken? token)
{
if (json[_currentIndex] != '/')
{
token = null;
return false;
}

var nextToken = json[_currentIndex + 1];
if (nextToken is not '/')
{
token = null;
return false;
}

// Single line comments can technically be placed on the final line of the specified
// input. The lexer accounts for this by assuming that if no line breaks are detected
// after the start of a comment, that the comment goes to the end of the input.
var startIndex = _currentIndex + 2;
var endIndex = json.IndexOf('\n', startIndex);
if (endIndex == -1)
endIndex = json.Length;

var value = json.Substring(startIndex, endIndex - startIndex);
token = new CommentToken(value.Trim());
_currentIndex = endIndex + 1;
_columnNumber += value.Length + 2;
return true;
}
}
13 changes: 13 additions & 0 deletions test/Pasper.Json.Tests/Parsing/JsonLexer/TryGetNextToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,17 @@ public void TryGetNextToken_EndObjectDetected_ReturnsTrue()
Assert.NotNull(lexer.Current);
Assert.IsType<EndObjectToken>(lexer.Current);
}

[Fact]
public void TryGetNextToken_CommentDetected_ReturnsTrue()
{
const string comment = "this is a comment";
const string testJson = $"// {comment}";
var lexer = new JsonLexer(testJson);
var tokenAvailable = lexer.MoveNext();
Assert.True(tokenAvailable);
Assert.NotNull(lexer.Current);
Assert.IsType<CommentToken>(lexer.Current);
Assert.Equal(comment, lexer.Current.Value);
}
}