aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMu Qiao <qiaomuf@gentoo.org>2011-07-28 19:50:44 +0800
committerMu Qiao <qiaomuf@gentoo.org>2011-08-02 15:52:19 +0800
commit9bfb2dbd4180e1171db2026fb58646045c1e7144 (patch)
tree11ec06d9a84e27cac0699b49e252583915680109
parentParser: split token compositions (diff)
downloadlibbash-9bfb2dbd4180e1171db2026fb58646045c1e7144.tar.gz
libbash-9bfb2dbd4180e1171db2026fb58646045c1e7144.tar.bz2
libbash-9bfb2dbd4180e1171db2026fb58646045c1e7144.zip
Walker: support expansions without colon
-rw-r--r--bashast/libbashWalker.g29
-rw-r--r--scripts/var_expansion.bash18
-rw-r--r--src/core/interpreter.h19
3 files changed, 53 insertions, 13 deletions
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index fc05b91..bfcb73b 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -475,13 +475,36 @@ var_expansion returns[std::string libbash_value]
bool greedy;
}
:^(USE_DEFAULT_WHEN_UNSET_OR_NULL var_name libbash_word=raw_string) {
- libbash_value = walker->do_default_expansion($var_name.libbash_value, libbash_word, $var_name.index);
+ libbash_value = walker->do_default_expansion(walker->is_unset_or_null($var_name.libbash_value, $var_name.index),
+ $var_name.libbash_value,
+ libbash_word,
+ $var_name.index);
+ }
+ |^(USE_DEFAULT_WHEN_UNSET var_name libbash_word=raw_string) {
+ libbash_value = walker->do_default_expansion(walker->is_unset($var_name.libbash_value),
+ $var_name.libbash_value,
+ libbash_word,
+ $var_name.index);
}
|^(ASSIGN_DEFAULT_WHEN_UNSET_OR_NULL var_name libbash_word=raw_string) {
- libbash_value = walker->do_assign_expansion($var_name.libbash_value, libbash_word, $var_name.index);
+ libbash_value = walker->do_assign_expansion(walker->is_unset_or_null($var_name.libbash_value, $var_name.index),
+ $var_name.libbash_value,
+ libbash_word,
+ $var_name.index);
+ }
+ |^(ASSIGN_DEFAULT_WHEN_UNSET var_name libbash_word=raw_string) {
+ libbash_value = walker->do_assign_expansion(walker->is_unset($var_name.libbash_value),
+ $var_name.libbash_value,
+ libbash_word,
+ $var_name.index);
}
|^(USE_ALTERNATE_WHEN_UNSET_OR_NULL var_name libbash_word=raw_string) {
- libbash_value = walker->do_alternate_expansion($var_name.libbash_value, libbash_word, $var_name.index);
+ libbash_value = walker->do_alternate_expansion(walker->is_unset_or_null($var_name.libbash_value, $var_name.index),
+ libbash_word);
+ }
+ |^(USE_ALTERNATE_WHEN_UNSET var_name libbash_word=raw_string) {
+ libbash_value = walker->do_alternate_expansion(walker->is_unset($var_name.libbash_value),
+ libbash_word);
}
|(^(OFFSET array_name arithmetics arithmetics)) => ^(OFFSET libbash_name=array_name offset=arithmetics length=arithmetics) {
libbash_value = walker->do_subarray_expansion(libbash_name, offset, length);
diff --git a/scripts/var_expansion.bash b/scripts/var_expansion.bash
index f1f5e52..2d9a28e 100644
--- a/scripts/var_expansion.bash
+++ b/scripts/var_expansion.bash
@@ -109,3 +109,21 @@ echo $#
echo a{b,c}d
echo a{a,bc}d{e,}f
echo a{ab,cd}d{ef,gh}
+foo=
+unset bar
+echo ${foo-abc}
+foo=
+unset bar
+echo ${foo+abc}
+foo=
+unset bar
+echo ${foo=abc}
+foo=
+unset bar
+echo ${bar-abc}
+foo=
+unset bar
+echo ${bar+abc}
+foo=
+unset bar
+echo ${bar=abc}
diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 1ee02dd..091d4b7 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -372,12 +372,12 @@ public:
/// \param value the value of the word
/// \param index the index of the paramter
/// \return the expansion result
- const std::string do_default_expansion(const std::string& name,
+ const std::string do_default_expansion(bool cond,
+ const std::string& name,
const std::string& value,
const unsigned index) const
{
- return (is_unset_or_null(name, index)?
- value : resolve<std::string>(name, index));
+ return (cond ? value : resolve<std::string>(name, index));
}
/// \brief perform ${parameter:=word} expansion
@@ -385,12 +385,12 @@ public:
/// \param value the value of the word
/// \param index the index of the paramter
/// \return the expansion result
- const std::string do_assign_expansion(const std::string& name,
+ const std::string do_assign_expansion(bool cond,
+ const std::string& name,
const std::string& value,
const unsigned index)
{
- return (is_unset_or_null(name, index)?
- set_value(name, value, index) : resolve<std::string>(name, index));
+ return (cond ? set_value(name, value, index) : resolve<std::string>(name, index));
}
/// \brief perform ${parameter:+word} expansion
@@ -398,11 +398,10 @@ public:
/// \param value the value of the word
/// \param index the index of the paramter
/// \return the expansion result
- const std::string do_alternate_expansion(const std::string& name,
- const std::string& value,
- const unsigned index) const
+ const std::string do_alternate_expansion(bool cond,
+ const std::string& value) const
{
- return (is_unset_or_null(name, index)? "" : value);
+ return (cond ? "" : value);
}
/// \brief perform substring expansion