예제 #1
0
        public void ParseAssert(string text, Action <CppCompilation> assertCompilation, CppParserOptions options = null, bool asFile = true)
        {
            if (assertCompilation == null)
            {
                throw new ArgumentNullException(nameof(assertCompilation));
            }

            options = options ?? new CppParserOptions();
            var currentDirectory = Environment.CurrentDirectory;
            var headerFilename   = $"{TestContext.CurrentContext.Test.FullName}-{TestContext.CurrentContext.Test.ID}.h";
            var headerFile       = Path.Combine(currentDirectory, headerFilename);

            // Parse in memory
            var compilation = CppParser.Parse(text, options, headerFilename);

            foreach (var diagnosticsMessage in compilation.Diagnostics.Messages)
            {
                Console.WriteLine(diagnosticsMessage);
            }

            assertCompilation(compilation);

            if (asFile)
            {
                // Parse single file from disk
                File.WriteAllText(headerFile, text);
                compilation = CppParser.ParseFile(headerFile, options);
                assertCompilation(compilation);
            }
        }
예제 #2
0
        public static CSharpCompilation Convert(string text, CSharpConverterOptions options)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var converter = new CSharpConverter(options);

            return(converter.Run(parserOptions => CppParser.Parse(text, parserOptions)));
        }
예제 #3
0
파일: TestReadme.cs 프로젝트: xen2/CppAst
        public void TestSimple()
        {
            // Parse a C++ files
            var compilation = CppParser.Parse(@"
enum MyEnum { MyEnum_0, MyEnum_1 };
void function0(int a, int b);
struct MyStruct { int field0; int field1;};
typedef MyStruct* MyStructPtr;
"
                                              );

            // Print diagnostic messages
            foreach (var message in compilation.Diagnostics.Messages)
            {
                Console.WriteLine(message);
            }

            // Print All enums
            foreach (var cppEnum in compilation.Enums)
            {
                Console.WriteLine(cppEnum);
            }

            // Print All functions
            foreach (var cppFunction in compilation.Functions)
            {
                Console.WriteLine(cppFunction);
            }

            // Print All classes, structs
            foreach (var cppClass in compilation.Classes)
            {
                Console.WriteLine(cppClass);
            }

            // Print All typedefs
            foreach (var cppTypedef in compilation.Typedefs)
            {
                Console.WriteLine(cppTypedef);
            }
        }
예제 #4
0
        public void Process(string[] args)
        {
            var input_file = "test.cpp";

            var input_text = File.ReadAllText(input_file, encoding: Encoding.UTF8);

            var options = new CppParserOptions();
            //options.ParseAttributes = true;



            var compilation = CppParser.Parse(input_text, options);



            Context.Instance = new Context();

            foreach (var cpp_class in compilation.Classes)
            {
                HandleClass(cpp_class);
            }
        }
예제 #5
0
		protected CompilationUnit ParseStub (string continuation, bool appendSemicolon = true, string afterContinuation = null)
		{
			var mt = GetMemberTextToCaret ();
			if (mt == null)
				return null;
			
			string memberText = mt.Item1;
			bool wrapInClass = mt.Item2;
			
			var wrapper = new StringBuilder ();
			
			if (wrapInClass) {
/*				foreach (var child in Unit.Children) {
					if (child is UsingDeclaration) {
						var offset = document.GetOffset (child.StartLocation);
						wrapper.Append (document.GetText (offset, document.GetOffset (child.EndLocation) - offset));
					}
				}*/
				wrapper.Append ("class Stub {");
				wrapper.AppendLine ();
			}
			
			wrapper.Append (memberText);
			wrapper.Append (continuation);
			AppendMissingClosingBrackets (wrapper, memberText, appendSemicolon);
			wrapper.Append (afterContinuation);
			
			if (wrapInClass)
				wrapper.Append ('}');
			
			TextLocation memberLocation;
			if (currentMember != null && currentType != null && currentType.Kind != TypeKind.Enum) {
				memberLocation = currentMember.Region.Begin;
			} else if (currentType != null) {
				memberLocation = currentType.Region.Begin;
			} else {
				memberLocation = new TextLocation (1, 1);
			}
			                   
			using (var stream = new System.IO.StringReader (wrapper.ToString ())) {
				try {
					var parser = new CppParser ();
					return parser.Parse (stream, "stub.cs", wrapInClass ? memberLocation.Line - 2 : 0);
				} catch (Exception) {
					Console.WriteLine ("------");
					Console.WriteLine (wrapper);
					throw;
				}
			}
		}
        protected CompilationUnit ParseStub(string continuation, bool appendSemicolon = true, string afterContinuation = null)
        {
            var mt = GetMemberTextToCaret();

            if (mt == null)
            {
                return(null);
            }

            string memberText  = mt.Item1;
            bool   wrapInClass = mt.Item2;

            var wrapper = new StringBuilder();

            if (wrapInClass)
            {
/*				foreach (var child in Unit.Children) {
 *                                      if (child is UsingDeclaration) {
 *                                              var offset = document.GetOffset (child.StartLocation);
 *                                              wrapper.Append (document.GetText (offset, document.GetOffset (child.EndLocation) - offset));
 *                                      }
 *                              }*/
                wrapper.Append("class Stub {");
                wrapper.AppendLine();
            }

            wrapper.Append(memberText);
            wrapper.Append(continuation);
            AppendMissingClosingBrackets(wrapper, memberText, appendSemicolon);
            wrapper.Append(afterContinuation);

            if (wrapInClass)
            {
                wrapper.Append('}');
            }

            TextLocation memberLocation;

            if (currentMember != null && currentType != null && currentType.Kind != TypeKind.Enum)
            {
                memberLocation = currentMember.Region.Begin;
            }
            else if (currentType != null)
            {
                memberLocation = currentType.Region.Begin;
            }
            else
            {
                memberLocation = new TextLocation(1, 1);
            }

            using (var stream = new System.IO.StringReader(wrapper.ToString())) {
                try {
                    var parser = new CppParser();
                    return(parser.Parse(stream, "stub.cs", wrapInClass ? memberLocation.Line - 2 : 0));
                } catch (Exception) {
                    Console.WriteLine("------");
                    Console.WriteLine(wrapper);
                    throw;
                }
            }
        }
예제 #7
0
		Tuple<CppParsedFile, AstNode, CompilationUnit> GetNewExpressionAt (int offset)
		{
			var parser = new CppParser ();
			string text = this.document.GetText (0, this.offset); 
			var sb = new StringBuilder (text);
			sb.Append ("a ();");
			AppendMissingClosingBrackets (sb, text, false);
			
			var stream = new System.IO.StringReader (sb.ToString ());
			var completionUnit = parser.Parse (stream, CSharpParsedFile.FileName, 0);
			stream.Close ();
			var loc = document.GetLocation (offset);
			
			var expr = completionUnit.GetNodeAt (loc, n => n is Expression);
			if (expr == null) {
				// try without ";"
				sb = new StringBuilder (text);
				sb.Append ("a ()");
				AppendMissingClosingBrackets (sb, text, false);
				stream = new System.IO.StringReader (sb.ToString ());
				completionUnit = parser.Parse (stream, CSharpParsedFile.FileName, 0);
				stream.Close ();
				loc = document.GetLocation (offset);
				
				expr = completionUnit.GetNodeAt (loc, n => n is Expression);
				if (expr == null)
					return null;
			}
			var tsvisitor = new TypeSystemConvertVisitor (CSharpParsedFile.FileName);
			completionUnit.AcceptVisitor (tsvisitor, null);
			
			return Tuple.Create (tsvisitor.ParsedFile, expr, completionUnit);
		}