本文整理汇总了C#中CSharpParser类的典型用法代码示例。如果您正苦于以下问题:C# CSharpParser类的具体用法?C# CSharpParser怎么用?C# CSharpParser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CSharpParser类属于命名空间,在下文中一共展示了CSharpParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UsingAliasDeclarationTest
public void UsingAliasDeclarationTest()
{
string program = "using TESTME=System;\n" +
"using myAlias=My.Name.Space;\n" +
"using StringCollection = System.Collections.Generic.List<string>;\n";
CSharpParser parser = new CSharpParser();
SyntaxTree syntaxTree = parser.Parse(program);
Assert.IsFalse(parser.HasErrors);
Assert.IsTrue(3 <= syntaxTree.Children.Count());
Assert.IsTrue(syntaxTree.Children.ElementAt(0) is UsingAliasDeclaration);
UsingAliasDeclaration ud = (UsingAliasDeclaration)syntaxTree.Children.ElementAt(0);
Assert.AreEqual("TESTME", ud.Alias);
Assert.AreEqual("System", ud.Import.ToString());
Assert.IsTrue(syntaxTree.Children.Where (c => c.Role != Roles.NewLine).ElementAt(1) is UsingAliasDeclaration);
ud = (UsingAliasDeclaration)syntaxTree.Children.Where (c => c.Role != Roles.NewLine).ElementAt(1);
Assert.AreEqual("myAlias", ud.Alias);
Assert.AreEqual("My.Name.Space", ud.Import.ToString());
Assert.IsTrue(syntaxTree.Children.Where (c => c.Role != Roles.NewLine).ElementAt(2) is UsingAliasDeclaration);
ud = (UsingAliasDeclaration)syntaxTree.Children.Where (c => c.Role != Roles.NewLine).ElementAt(2);
Assert.AreEqual("StringCollection", ud.Alias);
Assert.AreEqual("System.Collections.Generic.List<string>", ud.Import.ToString());
}
示例2: UsingWithAliasing
public void UsingWithAliasing()
{
string program = "using global::System;\n" +
"using myAlias=global::My.Name.Space;\n" +
"using a::b.c;\n";
CSharpParser parser = new CSharpParser();
CompilationUnit cu = parser.Parse(new StringReader(program));
Assert.IsFalse(parser.HasErrors);
Assert.AreEqual(3, cu.Children.Count());
Assert.IsTrue(cu.Children.ElementAt(0) is UsingDeclaration);
Assert.IsFalse(cu.Children.ElementAt(0) is UsingAliasDeclaration);
UsingDeclaration ud = (UsingDeclaration)cu.Children.ElementAt(0);
Assert.AreEqual("global::System", ud.Namespace);
Assert.IsTrue(cu.Children.ElementAt(1) is UsingAliasDeclaration);
UsingAliasDeclaration uad = (UsingAliasDeclaration)cu.Children.ElementAt(1);
Assert.AreEqual("myAlias", uad.Alias);
Assert.AreEqual("global::My.Name.Space", uad.Import.ToString());
Assert.IsTrue(cu.Children.ElementAt(2) is UsingDeclaration);
Assert.IsFalse(cu.Children.ElementAt(2) is UsingAliasDeclaration);
ud = (UsingDeclaration)cu.Children.ElementAt(2);
Assert.AreEqual("a::b.c", ud.Namespace);
}
示例3: UsingAliasDeclarationTest
public void UsingAliasDeclarationTest()
{
string program = "using TESTME=System;\n" +
"using myAlias=My.Name.Space;\n" +
"using StringCollection = System.Collections.Generic.List<string>;\n";
CSharpParser parser = new CSharpParser();
CompilationUnit cu = parser.Parse(new StringReader(program));
Assert.IsFalse(parser.HasErrors);
Assert.AreEqual(3, cu.Children.Count());
Assert.IsTrue(cu.Children.ElementAt(0) is UsingAliasDeclaration);
UsingAliasDeclaration ud = (UsingAliasDeclaration)cu.Children.ElementAt(0);
Assert.AreEqual("TESTME", ud.Alias);
Assert.AreEqual("System", ud.Import.ToString());
Assert.IsTrue(cu.Children.ElementAt(1) is UsingAliasDeclaration);
ud = (UsingAliasDeclaration)cu.Children.ElementAt(1);
Assert.AreEqual("myAlias", ud.Alias);
Assert.AreEqual("My.Name.Space", ud.Import.ToString());
Assert.IsTrue(cu.Children.ElementAt(2) is UsingAliasDeclaration);
ud = (UsingAliasDeclaration)cu.Children.ElementAt(2);
Assert.AreEqual("StringCollection", ud.Alias);
Assert.AreEqual("System.Collections.Generic.List<string>", ud.Import.ToString());
}
示例4: ResolveCref
public override IEntity ResolveCref(string cref)
{
if (cref.Length > 2 && cref[1] == ':') {
// resolve ID string
return base.ResolveCref(cref);
}
var documentationReference = new CSharpParser().ParseDocumentationReference(cref);
var csharpContext = context as CSharpTypeResolveContext;
CSharpResolver resolver;
if (csharpContext != null) {
resolver = new CSharpResolver(csharpContext);
} else {
resolver = new CSharpResolver(context.Compilation);
}
var astResolver = new CSharpAstResolver(resolver, documentationReference);
var rr = astResolver.Resolve(documentationReference);
MemberResolveResult mrr = rr as MemberResolveResult;
if (mrr != null)
return mrr.Member;
TypeResolveResult trr = rr as TypeResolveResult;
if (trr != null)
return trr.Type.GetDefinition();
return null;
}
示例5: Parse
CSharpParsedFile Parse(string program)
{
CompilationUnit cu = new CSharpParser().Parse(new StringReader(program), "test.cs");
CSharpParsedFile parsedFile = cu.ToTypeSystem();
project = project.UpdateProjectContent(null, parsedFile);
compilation = project.CreateCompilation();
return parsedFile;
}
示例6: GlobalReferenceExpressionTest
public void GlobalReferenceExpressionTest()
{
CSharpParser parser = new CSharpParser();
parser.ParseTypeReference(new StringReader("global::System"));
//Assert.IsTrue(tre.TypeReference.IsGlobal);
//Assert.AreEqual("System", tre.TypeReference.Type);
throw new NotImplementedException();
}
示例7: WrongUsingTest
public void WrongUsingTest()
{
string program = "using\n";
CSharpParser parser = new CSharpParser();
CompilationUnit cu = parser.Parse(new StringReader(program));
Assert.AreEqual(0, cu.Children.Count());
Assert.IsTrue(parser.HasErrors);
}
示例8: WrongUsingTest
public void WrongUsingTest()
{
string program = "using\n";
CSharpParser parser = new CSharpParser();
SyntaxTree syntaxTree = parser.Parse (program);
Assert.AreEqual(0, syntaxTree.Children.Count());
Assert.IsTrue(parser.HasErrors);
}
示例9: GetCodeRoot
public CodeRoot GetCodeRoot(string path)
{
if (File.Exists(path) == false)
return null;
CSharpParser formatter = new CSharpParser();
formatter.ParseCode(File.ReadAllText(path));
return (CodeRoot)formatter.CreatedCodeRoot;
}
示例10: SingleLineCommentsAreDiscovered
public void SingleLineCommentsAreDiscovered()
{
var expression = "var a = 1 + 3; // comment";
var result = new CSharpParser().Parse(expression);
var aChunk = result.TextChunks.First(ch => ch.TextValue == "// comment");
ExpressionHelper.Check(expression, result);
Assert.AreEqual(CodeType.Comment, aChunk.CodeType);
}
示例11: IfIsIdentifiedAsKeyword
public void IfIsIdentifiedAsKeyword()
{
var expression = "if (System.Console.ReadLine() == \"a\") { System.Console.WriteLine(\"Oh!\"); }";
var result = new CSharpParser().Parse(expression);
var ifChunk = result.TextChunks.First(ch => ch.TextValue == "if");
ExpressionHelper.Check(expression, result);
Assert.AreEqual(Common.CodeType.Keyword, ifChunk.CodeType);
}
示例12: VariableMentionType
public void VariableMentionType()
{
var expression = "var a = 1 + 3; var b = a;";
var result = new CSharpParser().Parse(expression);
var aMention = result.TextChunks.Where(ch => ch.TextValue == "a").Skip(1).First();
ExpressionHelper.Check(expression, result);
Assert.AreEqual(CodeType.Variable, aMention.CodeType);
Assert.AreEqual("struct System.Int32", aMention.TooltipValue);
}
示例13: InitializationTypeType
public void InitializationTypeType()
{
var expression = "var a = new System.Int32();";
var result = new CSharpParser().Parse(expression);
var intChunk = result.TextChunks.First(ch => ch.TextValue == "Int32");
ExpressionHelper.Check(expression, result);
Assert.AreEqual(CodeType.Type, intChunk.CodeType);
Assert.AreEqual("struct System.Int32", intChunk.TooltipValue);
}
示例14: DeclarationGenericTypeType
public void DeclarationGenericTypeType()
{
var expression = "System.Tuple<int, int> a = System.Tuple.Create(1, 1);";
var result = new CSharpParser().Parse(expression);
var intChunk = result.TextChunks.First(ch => ch.TextValue == "Tuple");
ExpressionHelper.Check(expression, result);
Assert.AreEqual(CodeType.Type, intChunk.CodeType);
Assert.AreEqual("class System.Tuple", intChunk.TooltipValue);
}
示例15: DeclarationPredefinedTypeType
public void DeclarationPredefinedTypeType()
{
var expression = "int a = 1 + 3;";
var result = new CSharpParser().Parse(expression);
var intChunk = result.TextChunks.First(ch => ch.TextValue == "int");
ExpressionHelper.Check(expression, result);
Assert.AreEqual(CodeType.Keyword, intChunk.CodeType);
Assert.AreEqual("struct System.Int32", intChunk.TooltipValue);
}