diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index b0d2f7dd..e42d4727 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -50,7 +50,6 @@ void TSDocumentSetUp(TSDocument *document, TSParseConfig config); void TSDocumentSetText(TSDocument *document, const char *text); TSTree * TSDocumentTree(const TSDocument *document); const char * TSDocumentToString(const TSDocument *document); - #ifdef __cplusplus } diff --git a/src/runtime/document.c b/src/runtime/document.cpp similarity index 93% rename from src/runtime/document.c rename to src/runtime/document.cpp index 19c2a6e0..484b27e8 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.cpp @@ -9,8 +9,7 @@ struct TSDocument { }; TSDocument * TSDocumentMake() { - TSDocument *result = malloc(sizeof(TSDocument)); - return result; + return new TSDocument(); } void TSDocumentSetUp(TSDocument *document, TSParseConfig config) { @@ -19,8 +18,8 @@ void TSDocumentSetUp(TSDocument *document, TSParseConfig config) { } void TSDocumentSetText(TSDocument *document, const char *text) { - document->text = text; TSParseResult result = document->parse_fn(text); + document->text = text; document->tree = result.tree; document->error = result.error; } diff --git a/src/runtime/tree.c b/src/runtime/tree.cpp similarity index 61% rename from src/runtime/tree.c rename to src/runtime/tree.cpp index 7ee2033e..0644a8cb 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.cpp @@ -1,12 +1,15 @@ #include "tree_sitter/runtime.h" -#include +#include #include +using std::string; + TSTree * TSTreeMake(TSSymbol value, size_t child_count, TSTree **children) { - TSTree *result = malloc(sizeof(TSTree)); + TSTree *result = new TSTree(); result->value = value; result->child_count = child_count; result->children = children; + result->ref_count = 0; for (int i = 0; i < child_count; i++) TSTreeRetain(children[i]); return result; @@ -36,26 +39,17 @@ int TSTreeEquals(const TSTree *node1, const TSTree *node2) { return 1; } -char * TSTreeWriteToString(const TSTree *tree, const char **symbol_names, char *string) { - if (!tree) { - sprintf(string, "#"); - } - char *result = string; - const char *name = symbol_names[tree->value]; - sprintf(result, "(%s", name); - result += strlen(name) + 1; - for (int i = 0; i < tree->child_count; i++) { - result[0] = ' '; - result++; - result = TSTreeWriteToString(tree->children[i], symbol_names, result); - } - result[0] = ')'; - result++; - return result; +static string __tree_to_string(const TSTree *tree, const char **symbol_names) { + if (!tree) return "#"; + string result = string("(") + symbol_names[tree->value]; + for (int i = 0; i < tree->child_count; i++) + result += " " + __tree_to_string(tree->children[i], symbol_names); + return result + ")"; } char * TSTreeToString(const TSTree *tree, const char **symbol_names) { - char *string = calloc(200, sizeof(char)); - TSTreeWriteToString(tree, symbol_names, string); - return string; + string value(__tree_to_string(tree, symbol_names)); + char *result = (char *)malloc(value.size()); + strcpy(result, value.c_str()); + return result; } diff --git a/tree_sitter.xcodeproj/project.pbxproj b/tree_sitter.xcodeproj/project.pbxproj index 2bef22c3..6c50a75e 100644 --- a/tree_sitter.xcodeproj/project.pbxproj +++ b/tree_sitter.xcodeproj/project.pbxproj @@ -30,7 +30,7 @@ 12E75A9C1891C17D001B8F10 /* json_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12E75A9B1891C17D001B8F10 /* json_spec.cpp */; }; 12E75AA218930931001B8F10 /* expand_repeats.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12E75AA018930931001B8F10 /* expand_repeats.cpp */; }; 12EDCF8A187B498C005A7A07 /* tree_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12EDCF89187B498C005A7A07 /* tree_spec.cpp */; }; - 12EDCF8D187C6282005A7A07 /* document.c in Sources */ = {isa = PBXBuildFile; fileRef = 12EDCF8C187C6282005A7A07 /* document.c */; }; + 12EDCF8D187C6282005A7A07 /* document.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12EDCF8C187C6282005A7A07 /* document.cpp */; }; 12EDCF981881FCD5005A7A07 /* extract_tokens.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12EDCF8F1881FCCA005A7A07 /* extract_tokens.cpp */; }; 12EDCF991881FCD9005A7A07 /* perform.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12EDCF911881FCCA005A7A07 /* perform.cpp */; }; 12EDCF9A1881FCD9005A7A07 /* search_for_symbols.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12EDCF931881FCCA005A7A07 /* search_for_symbols.cpp */; }; @@ -53,7 +53,7 @@ 12FD40D8185FEEDF0041A84E /* rules_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 121492EA181E200B008E9BDA /* rules_spec.cpp */; }; 12FD40D9185FEEDF0041A84E /* pattern_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12D136A0183570F5005F3369 /* pattern_spec.cpp */; }; 12FD40DB185FEF0D0041A84E /* arithmetic_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12FD40DA185FEF0D0041A84E /* arithmetic_spec.cpp */; }; - 12FD40DF1860064C0041A84E /* tree.c in Sources */ = {isa = PBXBuildFile; fileRef = 12FD40DE1860064C0041A84E /* tree.c */; }; + 12FD40DF1860064C0041A84E /* tree.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12FD40DE1860064C0041A84E /* tree.cpp */; }; 12FD40E718639B910041A84E /* visitor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12FD40E618639B910041A84E /* visitor.cpp */; }; 12FD40E918641FB70041A84E /* rules.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12FD40E818641FB70041A84E /* rules.cpp */; }; 12FD40F7186A16020041A84E /* lex_table.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12FD40F5186A16020041A84E /* lex_table.cpp */; }; @@ -131,7 +131,7 @@ 12E75AA018930931001B8F10 /* expand_repeats.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = expand_repeats.cpp; path = src/compiler/prepare_grammar/expand_repeats.cpp; sourceTree = SOURCE_ROOT; }; 12E75AA118930931001B8F10 /* expand_repeats.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; name = expand_repeats.h; path = src/compiler/prepare_grammar/expand_repeats.h; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; 12EDCF89187B498C005A7A07 /* tree_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = tree_spec.cpp; sourceTree = ""; }; - 12EDCF8C187C6282005A7A07 /* document.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = document.c; sourceTree = ""; }; + 12EDCF8C187C6282005A7A07 /* document.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = document.cpp; sourceTree = ""; }; 12EDCF8F1881FCCA005A7A07 /* extract_tokens.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; name = extract_tokens.cpp; path = src/compiler/prepare_grammar/extract_tokens.cpp; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.cpp; }; 12EDCF901881FCCA005A7A07 /* extract_tokens.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = extract_tokens.h; path = src/compiler/prepare_grammar/extract_tokens.h; sourceTree = SOURCE_ROOT; }; 12EDCF911881FCCA005A7A07 /* perform.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = perform.cpp; path = src/compiler/prepare_grammar/perform.cpp; sourceTree = SOURCE_ROOT; }; @@ -165,7 +165,7 @@ 12FD4065185E7C2F0041A84E /* arithmetic.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = arithmetic.c; sourceTree = ""; }; 12FD40D1185EEB5E0041A84E /* runtime_specs */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = runtime_specs; sourceTree = BUILT_PRODUCTS_DIR; }; 12FD40DA185FEF0D0041A84E /* arithmetic_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = arithmetic_spec.cpp; sourceTree = ""; }; - 12FD40DE1860064C0041A84E /* tree.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = tree.c; sourceTree = ""; }; + 12FD40DE1860064C0041A84E /* tree.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = tree.cpp; sourceTree = ""; }; 12FD40E41862B3530041A84E /* visitor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = visitor.h; sourceTree = ""; }; 12FD40E618639B910041A84E /* visitor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = visitor.cpp; sourceTree = ""; }; 12FD40E818641FB70041A84E /* rules.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; path = rules.cpp; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.cpp; }; @@ -397,9 +397,9 @@ 12FD40AD185EE5440041A84E /* runtime */ = { isa = PBXGroup; children = ( - 12EDCF8C187C6282005A7A07 /* document.c */, + 12EDCF8C187C6282005A7A07 /* document.cpp */, 12BC470318822A17005AC502 /* error.cpp */, - 12FD40DE1860064C0041A84E /* tree.c */, + 12FD40DE1860064C0041A84E /* tree.cpp */, ); path = runtime; sourceTree = ""; @@ -561,8 +561,8 @@ files = ( 12EDCF8A187B498C005A7A07 /* tree_spec.cpp in Sources */, 12E75A9A1891BF57001B8F10 /* json.c in Sources */, - 12EDCF8D187C6282005A7A07 /* document.c in Sources */, - 12FD40DF1860064C0041A84E /* tree.c in Sources */, + 12EDCF8D187C6282005A7A07 /* document.cpp in Sources */, + 12FD40DF1860064C0041A84E /* tree.cpp in Sources */, 12FD40D2185EEB970041A84E /* arithmetic.c in Sources */, 12E75A9C1891C17D001B8F10 /* json_spec.cpp in Sources */, 12FD40DB185FEF0D0041A84E /* arithmetic_spec.cpp in Sources */,