tree-sitter-objc

raw JSON →
3.0.2 verified Sat May 09 auth: no python

Objective-C grammar for tree-sitter (v3.0.2). Parses Objective-C and Objective-C++ source code into a concrete syntax tree. Requires Python >=3.9.

pip install tree-sitter-objc
error ImportError: cannot import name 'Language' from 'tree_sitter_objc'
cause Incorrect import: trying to import a class 'Language' that does not exist.
fix
Use 'from tree_sitter_objc import language' or 'from tree_sitter import Language'.
error AttributeError: module 'tree_sitter_objc' has no attribute 'language'
cause Outdated version of tree-sitter-objc (before v3.0.0) or the grammar wasn't properly installed.
fix
Install the latest version: pip install --upgrade tree-sitter-objc.
error ValueError: Unknown language 'objc'
cause The language name passed to Language() doesn't match the registered grammar.
fix
Use 'objc' (not 'objective-c' or 'objc++') for the language name.
breaking v3.0.0+ switched from a direct 'Language' class to a 'language()' function. The grammar is loaded via tree-sitter's Language constructor using the module name.
fix Use 'from tree_sitter_objc import language' and pass it to Language() or use Language('tree_sitter_objc', 'objc').
gotcha The grammar module name is 'tree_sitter_objc' but the language name is 'objc'. Do not confuse these.
fix When using Language('tree_sitter_objc', 'objc'), the second argument is the language name, not the module name.
deprecated Python 3.8 and earlier are not supported. Requires Python >=3.9.
fix Upgrade Python to 3.9 or later.

Minimal example: load the grammar, parse ObjC code, and print the syntax tree.

from tree_sitter import Language, Parser

# Load the Objective-C grammar (v3.0.2)
ObjCLanguage = Language('tree_sitter_objc', 'objc')

parser = Parser()
parser.set_language(ObjCLanguage)

# Parse a simple Objective-C snippet
source = b"""
#import <Foundation/Foundation.h>

@interface MyClass : NSObject
- (void)sayHello;
@end
"""

tree = parser.parse(source)
print(tree.root_node.sexp())