3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-10-10 01:41:59 +00:00
This commit is contained in:
KrystalDelusion 2025-10-08 13:38:04 -05:00 committed by GitHub
commit b64fe98898
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
65 changed files with 409 additions and 152 deletions

View file

@ -2,6 +2,7 @@
#include "kernel/log.h"
#include <iostream>
#include <string>
#include <iomanip>
#if !defined(WIN32)
#include <dirent.h>
@ -37,15 +38,16 @@ std::string next_token(std::string &text, const char *sep, bool long_strings)
if (pos_begin == std::string::npos)
pos_begin = text.size();
if (long_strings && pos_begin != text.size() && text[pos_begin] == '"') {
if (long_strings && pos_begin != text.size() && (text[pos_begin] == '"' || text[pos_begin] == '\'')) {
std::string sep_string = sep;
for (size_t i = pos_begin+1; i < text.size(); i++) {
if (text[i] == '"' && (i+1 == text.size() || sep_string.find(text[i+1]) != std::string::npos)) {
bool close_quote = (text[i] == text[pos_begin]) && (text[i-1] != '\\' || text[pos_begin] == '\'');
if (close_quote && (i+1 == text.size() || sep_string.find(text[i+1]) != std::string::npos)) {
std::string token = text.substr(pos_begin, i-pos_begin+1);
text = text.substr(i+1);
return token;
}
if (i+1 < text.size() && text[i] == '"' && text[i+1] == ';' && (i+2 == text.size() || sep_string.find(text[i+2]) != std::string::npos)) {
if (i+1 < text.size() && close_quote && text[i+1] == ';' && (i+2 == text.size() || sep_string.find(text[i+2]) != std::string::npos)) {
std::string token = text.substr(pos_begin, i-pos_begin+1);
text = text.substr(i+2);
return token + ";";
@ -592,4 +594,34 @@ void format_emit_void_ptr(std::string &result, std::string_view spec, int *dynam
format_emit_stringf(result, spec, dynamic_ints, num_dynamic_ints, arg);
}
bool needs_quote(const std::string &s) {
for (auto c : {' ', '\\', '#', ';', '"', '\''}) {
if (s.find(c) != std::string::npos) return true;
}
return false;
}
std::string quote(const std::string &s) {
std::ostringstream ss;
if (s.find('\'') != std::string::npos)
ss << std::quoted(s);
else
ss << '\'' << s << '\'';
return ss.str();
}
std::string unquote(const std::string &s) {
if (s.length() >= 2) {
if (s.front() == '\'' && s.back() == '\'')
return s.substr(1, s.length()-2);
else if (s.front() == '"' && s.back() == '"') {
std::string result;
std::istringstream ss(s);
ss >> std::quoted(result);
return result;
}
}
return s;
}
YOSYS_NAMESPACE_END

View file

@ -470,6 +470,10 @@ void remove_directory(std::string dirname);
bool create_directory(const std::string& dirname);
std::string escape_filename_spaces(const std::string& filename);
bool needs_quote(const std::string &s);
std::string quote(const std::string &s);
std::string unquote(const std::string &s);
YOSYS_NAMESPACE_END
#endif // YOSYS_IO_H

View file

@ -222,7 +222,7 @@ void Pass::call(RTLIL::Design *design, std::string command)
while (!tok.empty() && tok.back() == ';')
tok.resize(tok.size()-1), num_semikolon++;
if (!tok.empty())
args.push_back(tok);
args.push_back(unquote(tok));
call(design, args);
args.clear();
if (num_semikolon == 2)
@ -230,7 +230,7 @@ void Pass::call(RTLIL::Design *design, std::string command)
if (num_semikolon == 3)
call(design, "clean -purge");
} else
args.push_back(tok);
args.push_back(unquote(tok));
bool found_nl = false;
for (auto c : cmd_buf) {
if (c == ' ' || c == '\t')
@ -256,8 +256,10 @@ void Pass::call(RTLIL::Design *design, std::vector<std::string> args)
if (echo_mode) {
log("%s", create_prompt(design, 0));
for (size_t i = 0; i < args.size(); i++)
log("%s%s", i ? " " : "", args[i]);
for (size_t i = 0; i < args.size(); i++) {
auto maybe_quoted = needs_quote(args[i]) ? quote(args[i]) : args[i];
log("%s%s", i ? " " : "", maybe_quoted);
}
log("\n");
}