本文整理汇总了C#中CSharpParser.Parse方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpParser.Parse方法的具体用法?C# CSharpParser.Parse怎么用?C# CSharpParser.Parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CSharpParser
的用法示例。
在下文中一共展示了CSharpParser.Parse方法的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: 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);
}
示例5: ParseTestCase
internal static IProjectContent ParseTestCase()
{
const string fileName = "TypeSystemTests.TestCase.cs";
CSharpParser parser = new CSharpParser();
CompilationUnit cu;
using (Stream s = typeof(TypeSystemTests).Assembly.GetManifestResourceStream(typeof(TypeSystemTests), fileName)) {
cu = parser.Parse(s);
}
var testCasePC = new SimpleProjectContent();
CSharpParsedFile parsedFile = new TypeSystemConvertVisitor(testCasePC, fileName).Convert(cu);
parsedFile.Freeze();
testCasePC.UpdateProjectContent(null, parsedFile);
return testCasePC;
}
示例6: 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);
}
示例7: ParseAndCheckPositions
public void ParseAndCheckPositions()
{
CSharpParser parser = new CSharpParser();
foreach (string fileName in fileNames) {
var currentDocument = new ReadOnlyDocument(File.ReadAllText(fileName));
SyntaxTree syntaxTree = parser.Parse(currentDocument, fileName);
if (parser.HasErrors)
continue;
ConsistencyChecker.CheckPositionConsistency(syntaxTree, fileName, currentDocument);
ConsistencyChecker.CheckMissingTokens(syntaxTree, fileName, currentDocument);
}
}
示例8: ParseTestCase
internal static IProjectContent ParseTestCase()
{
const string fileName = "TypeSystemTests.TestCase.cs";
CSharpParser parser = new CSharpParser();
SyntaxTree syntaxTree;
using (Stream s = typeof(TypeSystemTests).Assembly.GetManifestResourceStream(typeof(TypeSystemTests), fileName)) {
syntaxTree = parser.Parse(s, fileName);
}
return CreateContent(syntaxTree.ToTypeSystem());
}
示例9: AssemblyAndModuleAttributesDoNotAppearOnTypes
public void AssemblyAndModuleAttributesDoNotAppearOnTypes()
{
var parser = new CSharpParser();
var cu = parser.Parse("[assembly: My1][module: My2][My3]class C {} public class My1Attribute : System.Attribute {} public class My2Attribute : System.Attribute {} public class My3Attribute : System.Attribute {}", "File.cs");
var ts = cu.ToTypeSystem();
var compilation = new CSharpProjectContent()
.UpdateProjectContent(null, ts)
.AddAssemblyReferences(new[] { CecilLoaderTests.Mscorlib })
.CreateCompilation();
var type = ReflectionHelper.ParseReflectionName("C").Resolve(compilation).GetDefinition();
Assert.That(type.Attributes.Select(a => a.AttributeType.FullName).ToList(), Is.EqualTo(new[] { "My3Attribute" }));
}
示例10: ParseAndCheckPositions
public void ParseAndCheckPositions()
{
CSharpParser parser = new CSharpParser();
foreach (string fileName in fileNames) {
this.currentDocument = new ReadOnlyDocument(File.ReadAllText(fileName));
CompilationUnit cu = parser.Parse(currentDocument.CreateReader(), fileName);
if (parser.HasErrors)
continue;
this.currentFileName = fileName;
CheckPositionConsistency(cu);
CheckMissingTokens(cu);
}
}
示例11: GenerateTypeSystem
public void GenerateTypeSystem()
{
SimpleProjectContent pc = new SimpleProjectContent();
CSharpParser parser = new CSharpParser();
parser.GenerateTypeSystemMode = true;
foreach (string fileName in fileNames) {
CompilationUnit cu;
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan)) {
cu = parser.Parse(fs);
}
TypeSystemConvertVisitor cv = new TypeSystemConvertVisitor(pc, fileName);
pc.UpdateProjectContent(null, cv.Convert(cu));
}
}
示例12: ParseTestCase
internal static IProjectContent ParseTestCase()
{
const string fileName = "TypeSystemTests.TestCase.cs";
CSharpParser parser = new CSharpParser();
SyntaxTree syntaxTree;
using (Stream s = typeof(TypeSystemTests).Assembly.GetManifestResourceStream(typeof(TypeSystemTests), fileName)) {
syntaxTree = parser.Parse(s, fileName);
}
var unresolvedFile = syntaxTree.ToTypeSystem();
return new CSharpProjectContent()
.AddOrUpdateFiles(unresolvedFile)
.AddAssemblyReferences(new[] { CecilLoaderTests.Mscorlib })
.SetAssemblyName(typeof(TypeSystemTests).Assembly.GetName().Name);
}
示例13: FixtureSetUp
public void FixtureSetUp()
{
const string fileName = "TypeSystemTests.TestCase.cs";
CSharpParser parser = new CSharpParser();
CompilationUnit cu;
using (Stream s = typeof(TypeSystemTests).Assembly.GetManifestResourceStream(typeof(TypeSystemTests), fileName)) {
cu = parser.Parse(s);
}
testCasePC = new SimpleProjectContent();
TypeSystemConvertVisitor visitor = new TypeSystemConvertVisitor(testCasePC, fileName);
cu.AcceptVisitor(visitor, null);
ParsedFile parsedFile = visitor.ParsedFile;
((SimpleProjectContent)testCasePC).UpdateProjectContent(null, parsedFile.TopLevelTypeDefinitions, null, null);
}
示例14: ConditionalSymbolTest
public void ConditionalSymbolTest()
{
const string program = @"// Test
#if AAA
#undef AAA
#define CCC
#else
#define DDD
#endif
class C {}";
CSharpParser parser = new CSharpParser();
parser.CompilerSettings.ConditionalSymbols.Add("AAA");
parser.CompilerSettings.ConditionalSymbols.Add("BBB");
var syntaxTree = parser.Parse(program, "elif.cs");
Assert.AreEqual(new[] { "BBB", "CCC" }, syntaxTree.ConditionalSymbols);
}
示例15: ParseTestCase
internal static IProjectContent ParseTestCase()
{
const string fileName = "TypeSystemTests.TestCase.cs";
CSharpParser parser = new CSharpParser();
CompilationUnit cu;
using (Stream s = typeof(TypeSystemTests).Assembly.GetManifestResourceStream(typeof(TypeSystemTests), fileName)) {
cu = parser.Parse(s, fileName);
}
var parsedFile = cu.ToTypeSystem();
return new CSharpProjectContent()
.UpdateProjectContent(null, parsedFile)
.AddAssemblyReferences(new[] { CecilLoaderTests.Mscorlib })
.SetAssemblyName(typeof(TypeSystemTests).Assembly.GetName().Name);
}