fix fct minify encountering reserved words

This commit is contained in:
zawz 2021-06-29 15:53:25 +02:00
parent 767302dd56
commit 3af1fc57fc
6 changed files with 24 additions and 25 deletions

View file

@ -6,6 +6,7 @@
#include <string>
#include <utility>
#include <vector>
#include <set>
#include <tuple>
#define SPACES " \t"
@ -37,9 +38,9 @@ struct list_parse_options {
};
// globals
extern const std::vector<std::string> posix_cmdvar;
extern const std::vector<std::string> bash_cmdvar;
extern const std::set<std::string> all_reserved_words;
extern const std::set<std::string> posix_cmdvar;
extern const std::set<std::string> bash_cmdvar;
std::string import_file(std::string const& path);

View file

@ -24,8 +24,6 @@ inline bool is_dev_file(std::string const& filename) { return filename.substr(0,
std::string indent(int n);
bool is_among(std::string const& in, std::vector<std::string> const& values);
std::vector<std::string> split(std::string const& in, const char* splitters);
std::vector<std::string> split(std::string const& in, char c);
@ -137,6 +135,12 @@ bool is_in_vector(T el, std::vector<T> vec)
return false;
}
template <class T>
bool is_in_set(T el, std::set<T> ss)
{
return ss.find(el) != ss.end();
}
std::set<std::string> prune_matching(std::set<std::string>& in, std::regex re);
std::string delete_brackets(std::string const& in);

View file

@ -310,6 +310,7 @@ void minify_fct(_obj* in, std::regex const& exclude)
concat_sets(allcmds, m_excluded_fct);
concat_sets(allcmds, unsets);
// create mapping
concat_sets(allcmds, all_reserved_words);
fctmap=gen_minimal_map(m_fcts, allcmds);
// perform replace
recurse(r_replace_fct, in, &fctmap);

View file

@ -14,14 +14,14 @@
// macro
// constants
const std::vector<std::string> posix_cmdvar = { "export", "unset", "local", "read", "getopts" };
const std::vector<std::string> bash_cmdvar = { "readonly", "declare", "typeset" };
const std::set<std::string> posix_cmdvar = { "export", "unset", "local", "read", "getopts" };
const std::set<std::string> bash_cmdvar = { "readonly", "declare", "typeset" };
const std::vector<std::string> arithmetic_precedence_operators = { "!", "~", "+", "-" };
const std::vector<std::string> arithmetic_operators = { "+", "-", "*", "/", "+=", "-=", "*=", "/=", "=", "==", "!=", "&", "|", "^", "<<", ">>", "&&", "||" };
const std::set<std::string> arithmetic_precedence_operators = { "!", "~", "+", "-" };
const std::set<std::string> arithmetic_operators = { "+", "-", "*", "/", "+=", "-=", "*=", "/=", "=", "==", "!=", "&", "|", "^", "<<", ">>", "&&", "||" };
const std::vector<std::string> all_reserved_words = { "if", "then", "else", "fi", "case", "esac", "for", "while", "do", "done", "{", "}" };
const std::vector<std::string> out_reserved_words = { "then", "else", "fi", "esac", "do", "done", "}" };
const std::set<std::string> all_reserved_words = { "if", "then", "else", "fi", "case", "esac", "for", "while", "do", "done", "{", "}" };
const std::set<std::string> out_reserved_words = { "then", "else", "fi", "esac", "do", "done", "}" };
// stuff
@ -276,7 +276,7 @@ std::pair<arithmetic*, parse_context> parse_arithmetic(parse_context ctx)
}
auto po = get_operator(ctx);
if(is_among(po.first, arithmetic_precedence_operators))
if(is_in_set(po.first, arithmetic_precedence_operators))
{
ctx.i = po.second;
auto pa = parse_arithmetic(ctx);
@ -335,7 +335,7 @@ std::pair<arithmetic*, parse_context> parse_arithmetic(parse_context ctx)
auto po = get_operator(ctx);
if(po.first != "")
{
if(!is_among(po.first, arithmetic_operators))
if(!is_in_set(po.first, arithmetic_operators))
{
parse_error( "Unknown arithmetic operator: "+po.first, ctx);
}
@ -1289,9 +1289,10 @@ std::pair<cmd*, parse_context> parse_cmd(parse_context ctx)
ctx = parse_cmd_varassigns(ret, ctx);
auto wp=get_word(ctx, ARG_END);
if(is_in_vector(wp.first, posix_cmdvar) || is_in_vector(wp.first, bash_cmdvar))
bool is_bash_cmdvar=false;
if(is_in_set(wp.first, posix_cmdvar) || (is_bash_cmdvar=is_in_set(wp.first, bash_cmdvar)) )
{
if(!ctx.bash && is_in_vector(wp.first, bash_cmdvar))
if(!ctx.bash && (is_bash_cmdvar || is_in_set(wp.first, bash_cmdvar)))
{
parse_error("bash specific: "+wp.first, ctx);
}
@ -1627,7 +1628,7 @@ std::pair<block*, parse_context> parse_block(parse_context ctx)
ret = pp.first;
ctx = pp.second;
}
else if(is_in_vector(word, out_reserved_words)) // is a reserved word
else if(is_in_set(word, out_reserved_words)) // is a reserved word
{
parse_error( strf("Unexpected '%s'", word.c_str())+expecting(ctx.expecting) , ctx);
ctx.i+=word.size();

View file

@ -160,7 +160,7 @@ std::string get_varname(arg* in)
bool cmd_is_argvar(std::string const& in)
{
return is_in_vector(in, posix_cmdvar) || is_in_vector(in, bash_cmdvar);
return is_in_set(in, posix_cmdvar) || is_in_set(in, bash_cmdvar);
}
bool cmd::is_argvar()

View file

@ -45,14 +45,6 @@ std::string dirname(std::string const& in)
return ".";
}
bool is_among(std::string const& in, std::vector<std::string> const& values)
{
for(auto it: values)
if(in == it)
return true;
return false;
}
std::vector<std::string> split(std::string const& in, const char* splitters)
{
uint32_t i=0,j=0;