NSSharp ObjC Parser
Parse Objective-C headers into structured JSON or AST using the NSSharp .NET 10 CLI tool. No libclang or native dependencies required. Auto-detects vendor macros via UPPER_SNAKE_CASE heuristic and tracks NS_ASSUME_NONNULL scopes.
Quick Start
# Build (from repo root)
dotnet build NSSharp.slnx
# Or install as a dotnet tool
dotnet pack src/NSSharp/NSSharp.csproj -c Release
dotnet tool install -g --add-source src/NSSharp/bin/Release ASTools.NSSharp
# Or from NuGet.org
dotnet tool install -g ASTools.NSSharp
# Parse a header to C# bindings (default) — via dotnet run or installed tool
dotnet run --project src/NSSharp -- MyHeader.h
nssharp MyHeader.h
# Parse to JSON
nssharp MyHeader.h -f json
# Parse to file
nssharp MyHeader.h -f json -o output.json
# Parse xcframework
nssharp --xcframework MyLib.xcframework -f json
# List xcframework slices
nssharp --xcframework MyLib.xcframework --list-slices
# Parse specific slice
nssharp --xcframework MyLib.xcframework --slice ios-arm64 -f json
# Specify vendor export macros (treated as extern instead of skipped)
nssharp MyHeader.h --extern-macros PSPDF_EXPORT,FB_EXTERN
# Disable macro heuristic (all UPPER_SNAKE_CASE identifiers kept as-is)
nssharp MyHeader.h --no-macro-heuristic
CLI Options
| Option |
Description |
<files>... |
One or more .h files |
--xcframework <path> |
Parse all headers in xcframework |
--slice <name> |
Select xcframework slice |
--list-slices |
List available slices and exit |
-f, --format |
csharp (default) or json |
-o, --output |
Write to file |
--compact |
Compact JSON |
--extern-macros |
Comma-separated macros to treat as extern |
--emit-c-bindings |
Include C function DllImport declarations in C# output |
--no-macro-heuristic |
Disable UPPER_SNAKE_CASE auto-detection |
Supported ObjC Constructs
@interface (classes, categories, extensions, generics, generic superclasses, SWIFT_EXTENSION)
@protocol (@required / @optional, I-prefixed stubs, [Model] for delegates)
@property (attributes, nullability, custom getter/setter, weak)
- Instance (
-) and class (+) methods
NS_ENUM / NS_OPTIONS / NS_CLOSED_ENUM / NS_ERROR_ENUM / C enums with backing types
- Structs, typedefs, block types
- C function declarations (extern, static, bare) and extern constants
- Forward declarations (
@class, @protocol)
- Nullability annotations (
nullable, _Nullable, __nullable, etc.)
NS_ASSUME_NONNULL_BEGIN/END scope tracking
- Vendor macros auto-detected via UPPER_SNAKE_CASE heuristic
JSON Schema
See references/json-schema.md for the complete JSON output schema and AST node types.
Programmatic Usage in C#
using NSSharp.Lexer;
using NSSharp.Parser;
using NSSharp.Ast;
using NSSharp.Json;
var source = File.ReadAllText("MyHeader.h");
var options = new ObjCLexerOptions
{
MacroHeuristic = true,
ExternMacros = ["PSPDF_EXPORT"],
};
var lexer = new ObjCLexer(source, options);
var tokens = lexer.Tokenize();
var parser = new ObjCParser(tokens);
ObjCHeader header = parser.Parse("MyHeader.h");
// Access AST
foreach (var iface in header.Interfaces)
Console.WriteLine($"{iface.Name} : {iface.Superclass}");
// Serialize to JSON
string json = ObjCJsonSerializer.Serialize(header, pretty: true);
Project Layout
src/NSSharp/
├── Ast/ObjCNodes.cs # AST model types
├── Lexer/Token.cs # TokenKind enum
├── Lexer/ObjCLexer.cs # Tokenizer (UPPER_SNAKE_CASE macro heuristic)
├── Lexer/ObjCLexerOptions.cs # Lexer config (heuristic, extern macros)
├── Parser/ObjCParser.cs # Recursive-descent parser
├── Json/ObjCJsonSerializer.cs # JSON serializer
├── Binding/ # C# binding generator (see nssharp-binding-generator skill)
├── XCFrameworkResolver.cs # XCFramework header discovery
└── Program.cs # CLI entry point
Testing
dotnet test NSSharp.slnx
185 tests covering lexer, parser, JSON serializer, binding generator, macro heuristic scenarios, and tests from dotnet/macios sharpie PR #24622.
Known Limitations
- No full C preprocessor — vendor macros auto-detected via UPPER_SNAKE_CASE heuristic; use
--extern-macros for export macros
- Enum values with complex expressions preserved as strings, not evaluated
- No C++ support (classes, templates, namespaces)
- No semantic analysis or cross-header type resolution
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: nssharp-objc-parser3description: Parse Objective-C header files into structured AST using the NSSharp tool. Use when working with ObjC headers, generating JSON representations of ObjC APIs, inspecting xcframework contents, or analyzing ObjC type declarations (interfaces, protocols, enums, structs, functions, typedefs, categories, blocks, generics, nullability). Use when this capability is needed.4---56# NSSharp ObjC Parser78Parse Objective-C headers into structured JSON or AST using the NSSharp .NET 10 CLI tool. No libclang or native dependencies required. Auto-detects vendor macros via UPPER_SNAKE_CASE heuristic and tracks `NS_ASSUME_NONNULL` scopes.910## Quick Start1112```bash13# Build (from repo root)14dotnet build NSSharp.slnx1516# Or install as a dotnet tool17dotnet pack src/NSSharp/NSSharp.csproj -c Release18dotnet tool install -g --add-source src/NSSharp/bin/Release ASTools.NSSharp19# Or from NuGet.org20dotnet tool install -g ASTools.NSSharp2122# Parse a header to C# bindings (default) — via dotnet run or installed tool23dotnet run --project src/NSSharp -- MyHeader.h24nssharp MyHeader.h2526# Parse to JSON27nssharp MyHeader.h -f json2829# Parse to file30nssharp MyHeader.h -f json -o output.json3132# Parse xcframework33nssharp --xcframework MyLib.xcframework -f json3435# List xcframework slices36nssharp --xcframework MyLib.xcframework --list-slices3738# Parse specific slice39nssharp --xcframework MyLib.xcframework --slice ios-arm64 -f json4041# Specify vendor export macros (treated as extern instead of skipped)42nssharp MyHeader.h --extern-macros PSPDF_EXPORT,FB_EXTERN4344# Disable macro heuristic (all UPPER_SNAKE_CASE identifiers kept as-is)45nssharp MyHeader.h --no-macro-heuristic46```4748## CLI Options4950| Option | Description |51|---|---|52| `<files>...` | One or more .h files |53| `--xcframework <path>` | Parse all headers in xcframework |54| `--slice <name>` | Select xcframework slice |55| `--list-slices` | List available slices and exit |56| `-f, --format` | `csharp` (default) or `json` |57| `-o, --output` | Write to file |58| `--compact` | Compact JSON |59| `--extern-macros` | Comma-separated macros to treat as extern |60| `--emit-c-bindings` | Include C function DllImport declarations in C# output |61| `--no-macro-heuristic` | Disable UPPER_SNAKE_CASE auto-detection |6263## Supported ObjC Constructs6465- `@interface` (classes, categories, extensions, generics, generic superclasses, SWIFT_EXTENSION)66- `@protocol` (`@required` / `@optional`, I-prefixed stubs, `[Model]` for delegates)67- `@property` (attributes, nullability, custom getter/setter, weak)68- Instance (`-`) and class (`+`) methods69- `NS_ENUM` / `NS_OPTIONS` / `NS_CLOSED_ENUM` / `NS_ERROR_ENUM` / C enums with backing types70- Structs, typedefs, block types71- C function declarations (extern, static, bare) and extern constants72- Forward declarations (`@class`, `@protocol`)73- Nullability annotations (`nullable`, `_Nullable`, `__nullable`, etc.)74- `NS_ASSUME_NONNULL_BEGIN/END` scope tracking75- Vendor macros auto-detected via UPPER_SNAKE_CASE heuristic7677## JSON Schema7879See [references/json-schema.md](references/json-schema.md) for the complete JSON output schema and AST node types.8081## Programmatic Usage in C#8283```csharp84using NSSharp.Lexer;85using NSSharp.Parser;86using NSSharp.Ast;87using NSSharp.Json;8889var source = File.ReadAllText("MyHeader.h");90var options = new ObjCLexerOptions91{92 MacroHeuristic = true,93 ExternMacros = ["PSPDF_EXPORT"],94};95var lexer = new ObjCLexer(source, options);96var tokens = lexer.Tokenize();97var parser = new ObjCParser(tokens);98ObjCHeader header = parser.Parse("MyHeader.h");99100// Access AST101foreach (var iface in header.Interfaces)102 Console.WriteLine($"{iface.Name} : {iface.Superclass}");103104// Serialize to JSON105string json = ObjCJsonSerializer.Serialize(header, pretty: true);106```107108## Project Layout109110```111src/NSSharp/112├── Ast/ObjCNodes.cs # AST model types113├── Lexer/Token.cs # TokenKind enum114├── Lexer/ObjCLexer.cs # Tokenizer (UPPER_SNAKE_CASE macro heuristic)115├── Lexer/ObjCLexerOptions.cs # Lexer config (heuristic, extern macros)116├── Parser/ObjCParser.cs # Recursive-descent parser117├── Json/ObjCJsonSerializer.cs # JSON serializer118├── Binding/ # C# binding generator (see nssharp-binding-generator skill)119├── XCFrameworkResolver.cs # XCFramework header discovery120└── Program.cs # CLI entry point121```122123## Testing124125```bash126dotnet test NSSharp.slnx127```128129185 tests covering lexer, parser, JSON serializer, binding generator, macro heuristic scenarios, and tests from dotnet/macios sharpie PR #24622.130131## Known Limitations132133- No full C preprocessor — vendor macros auto-detected via UPPER_SNAKE_CASE heuristic; use `--extern-macros` for export macros134- Enum values with complex expressions preserved as strings, not evaluated135- No C++ support (classes, templates, namespaces)136- No semantic analysis or cross-header type resolution137138---139> Converted and distributed by [TomeVault](https://tomevault.io/claim/dalexsoto) — claim your Tome and manage your conversions.140<!-- tomevault:4.0:skill_md:2026-04-13 -->