forked from tom95/tree-sitter-smalltalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmalltalk-parser.m
53 lines (41 loc) · 1.42 KB
/
smalltalk-parser.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Filename - test-json-parser.c
#import <Foundation/Foundation.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <tree_sitter/api.h>
// Declare the `tree_sitter_json` function, which is
// implemented by the `tree-sitter-json` library.
TSLanguage *tree_sitter_smalltalk();
int main(int argc, char **argv) {
// Create a parser.
TSParser *parser = ts_parser_new();
// Set the parser's language (Smalltalk in this case).
ts_parser_set_language(parser, tree_sitter_smalltalk());
NSLog(@"filename: '%s'",argv[1]);
NSString *filename = @(argv[1]);
NSLog(@"filename: '%@'",filename);
NSString *source = [NSString stringWithContentsOfFile:@(argv[1])];
NSLog(@"file: '%@' contents: '%@'",filename,source);
// Build a syntax tree based on source code stored in a string.
const char *source_code = [source UTF8String];
TSTree *tree = ts_parser_parse_string(
parser,
NULL,
source_code,
strlen(source_code)
);
// Get the root node of the syntax tree.
TSNode root_node = ts_tree_root_node(tree);
// Get some child nodes.
TSNode array_node = ts_node_named_child(root_node, 0);
TSNode number_node = ts_node_named_child(array_node, 0);
// Print the syntax tree as an S-expression.
char *string = ts_node_string(root_node);
printf("Syntax tree: %s\n", string);
// Free all of the heap-allocated memory.
free(string);
ts_tree_delete(tree);
ts_parser_delete(parser);
return 0;
}