diff options
author | 2012-07-21 02:52:35 +0100 | |
---|---|---|
committer | 2012-08-19 17:52:07 +0300 | |
commit | b2f509d72462bbf3dcd89d0164e435556980e3ba (patch) | |
tree | d1c06a11be6b391cada86c2251ab26d3cdfc4d5d /bashast | |
parent | Merge remote-tracking branch 'aparicio/multiline_string_declaration' (diff) | |
download | libbash-b2f509d72462bbf3dcd89d0164e435556980e3ba.tar.gz libbash-b2f509d72462bbf3dcd89d0164e435556980e3ba.tar.bz2 libbash-b2f509d72462bbf3dcd89d0164e435556980e3ba.zip |
Walker: Support string operations on arrays
When a variable expansion applies some string operation like replace on
the whole array, apply the operation to every element in the array.
Diffstat (limited to 'bashast')
-rw-r--r-- | bashast/libbashWalker.g | 83 |
1 files changed, 48 insertions, 35 deletions
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g index 460ed0c..768129f 100644 --- a/bashast/libbashWalker.g +++ b/bashast/libbashWalker.g @@ -188,6 +188,14 @@ options walker->set_input_stream(_in); } }; + + std::string replace_expansion(const std::string name, int index, std::function<void(std::string&)> replacer) + { + if(index > -1) + return walker->do_replace_expansion(name, replacer, index); + else + return walker->do_array_replace_expansion(name, replacer); + } } } @@ -582,48 +590,53 @@ var_expansion returns[std::string libbash_value] libbash_value = boost::lexical_cast<std::string>(walker->get_array_length(libbash_name)); } )) - |^(REPLACE_ALL var_name bash_pattern[replace_pattern, true] (libbash_word=raw_string)?) { - libbash_value = walker->do_replace_expansion($var_name.libbash_value, - std::bind(&interpreter::replace_all, - std::placeholders::_1, - replace_pattern, - libbash_word), - $var_name.index); - } - |^(REPLACE_AT_END var_name bash_pattern[replace_pattern, true] (libbash_word=raw_string)?) { + |^(REPLACE_ALL var_or_array_name bash_pattern[replace_pattern, true] (libbash_word=raw_string)?) { + libbash_value = replace_expansion($var_or_array_name.libbash_value, $var_or_array_name.index, + std::bind(&interpreter::replace_all, + std::placeholders::_1, + replace_pattern, + libbash_word)); + } + |^(REPLACE_AT_END var_or_array_name bash_pattern[replace_pattern, true] (libbash_word=raw_string)?) { replace_pattern = sregex(replace_pattern >> eos); - libbash_value = walker->do_replace_expansion($var_name.libbash_value, - std::bind(&interpreter::replace_all, - std::placeholders::_1, - replace_pattern, - libbash_word), - $var_name.index); + libbash_value = replace_expansion($var_or_array_name.libbash_value, $var_or_array_name.index, + std::bind(&interpreter::replace_all, + std::placeholders::_1, + replace_pattern, + libbash_word)); } - |^(LAZY_REMOVE_AT_END var_name bash_pattern[replace_pattern, false] (libbash_word=raw_string)?) { + |^(LAZY_REMOVE_AT_END var_or_array_name bash_pattern[replace_pattern, false] (libbash_word=raw_string)?) { replace_pattern = sregex(bos >> (s1=*_) >> replace_pattern >> eos); - libbash_value = walker->do_replace_expansion($var_name.libbash_value, - std::bind(&interpreter::lazy_remove_at_end, - std::placeholders::_1, - replace_pattern), - $var_name.index); + libbash_value = replace_expansion($var_or_array_name.libbash_value, $var_or_array_name.index, + std::bind(&interpreter::lazy_remove_at_end, + std::placeholders::_1, + replace_pattern)); } |^((REPLACE_AT_START { greedy = true; }|LAZY_REMOVE_AT_START { greedy = false; }) - var_name bash_pattern[replace_pattern, greedy] (libbash_word=raw_string)?) { + var_or_array_name bash_pattern[replace_pattern, greedy] (libbash_word=raw_string)?) { replace_pattern = sregex(bos >> replace_pattern); - libbash_value = walker->do_replace_expansion($var_name.libbash_value, - std::bind(&interpreter::replace_all, - std::placeholders::_1, - replace_pattern, - libbash_word), - $var_name.index); + libbash_value = replace_expansion($var_or_array_name.libbash_value, $var_or_array_name.index, + std::bind(&interpreter::replace_all, + std::placeholders::_1, + replace_pattern, + libbash_word)); + } + |^(REPLACE_FIRST var_or_array_name bash_pattern[replace_pattern, true] (libbash_word=raw_string)?) { + libbash_value = replace_expansion($var_or_array_name.libbash_value, $var_or_array_name.index, + std::bind(&interpreter::replace_first, + std::placeholders::_1, + replace_pattern, + libbash_word)); + }; + +var_or_array_name returns[std::string libbash_value, int index] + :libbash_string=array_name { + $libbash_value = libbash_string; + $index = -1; } - |^(REPLACE_FIRST var_name bash_pattern[replace_pattern, true] (libbash_word=raw_string)?) { - libbash_value = walker->do_replace_expansion($var_name.libbash_value, - std::bind(&interpreter::replace_first, - std::placeholders::_1, - replace_pattern, - libbash_word), - $var_name.index); + |var_name { + $libbash_value = $var_name.libbash_value; + $index = $var_name.index; }; word returns[std::string libbash_value] |