Skip to content

Commit

Permalink
Handle non-standard keywords in SyntaxClassifier
Browse files Browse the repository at this point in the history
Fixes #136
  • Loading branch information
MSDN-WhiteKnight committed Jul 9, 2023
1 parent 0eb1b9e commit ea8e39b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
12 changes: 10 additions & 2 deletions CilTools.BytecodeAnalysis/Syntax/SyntaxClassifier.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* CilTools.BytecodeAnalysis library
* Copyright (c) 2021, MSDN.WhiteKnight (https://github.com/MSDN-WhiteKnight)
* Copyright (c) 2023, MSDN.WhiteKnight (https://github.com/MSDN-WhiteKnight)
* License: BSD 2.0 */
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -479,7 +479,15 @@ internal static class SyntaxClassifier
"with",
"wrapper",
"xor",
"windowsruntime" //not specified in ECMA-335, but used as keyword in .NET Framework implementation
// Not specified in ECMA-335, but used as keywords in Microsoft implementation
// (https://github.com/dotnet/runtime/blob/main/src/coreclr/inc/il_kywd.h)
"aggressiveinlining",
"type",
"uint8",
"uint16",
"uint32",
"uint64",
"windowsruntime"
};

// Excluded:
Expand Down
49 changes: 49 additions & 0 deletions tests/CilTools.BytecodeAnalysis.Tests/Syntax/SyntaxReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,54 @@ public void Test_SyntaxReader_IdSpecialChars()
Assert.AreEqual(string.Empty, node.TrailingWhitespace);
Assert.AreEqual("1", node.ToString());
}

[TestMethod]
public void Test_SyntaxReader_NonStandardKeywords()
{
//https://github.com/MSDN-WhiteKnight/CilTools/issues/136
string s = ".method public instance uint8 FizzBuzz() cil managed aggressiveinlining";
SyntaxNode[] nodes = SyntaxReader.ReadAllNodes(s);
Assert.AreEqual(10, nodes.Length);

SyntaxNode node = nodes[0];
Assert.IsTrue(node is KeywordSyntax);
Assert.AreEqual(".method", (node as KeywordSyntax).Content);

node = nodes[1];
Assert.IsTrue(node is KeywordSyntax);
Assert.AreEqual("public", (node as KeywordSyntax).Content);

node = nodes[2];
Assert.IsTrue(node is KeywordSyntax);
Assert.AreEqual("instance", (node as KeywordSyntax).Content);

node = nodes[3];
Assert.IsTrue(node is KeywordSyntax);
Assert.AreEqual("uint8", (node as KeywordSyntax).Content);

node = nodes[4];
Assert.IsTrue(node is IdentifierSyntax);
Assert.AreEqual("FizzBuzz", (node as IdentifierSyntax).Content);

node = nodes[5];
Assert.IsTrue(node is PunctuationSyntax);
Assert.AreEqual("(", (node as PunctuationSyntax).Content);

node = nodes[6];
Assert.IsTrue(node is PunctuationSyntax);
Assert.AreEqual(")", (node as PunctuationSyntax).Content);

node = nodes[7];
Assert.IsTrue(node is KeywordSyntax);
Assert.AreEqual("cil", (node as KeywordSyntax).Content);

node = nodes[8];
Assert.IsTrue(node is KeywordSyntax);
Assert.AreEqual("managed", (node as KeywordSyntax).Content);

node = nodes[9];
Assert.IsTrue(node is KeywordSyntax);
Assert.AreEqual("aggressiveinlining", (node as KeywordSyntax).Content);
}
}
}

0 comments on commit ea8e39b

Please sign in to comment.