From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1QAEWc-0001cw-4l for garchives@archives.gentoo.org; Thu, 14 Apr 2011 04:51:26 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id EAF461C02C; Thu, 14 Apr 2011 04:50:16 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id A57AD1C028 for ; Thu, 14 Apr 2011 04:50:16 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 473BF1B415F for ; Thu, 14 Apr 2011 04:50:16 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 6682B8007B for ; Thu, 14 Apr 2011 04:50:15 +0000 (UTC) From: "Petteri Räty" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Petteri Räty" Message-ID: <604169c50644cafde6af6b3cbed4aea6db4df721.betelgeuse@gentoo> Subject: [gentoo-commits] proj/libbash:master commit in: scripts/, src/core/, bashast/, test/ X-VCS-Repository: proj/libbash X-VCS-Files: bashast/libbashWalker.g scripts/var_def.ebuild scripts/var_def.ebuild.result src/core/interpreter.cpp src/core/interpreter.h test/variable_printer.cpp X-VCS-Directories: scripts/ src/core/ bashast/ test/ X-VCS-Committer: betelgeuse X-VCS-Committer-Name: Petteri Räty X-VCS-Revision: 604169c50644cafde6af6b3cbed4aea6db4df721 Date: Thu, 14 Apr 2011 04:50:15 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 4f932b7d84f5b831de7031dba2ad9f5a commit: 604169c50644cafde6af6b3cbed4aea6db4df721 Author: Mu Qiao gentoo org> AuthorDate: Wed Apr 13 07:07:33 2011 +0000 Commit: Petteri R=C3=A4ty gentoo org> CommitDate: Thu Apr 14 01:20:31 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a= =3Dcommit;h=3D604169c5 Support referring a whole array Syntax like ${array[@]} and ${array[*]} is supported now. Currently the only difference between the two is when they are used in a double quoted string, ${array[*]} will use the first character of IFS as delimiter rather than space. Rename some variables in test script for better ordering. --- bashast/libbashWalker.g | 13 ++++++++++--- scripts/var_def.ebuild | 27 +++++++++++++++++---------- scripts/var_def.ebuild.result | 14 ++++++++++---- src/core/interpreter.cpp | 35 +++++++++++++++++++++++++++++++++++ src/core/interpreter.h | 13 +++++++++++++ test/variable_printer.cpp | 2 ++ 6 files changed, 87 insertions(+), 17 deletions(-) diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g index 9a3b7f2..47634e8 100644 --- a/bashast/libbashWalker.g +++ b/bashast/libbashWalker.g @@ -109,13 +109,13 @@ string_expr returns[std::string libbash_value]: ^(STRING( (DOUBLE_QUOTED_STRING) =3D> ^(DOUBLE_QUOTED_STRING (libbash_string=3Dd= ouble_quoted_string { $libbash_value +=3D libbash_string; })*) |(ARITHMETIC_EXPRESSION) =3D> ^(ARITHMETIC_EXPRESSION value=3Darithmet= ics { $libbash_value =3D boost::lexical_cast(value); }) - |(var_ref) =3D> libbash_string=3Dvar_ref { $libbash_value =3D libbash_= string; } + |(var_ref[false]) =3D> libbash_string=3Dvar_ref[false] { $libbash_valu= e =3D libbash_string; } |(libbash_string=3Dany_string { $libbash_value +=3D libbash_string; })= + )); =20 //double quoted string rule, allows expansions double_quoted_string returns[std::string libbash_value]: - (var_ref) =3D> libbash_string=3Dvar_ref { $libbash_value =3D libbash_st= ring; } + (var_ref[true]) =3D> libbash_string=3Dvar_ref[true] { $libbash_value =3D= libbash_string; } |(ARITHMETIC_EXPRESSION) =3D> ^(ARITHMETIC_EXPRESSION value=3Darithmeti= cs) { $libbash_value =3D boost::lexical_cast(value); } |libbash_string=3Dany_string { $libbash_value =3D libbash_string; }; =20 @@ -167,8 +167,15 @@ word returns[std::string libbash_value]: |value=3Darithmetics { $libbash_value =3D boost::lexical_cast(value); }; =20 //variable reference -var_ref returns[std::string libbash_value]: +var_ref [bool double_quoted] returns[std::string libbash_value]: ^(VAR_REF name) { $libbash_value =3D walker->resolve($name= .libbash_value, $name.index); } + |^(VAR_REF ^(libbash_string=3Dname_base AT)) { walker->get_all_elements= (libbash_string, $libbash_value); } + |^(VAR_REF ^(libbash_string=3Dname_base TIMES)) { + if(double_quoted) + walker->get_all_elements_IFS_joined(libbash_string, $libbash_value); + else + walker->get_all_elements(libbash_string, $libbash_value); + } |^(VAR_REF libbash_string=3Dvar_expansion) { $libbash_value =3D libbash= _string; }; =20 // shell arithmetic diff --git a/scripts/var_def.ebuild b/scripts/var_def.ebuild index 06ebb3b..c93a6de 100644 --- a/scripts/var_def.ebuild +++ b/scripts/var_def.ebuild @@ -12,16 +12,23 @@ DEPEND=3D"${RDEPEND} dev-util/pkgconfig" MY_PATCH=3Dldflags.patch PATCH=3D("1.patch" 2.patch) -ARRAY=3D(1 2 3 [5]=3D4 5) -ARRAY2=3D(1 2 3) -ARRAY2[2]=3D4 -ARRAY2[3]=3D5 +ARRAY01=3D(1 2 3 [5]=3D4 5) +ARRAY02=3D(1 2 3) +ARRAY02[2]=3D4 +ARRAY02[3]=3D5 EMPTY=3D -ARRAY_LAST=3D${ARRAY[6]} PARTIAL[5]=3D5 +ARRAY_LAST=3D${ARRAY01[6]} EMPTY_ARRAY=3D() -ARRAY3=3D(1 2 3) -ARRAY3[0]=3D -ARRAY4=3D(1 2 3) -# The following one is equivalent to ARRAY4[0]=3D -ARRAY4=3D +ARRAY03=3D(1 2 3) +ARRAY03[0]=3D +ARRAY04=3D(1 2 3) +# The following one is equivalent to ARRAY04[0]=3D +ARRAY04=3D +ARRAY05=3D(1 2 3 4 5) +ARRAY06=3D${ARRAY05[@]} +ARRAY07=3D${ARRAY05[*]} +ARRAY08=3D"${ARRAY05[@]}" +ARRAY09=3D"${ARRAY05[*]}" +IFS=3D";,:" +ARRAY10=3D"${ARRAY05[*]}" diff --git a/scripts/var_def.ebuild.result b/scripts/var_def.ebuild.resul= t index 6433df7..058bd98 100644 --- a/scripts/var_def.ebuild.result +++ b/scripts/var_def.ebuild.result @@ -1,7 +1,13 @@ -ARRAY=3D1 2 3 4 5 -ARRAY2=3D1 2 4 5 -ARRAY3=3D2 3 -ARRAY4=3D2 3 +ARRAY01=3D1 2 3 4 5 +ARRAY02=3D1 2 4 5 +ARRAY03=3D2 3 +ARRAY04=3D2 3 +ARRAY05=3D1 2 3 4 5 +ARRAY06=3D1 2 3 4 5 +ARRAY07=3D1 2 3 4 5 +ARRAY08=3D1 2 3 4 5 +ARRAY09=3D1 2 3 4 5 +ARRAY10=3D1;2;3;4;5 ARRAY_LAST=3D5 DEPEND=3Ddev-db/sqlite:3 dev-util/pkgconfig diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp index 0086216..9f10949 100644 --- a/src/core/interpreter.cpp +++ b/src/core/interpreter.cpp @@ -21,3 +21,38 @@ /// \author Mu Qiao /// \brief implementations for bash interpreter (visitor pattern). /// + +#include "core/interpreter.h" + +#include + +void interpreter::get_all_elements_joined(const std::string& name, + const std::string& delim, + std::string& result) +{ + std::vector source; + std::shared_ptr value =3D members.resolve(name); + if(value) + { + value->get_all_values(source); + result =3D boost::algorithm::join(source, delim); + } + else + { + result =3D ""; + } +} + +void interpreter::get_all_elements(const std::string& name, + std::string& result) +{ + get_all_elements_joined(name, " ", result); +} + +void interpreter::get_all_elements_IFS_joined(const std::string& name, + std::string& result) +{ + get_all_elements_joined(name, + resolve("IFS").substr(0, 1), + result); +} diff --git a/src/core/interpreter.h b/src/core/interpreter.h index 1e2babe..40663c0 100644 --- a/src/core/interpreter.h +++ b/src/core/interpreter.h @@ -56,8 +56,17 @@ class interpreter{ return !(offset < 0 || offset >=3D static_cast(str.size())); } =20 + void get_all_elements_joined(const std::string& name, + const std::string& delim, + std::string& result); + public: =20 + interpreter() + { + define("IFS", " \t\n"); + } + /// /// \brief return the number of variables /// \return the number of variables @@ -531,5 +540,9 @@ public: return 0; return value->get_array_length(); } + + void get_all_elements(const std::string&, std::string&); + + void get_all_elements_IFS_joined(const std::string&, std::string&); }; #endif diff --git a/test/variable_printer.cpp b/test/variable_printer.cpp index 32fc161..ed4aa69 100644 --- a/test/variable_printer.cpp +++ b/test/variable_printer.cpp @@ -44,6 +44,8 @@ int main(int argc, char** argv) libbash::interpret(argv[1], variables); =20 std::map> sorted(variables.begin= (), variables.end()); + // Currently we don't need internal variables + sorted.erase("IFS"); =20 using namespace boost::spirit::karma; std::cout << format((string << '=3D' << -(string % ' ')) % eol, sorted= ) << std::endl;