Move some general code generation helpers into their own file

This commit is contained in:
Max Brunsfeld 2014-02-26 19:08:07 -08:00
parent 63718d5bad
commit 7251456cd2
4 changed files with 69 additions and 38 deletions

View file

@ -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;