80 return str_.substr(start, end - start + 1);
91 return std::string_view(
str_).substr(start, end - start + 1);
109 bool replace_try(
size_t from,
size_t to,
const std::string &replacement)
113 if (mut.src_range.overlaps(range)) {
117 replacement +
"\"\nbut it overlaps with substituting \n\"" +
118 str_.substr(mut.src_range.start, mut.src_range.size) +
119 "\"\nwith \n\"" + mut.replacement +
"\"";
131 const std::string &replacement,
132 bool keep_trailing_whitespaces =
false)
134 if (keep_trailing_whitespaces) {
135 return replace_try(from.str_index_start(), to.str_index_last_no_whitespace(), replacement);
137 return replace_try(from.str_index_start(), to.str_index_last(), replacement);
141 void replace(
size_t from,
size_t to,
const std::string &replacement)
156 const std::string &replacement,
157 bool keep_trailing_whitespaces =
false)
159 if (keep_trailing_whitespaces) {
160 replace(from.str_index_start(), to.str_index_last_no_whitespace(), replacement);
163 replace(from.str_index_start(), to.str_index_last(), replacement);
168 void replace(
Token tok,
const std::string &replacement,
bool keep_trailing_whitespaces =
false)
170 if (keep_trailing_whitespaces) {
171 replace(tok.str_index_start(), tok.str_index_last_no_whitespace(), replacement);
174 replace(tok.str_index_start(), tok.str_index_last(), replacement);
178 void replace(
Scope scope,
const std::string &replacement,
bool keep_trailing_whitespaces =
false)
180 if (keep_trailing_whitespaces) {
182 scope.
back().str_index_last_no_whitespace(),
191 const std::string &replacement,
192 bool keep_trailing_whitespaces =
false)
194 if (keep_trailing_whitespaces) {
196 Token(
node.back()).str_index_last_no_whitespace(),
210 size_t lines = std::count(content.begin(), content.end(),
'\n');
211 size_t spaces = content.find_last_of(
"\n");
212 if (spaces != std::string::npos) {
213 spaces = content.length() - (spaces + 1);
216 spaces = content.length();
218 return replace_try(from, to, std::string(lines,
'\n') + std::string(spaces,
' '));
234 return erase_try(from.str_index_start(), to.str_index_last());
284 void insert_before(
size_t at,
const std::string &content,
bool prepend =
false)
311 std::string
str =
"\n#line " + std::to_string(line);
328 size_t line_break =
str_.find_last_of(
"\n", at.str_index_last() + 1);
329 size_t spaces = at.str_index_last() - line_break;
349 out += std::to_string(mut.src_range.start);
351 out += std::to_string(mut.src_range.size);
353 out +=
str_.substr(mut.src_range.start, mut.src_range.size);
355 out += mut.replacement;
378template<
typename LexerFn,
typename ParserFn>
409 return Scope(*
this, 0);
430 const std::string &
result_get(
const bool all_mutation_ordered =
false)
454 std::cout <<
"Input: \n" <<
str_ <<
" \nEnd of Input\n" << std::endl;
455 std::cout <<
"Token Types: \"" << this->
token_types_str() <<
"\"" << std::endl;
456 std::cout <<
"Token scopes: \"" << this->
token_scope <<
"\"" << std::endl;
457 std::cout <<
"Scope Types: \"" << this->
scope_types_str <<
"\"" << std::endl;
462 for (
auto tok : *
this) {
463 std::cout <<
"id:" << int(tok) <<
" start:" << this->
offsets_[int(tok)]
464 <<
" end:" << this->
offsets_end_[int(tok)] <<
" type:" << tok.type()
467 <<
" atom:" << tok.atom() <<
" str:\"" << tok.str() <<
"\""
468 <<
" followed_by_whitespace:" << tok.followed_by_whitespace() <<
"\n";
constexpr IndexRange()=default
std::string filename(const std::string_view &str, size_t pos)
std::ostream & operator<<(std::ostream &out, const std::vector< int > &v)
ATTR_WARN_UNUSED_RESULT const BMVert * v
std::string_view token_types_str() const
Mutation(IndexRange src_range, std::string replacement)
friend bool operator<(const Mutation &a, const Mutation &b)
void insert_before(size_t at, const std::string &content, bool prepend=false)
bool erase_try(Token from, Token to)
const std::string & str()
std::string substr_range_inclusive(Token start, Token end)
std::vector< Mutation > mutations_
void erase(Token from, Token to)
bool erase_try(size_t from, size_t to)
std::string_view substr_range_inclusive_view(Token start, Token end)
void insert_directive(Token at, const std::string directive)
MutableString(MutableString &&other)=default
void insert_before(Token at, const std::string &content, bool prepend=false)
bool replace_try(Token from, Token to, const std::string &replacement, bool keep_trailing_whitespaces=false)
MutableString(const MutableString &other)=delete
void insert_line_number(size_t at, int line, std::string_view filename="")
void replace(size_t from, size_t to, const std::string &replacement)
void insert_after(size_t at, const std::string &content)
MutableString & operator=(const MutableString &other)=delete
void replace(Scope scope, const std::string &replacement, bool keep_trailing_whitespaces=false)
MutableString(const std::string_view input)
void erase(size_t from, size_t to)
void replace(Token tok, const std::string &replacement, bool keep_trailing_whitespaces=false)
void insert_line_number(Token at, int line, std::string_view filename="")
bool erase_try(Scope scope)
void replace(Token from, Token to, const std::string &replacement, bool keep_trailing_whitespaces=false)
bool apply_mutations(LexerBase &lexer, const bool all_mutation_ordered=false)
std::string serialize_mutations() const
bool erase_try(Token tok)
std::string_view substr_range_inclusive_view(size_t start, size_t end)
bool replace_try(size_t from, size_t to, const std::string &replacement)
MutableString & operator=(MutableString &&other)=default
void insert_after(Token at, const std::string &content)
void erase(ast::Node node)
void replace(ast::Node node, const std::string &replacement, bool keep_trailing_whitespaces=false)
std::string last_substitution_failure
bool erase_try(ast::Node node)
std::string substr_range_inclusive(size_t start, size_t end)
std::string_view scope_types_str
std::vector< ScopeType > scope_types
std::vector< int > token_scope
void parse_bsl(ErrorHandler &err_handler)
void semantic_analysis(ErrorHandler &err_handler)
void lexical_analysis(std::string_view input)
AlignedArrayPtr< uint32_t > offsets_
AlignedArrayPtr< uint32_t > offsets_end_
Token(const TokenBuffer *buf, int32_t index)