diff --git a/src/compiler/generate_code/c_code.cpp b/src/compiler/generate_code/c_code.cpp index c36c148c..5cfde752 100644 --- a/src/compiler/generate_code/c_code.cpp +++ b/src/compiler/generate_code/c_code.cpp @@ -2,7 +2,8 @@ #include #include #include -#include "built_in_symbols.h" +#include "rules/built_in_symbols.h" +#include "./helpers.h" namespace tree_sitter { using std::string; @@ -13,38 +14,6 @@ namespace tree_sitter { using std::pair; namespace generate_code { - static void str_replace(string &input, const string &search, const string &replace) { - size_t pos = 0; - while (1) { - pos = input.find(search, pos); - if (pos == string::npos) break; - input.erase(pos, search.length()); - input.insert(pos, replace); - pos += replace.length(); - } - } - - string join(vector lines, string separator) { - string result; - bool started = false; - for (auto line : lines) { - if (started) result += separator; - started = true; - result += line; - } - return result; - } - - string join(vector lines) { - return join(lines, "\n"); - } - - string indent(string input) { - string tab = " "; - str_replace(input, "\n", "\n" + tab); - return tab + input; - } - string _switch(string condition, string body) { return join({ "switch (" + condition + ") {", @@ -165,11 +134,6 @@ namespace tree_sitter { } } - string escape_string(string input) { - str_replace(input, "\"", "\\\""); - return input; - } - string parse_error_call(const set &expected_inputs) { string result = "PARSE_ERROR(" + to_string(expected_inputs.size()) + ", EXPECT({"; bool started = false; diff --git a/src/compiler/generate_code/helpers.cpp b/src/compiler/generate_code/helpers.cpp new file mode 100644 index 00000000..805d5d00 --- /dev/null +++ b/src/compiler/generate_code/helpers.cpp @@ -0,0 +1,42 @@ +#include "./helpers.h" + +namespace tree_sitter { + namespace generate_code { + static void str_replace(string &input, const string &search, const string &replace) { + size_t pos = 0; + while (1) { + pos = input.find(search, pos); + if (pos == string::npos) break; + input.erase(pos, search.length()); + input.insert(pos, replace); + pos += replace.length(); + } + } + + string join(vector lines, string separator) { + string result; + bool started = false; + for (auto line : lines) { + if (started) result += separator; + started = true; + result += line; + } + return result; + } + + string join(vector lines) { + return join(lines, "\n"); + } + + string indent(string input) { + string tab = " "; + str_replace(input, "\n", "\n" + tab); + return tab + input; + } + + string escape_string(string input) { + str_replace(input, "\"", "\\\""); + return input; + } + } +} \ No newline at end of file diff --git a/src/compiler/generate_code/helpers.h b/src/compiler/generate_code/helpers.h new file mode 100644 index 00000000..9bee4802 --- /dev/null +++ b/src/compiler/generate_code/helpers.h @@ -0,0 +1,19 @@ +#ifndef __tree_sitter__helpers__ +#define __tree_sitter__helpers__ + +#include +#include + +namespace tree_sitter { + using std::string; + using std::vector; + + namespace generate_code { + string indent(string input); + string join(vector lines, string separator); + string join(vector lines); + string escape_string(string input); + } +} + +#endif diff --git a/tree_sitter.xcodeproj/project.pbxproj b/tree_sitter.xcodeproj/project.pbxproj index 15eaaec7..6308056a 100644 --- a/tree_sitter.xcodeproj/project.pbxproj +++ b/tree_sitter.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 1214930E181E200B008E9BDA /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 121492E9181E200B008E9BDA /* main.cpp */; }; 122587AF18BDD28B00A68B84 /* built_in_symbols.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 122587AD18BDD28B00A68B84 /* built_in_symbols.cpp */; }; 122587B118BDD79600A68B84 /* follow_sets_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 122587B018BDD79600A68B84 /* follow_sets_spec.cpp */; }; + 122587B418BEE2C600A68B84 /* helpers.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 122587B218BEE2C600A68B84 /* helpers.cpp */; }; 1225CC6418765693000D4723 /* prepare_grammar_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1225CC6318765693000D4723 /* prepare_grammar_spec.cpp */; }; 1236A7C518B287DC00593ABB /* character_range.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1236A7C318B287DC00593ABB /* character_range.cpp */; }; 1236A7D218B554C800593ABB /* prepared_grammar.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1236A7D018B554C800593ABB /* prepared_grammar.cpp */; }; @@ -100,6 +101,8 @@ 122587AD18BDD28B00A68B84 /* built_in_symbols.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = built_in_symbols.cpp; sourceTree = ""; }; 122587AE18BDD28B00A68B84 /* built_in_symbols.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = built_in_symbols.h; sourceTree = ""; }; 122587B018BDD79600A68B84 /* follow_sets_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = follow_sets_spec.cpp; sourceTree = ""; }; + 122587B218BEE2C600A68B84 /* helpers.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = helpers.cpp; sourceTree = ""; }; + 122587B318BEE2C600A68B84 /* helpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = helpers.h; sourceTree = ""; }; 1225CC6318765693000D4723 /* prepare_grammar_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = prepare_grammar_spec.cpp; sourceTree = ""; }; 1236A7C318B287DC00593ABB /* character_range.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; path = character_range.cpp; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.cpp; }; 1236A7C918B2A79F00593ABB /* rule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rule.h; sourceTree = ""; }; @@ -374,6 +377,8 @@ children = ( 12FD405F185E68470041A84E /* c_code.cpp */, 12FD4060185E68470041A84E /* c_code.h */, + 122587B218BEE2C600A68B84 /* helpers.cpp */, + 122587B318BEE2C600A68B84 /* helpers.h */, ); path = generate_code; sourceTree = ""; @@ -535,6 +540,7 @@ 12FD4061185E68470041A84E /* c_code.cpp in Sources */, 12FD40D8185FEEDF0041A84E /* rules_spec.cpp in Sources */, 12EDCFC318820A70005A7A07 /* item_set_transitions.cpp in Sources */, + 122587B418BEE2C600A68B84 /* helpers.cpp in Sources */, 12FD4064185E75290041A84E /* compile_examples.cpp in Sources */, 12EDCFAF18820387005A7A07 /* parse_table.cpp in Sources */, 122587AF18BDD28B00A68B84 /* built_in_symbols.cpp in Sources */,