-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[examples] Added an example for the parser APIs.
Signed-off-by: Joao Matos <joao@tritao.eu>
- Loading branch information
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using CppSharp.Parser; | ||
using System; | ||
using System.IO; | ||
|
||
namespace CppSharp | ||
{ | ||
/// <summary> | ||
/// This sample shows how to use the parser APIs to parse a C/C++ source code | ||
/// file into a syntax tree representation. It takes as an argument a path to | ||
/// a source file and outputs the translation units parsed by the compiler. | ||
/// </summary> | ||
static class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
if (args.Length < 1) | ||
{ | ||
Console.Error.WriteLine("A path to a file for parsing is required."); | ||
return; | ||
} | ||
|
||
var file = Path.GetFullPath(args[0]); | ||
ParseSourceFile(file); | ||
} | ||
|
||
public static bool ParseSourceFile(string file) | ||
{ | ||
// Lets setup the options for parsing the file. | ||
var parserOptions = new ParserOptions | ||
{ | ||
LanguageVersion = LanguageVersion.CPP11, | ||
|
||
// Verbose here will make sure the parser outputs some extra debugging | ||
// information regarding include directories, which can be helpful when | ||
// tracking down parsing issues. | ||
Verbose = true | ||
}; | ||
|
||
// This will setup the necessary system include paths and arguments for parsing. | ||
// It will probe into the registry (on Windows) and filesystem to find the paths | ||
// of the system toolchains and necessary include directories. | ||
parserOptions.Setup(); | ||
|
||
// We create the Clang parser and parse the source code. | ||
var parser = new ClangParser(); | ||
var parserResult = parser.ParseSourceFile(file, parserOptions); | ||
|
||
// If there was some kind of error parsing, then lets print some diagnostics. | ||
if (parserResult.Kind != ParserResultKind.Success) | ||
{ | ||
if (parserResult.Kind == ParserResultKind.FileNotFound) | ||
Console.Error.WriteLine($"{file} was not found."); | ||
|
||
for (uint i = 0; i < parserResult.DiagnosticsCount; i++) | ||
{ | ||
var diag = parserResult.GetDiagnostics(i); | ||
|
||
Console.WriteLine("{0}({1},{2}): {3}: {4}", | ||
diag.FileName, diag.LineNumber, diag.ColumnNumber, | ||
diag.Level.ToString().ToLower(), diag.Message); | ||
} | ||
|
||
parserResult.Dispose(); | ||
return false; | ||
} | ||
|
||
// Now we can consume the output of the parser (syntax tree). | ||
|
||
// First we will convert the output, bindings for the native Clang AST, | ||
// to CppSharp's managed AST representation. | ||
var astContext = ClangParser.ConvertASTContext(parserResult.ASTContext); | ||
|
||
// After its converted, we can dispose of the native AST bindings. | ||
parserResult.Dispose(); | ||
|
||
// Now we can finally do what we please with the syntax tree. | ||
foreach (var sourceUnit in astContext.TranslationUnits) | ||
Console.WriteLine(sourceUnit.FileName); | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
include "../../build/Tests.lua" | ||
|
||
project "Parser" | ||
kind "ConsoleApp" | ||
language "C#" | ||
debugdir "." | ||
|
||
files { "**.cs", "./*.lua" } | ||
links | ||
{ | ||
"CppSharp", | ||
"CppSharp.AST", | ||
"CppSharp.Parser" | ||
} | ||
|
||
SetupManagedProject() | ||
SetupParser() |