Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsequitur committed Feb 13, 2025
1 parent c125401 commit 27a1b20
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,26 @@ public partial class PolyglotSyntaxParserTests
{
public class Directives
{
[Fact]
public void Pound_r_nuget_is_parsed_as_a_compiler_directive_node_in_csharp()
[Theory]
[InlineData("""
var x = 1;
#r "nuget:SomePackage"
x
""",
"#r \"nuget:SomePackage\"")]
[InlineData(""""
var x = 1;
#r """nuget:SomePackage"""
x
"""",
""""
#r """nuget:SomePackage"""
"""")]
public void Pound_r_nuget_is_parsed_as_a_compiler_directive_node_in_csharp(
string codeSubmission,
string expectedDirectiveText)
{
var tree = Parse("var x = 1;\n#r \"nuget:SomePackage\"\nx", "csharp");
var tree = Parse(codeSubmission, "csharp");

var node = tree.RootNode
.ChildNodes
Expand All @@ -27,15 +43,38 @@ public void Pound_r_nuget_is_parsed_as_a_compiler_directive_node_in_csharp()
.Which;
node.Text
.Should()
.Be("#r \"nuget:SomePackage\"");
.Be(expectedDirectiveText);

node.Kind.Should().Be(DirectiveNodeKind.CompilerDirective);

var expectedParameterValue = expectedDirectiveText.Replace("#r", "").Trim();

node.DescendantNodesAndTokens()
.Should().ContainSingle<DirectiveParameterValueNode>()
.Which.Text
.Should().Be(expectedParameterValue);
}

[Fact]
public void Pound_r_nuget_is_parsed_as_a_compiler_directive_node_in_fsharp()
[Theory]
[InlineData("""
var x = 1;
#r "nuget:SomePackage"
x
""",
"#r \"nuget:SomePackage\"")]
[InlineData(""""
var x = 1;
#r """nuget:SomePackage"""
x
"""",
""""
#r """nuget:SomePackage"""
"""")]
public void Pound_r_nuget_is_parsed_as_a_compiler_directive_node_in_fsharp(
string codeSubmission,
string expectedDirectiveText)
{
var tree = Parse("var x = 1;\n#r \"nuget:SomePackage\"\nx", "fsharp");
var tree = Parse(codeSubmission, "fsharp");

var node = tree.RootNode
.ChildNodes
Expand All @@ -44,9 +83,16 @@ public void Pound_r_nuget_is_parsed_as_a_compiler_directive_node_in_fsharp()
.Which;
node.Text
.Should()
.Be("#r \"nuget:SomePackage\"");
.Be(expectedDirectiveText);

node.Kind.Should().Be(DirectiveNodeKind.CompilerDirective);

var expectedParameterValue = expectedDirectiveText.Replace("#r", "").Trim();

node.DescendantNodesAndTokens()
.Should().ContainSingle<DirectiveParameterValueNode>()
.Which.Text
.Should().Be(expectedParameterValue);
}

[Fact]
Expand Down
16 changes: 10 additions & 6 deletions src/Microsoft.DotNet.Interactive/Parsing/PolyglotSyntaxParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,16 @@ void ParsePossibleJsonValueInto(SyntaxNode node)

ConsumeCurrentTokenInto(node);

if (quoteCount is 2)
// triple-quoted string
if (CurrentToken is { Kind: TokenKind.Punctuation } and { Text: "\"" } &&
CurrentTokenPlus(1) is { Kind: TokenKind.Punctuation } and { Text: "\"" })
{
ConsumeCurrentTokenInto(node);
ConsumeCurrentTokenInto(node);
quoteCount += 2;
}

if (quoteCount is 2 or 6)
{
break;
}
Expand Down Expand Up @@ -497,11 +506,6 @@ currentChar is

if (illegalEscapeChars > 0)
{
if (legalEscapeChars > 0)
{

}

return false;
}
}
Expand Down

0 comments on commit 27a1b20

Please sign in to comment.