栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

有没有办法在C#中实现自定义语言功能?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

有没有办法在C#中实现自定义语言功能?

Microsoft提出将Rolsyn
API作为带有公共API的C#编译器的实现。它为每个编译器管道阶段包含单独的API:语法分析,符号创建,绑定,MSIL发出。您可以提供自己的语法解析器实现或扩展现有的语法解析器实现,以便获得具有所需功能的C#编译器。

罗斯林CTP

让我们使用Roslyn扩展C#语言!在我的示例中,我将替换带有相应的do-while的do-until语句:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Roslyn.Compilers.CSharp;namespace RoslynTest{    class Program    {        static void Main(string[] args)        { var pre = @" using System; class Program {     public void My() {         var i = 5;         do {  Console.WriteLine(""hello world"");  i++;         }         until (i > 10);     } } "; //Parsing input pre into a SynaxTree object. var syntaxTree = SyntaxTree.ParseCompilationUnit(pre); var syntaxRoot = syntaxTree.GetRoot(); //Here we will keep all nodes to replace var replaceDictionary = new Dictionary<DoStatementSyntax, DoStatementSyntax>(); //Looking for do-until statements in all descendant nodes foreach (var doStatement in syntaxRoot.DescendantNodes().OfType<DoStatementSyntax>()) {     //Until token is treated as an identifier by C# compiler. It doesn't know that in our case it is a keyword.     var untilNode = doStatement.Condition.ChildNodes().OfType<IdentifierNameSyntax>().FirstOrDefault((_node =>     {         return _node.Identifier.ValueText == "until";     }));     //Condition is treated as an argument list     var conditionNode = doStatement.Condition.ChildNodes().OfType<ArgumentListSyntax>().FirstOrDefault();     if (untilNode != null && conditionNode != null)     {         //Let's replace identifier w/ correct while keyword and condition         var whileNode = Syntax.ParseToken("while");         var condition = Syntax.Parseexpression("(!" + conditionNode.GetFullText() + ")");         var newDoStatement = doStatement.WithWhileKeyword(whileNode).WithCondition(condition);         //Accumulating all replacements         replaceDictionary.Add(doStatement, newDoStatement);     } } syntaxRoot = syntaxRoot.ReplaceNodes(replaceDictionary.Keys, (node1, node2) => replaceDictionary[node1]); //Output preprocessed pre Console.WriteLine(syntaxRoot.GetFullText());        }    }}/////////////OUTPUT://///////////// using System;// class Program {//     public void My() {//         var i = 5;//         do {//  Console.WriteLine("hello world");//  i++;//         }//while(!(i > 10));//     }// }

现在,我们可以使用Roslyn API编译更新的语法树,或将语法Root.GetFullText()保存到文本文件并将其传递给csc.exe。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/465371.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号