Browse Source

Refactored the parser class.

pull/86/head
triton 12 years ago
parent
commit
0cc9446fe7
  1. 94
      src/Core/Parser/Parser.cs
  2. 71
      src/Generator/Driver.cs
  3. 109
      src/Generator/Parser.cs

94
src/Core/Parser/Parser.cs

@ -0,0 +1,94 @@
using System;
#if !OLD_PARSER
using CppSharp.Parser;
using CppSharp.Parser.AST;
#else
using CppSharp.AST;
#endif
namespace CppSharp
{
public class ClangParser
{
/// <summary>
/// Context with translation units ASTs.
/// </summary>
public ASTContext ASTContext { get; private set; }
/// <summary>
/// Fired when source files are parsed.
/// </summary>
public Action<SourceFile, ParserResult> SourceParsed = delegate {};
/// <summary>
/// Fire when library files are parsed.
/// </summary>
public Action<string, ParserResult> LibraryParsed = delegate {};
public ClangParser()
{
ASTContext = new ASTContext();
}
/// <summary>
/// Parses a C++ source file to a translation unit.
/// </summary>
public ParserResult ParseSourceFile(SourceFile file, ParserOptions options)
{
//if (options.ASTContext == null)
options.ASTContext = ASTContext;
options.FileName = file.Path;
var result = Parser.ClangParser.ParseHeader(options);
SourceParsed(file, result);
return result;
}
/// <summary>
/// Parses the project source files.
/// </summary>
public void ParseProject(Project project, ParserOptions options)
{
// TODO: Search for cached AST trees on disk
// TODO: Do multi-threaded parsing of source files
foreach (var source in project.Sources)
ParseSourceFile(source, source.Options);
}
/// <summary>
/// Parses a library file with symbols.
/// </summary>
public ParserResult ParseLibrary(string file, ParserOptions options)
{
options.FileName = file;
var result = Parser.ClangParser.ParseLibrary(options);
LibraryParsed(file, result);
return result;
}
#if !OLD_PARSER
/// <summary>
/// Converts a native parser AST to a managed AST.
/// </summary>
static public AST.ASTContext ConvertASTContext(ASTContext context)
{
var converter = new ASTConverter(context);
return converter.Convert();
}
public static AST.NativeLibrary ConvertLibrary(NativeLibrary library)
{
var newLibrary = new AST.NativeLibrary { FileName = library.FileName };
foreach (var symbol in library.Symbols)
newLibrary.Symbols.Add(symbol);
return newLibrary;
}
#endif
}
}

71
src/Generator/Driver.cs

@ -1,14 +1,18 @@
using System.Text; using CppSharp.AST;
using CppSharp.AST;
using CppSharp.Generators; using CppSharp.Generators;
using CppSharp.Generators.CLI; using CppSharp.Generators.CLI;
using CppSharp.Generators.CSharp; using CppSharp.Generators.CSharp;
using CppSharp.Parser;
using CppSharp.Passes; using CppSharp.Passes;
using CppSharp.Types; using CppSharp.Types;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
#if !OLD_PARSER
using Std;
#endif
namespace CppSharp namespace CppSharp
{ {
public class Driver public class Driver
@ -107,22 +111,73 @@ namespace CppSharp
} }
} }
ParserOptions BuildParseOptions(SourceFile file)
{
var options = new ParserOptions
{
FileName = file.Path,
#if !OLD_PARSER
IncludeDirs = Options.IncludeDirs.ToStd(),
SystemIncludeDirs = Options.SystemIncludeDirs.ToStd(),
Defines = Options.Defines.ToStd(),
LibraryDirs = Options.LibraryDirs.ToStd(),
#else
IncludeDirs = Options.IncludeDirs,
SystemIncludeDirs = Options.SystemIncludeDirs,
Defines = Options.Defines,
LibraryDirs = Options.LibraryDirs,
#endif
Abi = Options.Parser.Abi,
ToolSetToUse = Options.Parser.ToolSetToUse,
TargetTriple = Options.Parser.TargetTriple,
NoStandardIncludes = Options.Parser.NoStandardIncludes,
NoBuiltinIncludes = Options.Parser.NoBuiltinIncludes,
MicrosoftMode = Options.Parser.MicrosoftMode,
Verbose = Options.Parser.Verbose,
};
return options;
}
public bool ParseCode() public bool ParseCode()
{ {
if (!Parser.ParseHeaders(Options.Headers)) foreach (var header in Options.Headers)
return false; {
var source = Project.AddFile(header);
source.Options = BuildParseOptions(source);
}
Library = Parser.Library; var parser = new ClangParser();
parser.SourceParsed += OnSourceFileParsed;
parser.LibraryParsed += OnFileParsed;
parser.ParseProject(Project, Options.Parser);
#if !OLD_PARSER
ASTContext = ClangParser.ConvertASTContext(parser.ASTContext);
#else
ASTContext = parser.ASTContext;
#endif
return true; return true;
} }
public bool ParseLibraries() public bool ParseLibraries()
{ {
if (!Parser.ParseLibraries(Options.Libraries)) foreach (var library in Options.Libraries)
return false; {
var parser = new ClangParser();
var res = parser.ParseLibrary(library, Options.Parser);
LibrarySymbols = Parser.Library; if (res.Kind != ParserResultKind.Success)
continue;
#if !OLD_PARSER
Symbols.Libraries.Add(ClangParser.ConvertLibrary(res.Library));
#else
Symbols.Libraries.Add(res.Library);
#endif
}
return true; return true;
} }

109
src/Generator/Parser.cs

@ -1,109 +0,0 @@
using System;
using System.Collections.Generic;
using CppSharp.AST;
namespace CppSharp
{
public class Parser
{
public Library Library { get; private set; }
private readonly DriverOptions options;
public Parser(DriverOptions options)
{
this.options = options;
Library = new Library();
}
public bool ParseHeaders(IEnumerable<string> headers)
{
var hasErrors = false;
foreach (var header in headers)
{
var result = ParseHeader(header);
// If we have some error, report to end-user.
if (!options.IgnoreParseErrors)
{
foreach (var diag in result.Diagnostics)
{
if (diag.Level == ParserDiagnosticLevel.Error ||
diag.Level == ParserDiagnosticLevel.Fatal)
hasErrors = true;
}
}
}
return !hasErrors;
}
public ParserResult ParseHeader(string file)
{
var parserOptions = new ParserOptions
{
Library = Library,
FileName = file,
Verbose = options.Verbose,
IncludeDirs = options.IncludeDirs,
SystemIncludeDirs = options.SystemIncludeDirs,
Defines = options.Defines,
NoStandardIncludes = options.NoStandardIncludes,
NoBuiltinIncludes = options.NoBuiltinIncludes,
MicrosoftMode = options.MicrosoftMode,
ToolSetToUse = options.ToolsetToUse,
TargetTriple = options.TargetTriple,
Abi = options.Abi
};
var result = ClangParser.ParseHeader(parserOptions);
OnHeaderParsed(file, result);
return result;
}
public bool ParseLibraries(IEnumerable<string> libraries)
{
var hasErrors = false;
foreach (var lib in libraries)
{
var result = ParseLibrary(lib);
// If we have some error, report to end-user.
if (!options.IgnoreParseErrors)
{
foreach (var diag in result.Diagnostics)
{
if (diag.Level == ParserDiagnosticLevel.Error ||
diag.Level == ParserDiagnosticLevel.Fatal)
hasErrors = true;
}
}
}
return !hasErrors;
}
public ParserResult ParseLibrary(string file)
{
var parserOptions = new ParserOptions
{
Library = Library,
FileName = file,
Verbose = false,
LibraryDirs = options.LibraryDirs,
ToolSetToUse = options.ToolsetToUse
};
var result = ClangParser.ParseLibrary(parserOptions);
OnLibraryParsed(file, result);
return result;
}
public Action<string, ParserResult> OnHeaderParsed = delegate {};
public Action<string, ParserResult> OnLibraryParsed = delegate { };
}
}
Loading…
Cancel
Save