Move some general code generation helpers into their own file
This commit is contained in:
parent
63718d5bad
commit
7251456cd2
4 changed files with 69 additions and 38 deletions
|
|
@ -2,7 +2,8 @@
|
|||
#include <vector>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#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<string> 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<string> 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<rules::Symbol> &expected_inputs) {
|
||||
string result = "PARSE_ERROR(" + to_string(expected_inputs.size()) + ", EXPECT({";
|
||||
bool started = false;
|
||||
|
|
|
|||
42
src/compiler/generate_code/helpers.cpp
Normal file
42
src/compiler/generate_code/helpers.cpp
Normal file
|
|
@ -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<string> 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<string> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
19
src/compiler/generate_code/helpers.h
Normal file
19
src/compiler/generate_code/helpers.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef __tree_sitter__helpers__
|
||||
#define __tree_sitter__helpers__
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace tree_sitter {
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
namespace generate_code {
|
||||
string indent(string input);
|
||||
string join(vector<string> lines, string separator);
|
||||
string join(vector<string> lines);
|
||||
string escape_string(string input);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -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 = "<group>"; };
|
||||
122587AE18BDD28B00A68B84 /* built_in_symbols.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = built_in_symbols.h; sourceTree = "<group>"; };
|
||||
122587B018BDD79600A68B84 /* follow_sets_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = follow_sets_spec.cpp; sourceTree = "<group>"; };
|
||||
122587B218BEE2C600A68B84 /* helpers.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = helpers.cpp; sourceTree = "<group>"; };
|
||||
122587B318BEE2C600A68B84 /* helpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = helpers.h; sourceTree = "<group>"; };
|
||||
1225CC6318765693000D4723 /* prepare_grammar_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = prepare_grammar_spec.cpp; sourceTree = "<group>"; };
|
||||
1236A7C318B287DC00593ABB /* character_range.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; path = character_range.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
|
||||
1236A7C918B2A79F00593ABB /* rule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rule.h; sourceTree = "<group>"; };
|
||||
|
|
@ -374,6 +377,8 @@
|
|||
children = (
|
||||
12FD405F185E68470041A84E /* c_code.cpp */,
|
||||
12FD4060185E68470041A84E /* c_code.h */,
|
||||
122587B218BEE2C600A68B84 /* helpers.cpp */,
|
||||
122587B318BEE2C600A68B84 /* helpers.h */,
|
||||
);
|
||||
path = generate_code;
|
||||
sourceTree = "<group>";
|
||||
|
|
@ -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 */,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue