Skip to content

Commit 00ac0e4

Browse files
authored
Update README.md
1 parent f5e0564 commit 00ac0e4

File tree

1 file changed

+98
-1
lines changed

1 file changed

+98
-1
lines changed

README.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,98 @@
1-
# ILSourceParser
1+
# ILSourceParser
2+
This library for .NET provides functionality to parse IL source code. If you're not aware, Microsoft Intermediate Language (IL or CIL, formerly known as MSIL) is
3+
what the .NET application typically consists of. C#, Visual Basic, F#, and other well-known official .NET languages compile to IL bytecode. However,
4+
IL actually has its syntax as well. ILSourceParser parses the IL syntax into syntax nodes, making it easier to analyze IL.
5+
6+
This library does not rely on any existing project and is not a port from another language, so chances are there could be some issues with this library. If
7+
you encounter one, please create a new issue post about it!
8+
9+
The known issue is that ILSourceParser might not be the fastest thing in the world, because it took nearly 1 second to analyze a 200-line of code IL file. Maybe
10+
it just needs some additional JIT warming, although there could be a different reason - parsing IL is generally harder than parsing C#, mostly because IL has almost 300
11+
instructions, and we have to do over 300 checks when parsing method body to parse one instruction.
12+
13+
### Example
14+
```cs
15+
var syntaxTree = ILSyntaxTree.ParseText(@".assembly MyAssembly { }");
16+
var root = syntaxTree.GetRoot();
17+
foreach (SyntaxNode node in root.DescendantNodes)
18+
{
19+
// do something with node
20+
}
21+
```
22+
ILSourceParser is generally really similar to [Roslyn](https://github.com/dotnet/roslyn), even though it doesn't rely on it at all.
23+
24+
A more complex example here:
25+
```cs
26+
using ILSourceParser;
27+
using ILSourceParser.Syntax;
28+
using ILSourceParser.Syntax.Instructions;
29+
using Sprache;
30+
31+
string input = @".assembly MyAssembly
32+
{
33+
.ver 1:2:3:4
34+
}
35+
.assembly extern System.Runtime
36+
{
37+
}
38+
.assembly extern System.Console
39+
{
40+
}
41+
42+
// My Cool Application !
43+
44+
.imagebase 0x00400000
45+
.line 123
46+
47+
.class public sequential auto ansi beforefieldinit MyValueType extends [System.Runtime]System.ValueType
48+
{
49+
.field private static initonly string s_myString
50+
51+
.method public static hidebysig void .cctor() cil managed
52+
{
53+
ldstr ""Hello, World!""
54+
stsfld string MyValueType::s_myString
55+
}
56+
57+
.method public static void PrintText() cil managed
58+
{
59+
.maxstack 8
60+
61+
ldsfld string MyValueType::s_myString
62+
call void [System.Console]System.Console::WriteLine(string)
63+
64+
ret
65+
}
66+
}";
67+
var syntaxRoot = ILSyntaxTree.ParseText(input).GetRoot();
68+
69+
foreach (var descendant in syntaxRoot.DescendantNodes)
70+
{
71+
switch (descendant)
72+
{
73+
case AssemblyDeclarationSyntax asm:
74+
Console.WriteLine($"Assembly {asm.AssemblyName}, is external: {asm.IsExtern}");
75+
break;
76+
case BaseCommentSyntax comment:
77+
if (comment is InlineCommentSyntax inline)
78+
{
79+
Console.WriteLine($"Inline comment, text: {inline.CommentText}");
80+
}
81+
else if (comment is MultilineCommentSyntax multiline)
82+
{
83+
Console.WriteLine($"Multiline comment, text: {multiline.CommentText}");
84+
}
85+
break;
86+
case ImageBaseDirectiveSyntax imageBase:
87+
Console.WriteLine($"Image base {imageBase.ImageBase}");
88+
break;
89+
case LineDirectiveSyntax line:
90+
Console.WriteLine($"Line {line.Line}");
91+
break;
92+
case ClassDeclarationSyntax @class:
93+
string flags = string.Join(", ", @class.Flags);
94+
Console.WriteLine($"Class named {@class.Name}; flags: {flags}");
95+
break;
96+
}
97+
}
98+
```

0 commit comments

Comments
 (0)