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 1QZ0yX-0001Od-Od for garchives@archives.gentoo.org; Tue, 21 Jun 2011 13:26:42 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 70EF01C01A; Tue, 21 Jun 2011 13:26:33 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 3000F1C01A for ; Tue, 21 Jun 2011 13:26:33 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id A6EA41B4020 for ; Tue, 21 Jun 2011 13:26:32 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 0C19B8003E for ; Tue, 21 Jun 2011 13:26:32 +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: <602e860e3ef44afbd8af4b5aafff993a1a0d8aed.betelgeuse@gentoo> Subject: [gentoo-commits] proj/libbash:master commit in: scripts/, bashast/, test/ X-VCS-Repository: proj/libbash X-VCS-Files: bashast/libbashWalker.g scripts/var_expansion.bash scripts/var_expansion.bash.result test/walker_test.cpp X-VCS-Directories: scripts/ bashast/ test/ X-VCS-Committer: betelgeuse X-VCS-Committer-Name: Petteri Räty X-VCS-Revision: 602e860e3ef44afbd8af4b5aafff993a1a0d8aed Date: Tue, 21 Jun 2011 13:26:32 +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: 304501ad812f6b2ff4b50d1b76f45f26 commit: 602e860e3ef44afbd8af4b5aafff993a1a0d8aed Author: Mu Qiao gentoo org> AuthorDate: Wed Jun 15 07:12:14 2011 +0000 Commit: Petteri R=C3=A4ty gentoo org> CommitDate: Tue Jun 21 13:23:34 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a= =3Dcommit;h=3D602e860e Walker: first support for brace expansion We only support brace expansion in raw string for now. Other cases will cause an interpreter_exception. --- bashast/libbashWalker.g | 38 +++++++++++++++++++++++++++++++= ++++- scripts/var_expansion.bash | 3 ++ scripts/var_expansion.bash.result | 3 ++ test/walker_test.cpp | 10 +++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g index fda4ef6..320e69a 100644 --- a/bashast/libbashWalker.g +++ b/bashast/libbashWalker.g @@ -45,6 +45,7 @@ options #include =20 #include + #include =20 #include "builtins/builtin_exceptions.h" #include "core/bash_condition.h" @@ -210,17 +211,47 @@ var_def[bool local] string_expr returns[std::string libbash_value, bool quoted] @init { $quoted =3D true; + bool is_raw_string =3D true; + std::vector brace_expansion_base{""}; + std::vector brace_expansion_elements; +} +@after { + if(!is_raw_string && brace_expansion_base.size() > 1) + throw libbash::interpreter_exception("We only support brace expansion = in raw string for now"); + $libbash_value =3D boost::algorithm::join(brace_expansion_base, " "); } :^(STRING ( string_part { - $libbash_value +=3D $string_part.libbash_value; + for(auto iter =3D brace_expansion_base.begin(); iter !=3D brace_expan= sion_base.end(); ++iter) + *iter +=3D $string_part.libbash_value; $quoted =3D $string_part.quoted; + if(is_raw_string) + is_raw_string =3D $string_part.is_raw_string; + } + |(BRACE_EXP) =3D> ^(BRACE_EXP brace_expansion[brace_expansion_elements= ]) { + auto group_number =3D brace_expansion_elements.size(); + auto group_size =3D brace_expansion_base.size(); + std::vector expanded_values; + + expanded_values.reserve(group_number * group_size); + for(auto iter =3D brace_expansion_base.begin(); iter !=3D brace_expan= sion_base.end(); ++iter) + for(std::vector::size_type i =3D 0u; i !=3D group_numbe= r; ++i) + expanded_values.push_back(*iter + brace_expansion_elements[i \% gro= up_number]); + + brace_expansion_base.swap(expanded_values); + brace_expansion_elements.clear(); } )*); =20 -string_part returns[std::string libbash_value, bool quoted] +brace_expansion[std::vector& elements] + :(string_expr{ + $elements.push_back($string_expr.libbash_value); + })+; + +string_part returns[std::string libbash_value, bool quoted, bool is_raw_= string] @init { $quoted =3D false; + $is_raw_string =3D true; } :(DOUBLE_QUOTED_STRING) =3D> ^(DOUBLE_QUOTED_STRING (libbash_string=3Ddouble_quoted_string { @@ -235,12 +266,15 @@ string_part returns[std::string libbash_value, bool= quoted] |(ARITHMETIC_EXPRESSION) =3D> ^(ARITHMETIC_EXPRESSION value=3Darithmetics { $libbash_value =3D boost::lexical_cast(value); + $is_raw_string =3D false; }) |(var_ref[false]) =3D> libbash_string=3Dvar_ref[false] { $libbash_value =3D libbash_string; + $is_raw_string =3D false; } |libbash_string=3Dcommand_substitution { $libbash_value =3D libbash_string; + $is_raw_string =3D false; } |(libbash_string=3Dany_string { $libbash_value =3D libbash_string; diff --git a/scripts/var_expansion.bash b/scripts/var_expansion.bash index 9f42f19..16573a9 100644 --- a/scripts/var_expansion.bash +++ b/scripts/var_expansion.bash @@ -110,3 +110,6 @@ ARRAY=3D(1 2 3 4 5) echo ${ARRAY[@]:1} echo ${ARRAY[@]:1:3} echo $# +echo a{b,c}d +echo a{a,bc}d{e,}f +echo a{ab,cd}d{ef,gh} diff --git a/scripts/var_expansion.bash.result b/scripts/var_expansion.ba= sh.result index bd83c62..52c87c0 100644 --- a/scripts/var_expansion.bash.result +++ b/scripts/var_expansion.bash.result @@ -5,6 +5,9 @@ abcabc 2 3 4 5 2 3 4 0 +abd acd +aadef aadf abcdef abcdf +aabdef aabdgh acddef acddgh ARRAY=3D1 2 3 4 5 ARRAY2=3Dhello EAPI=3D3 diff --git a/test/walker_test.cpp b/test/walker_test.cpp index 5a64b04..20cb00d 100644 --- a/test/walker_test.cpp +++ b/test/walker_test.cpp @@ -90,3 +90,13 @@ TEST(extglob, used_when_disabled) EXPECT_STREQ(e.what(), "Entered extended pattern matching with extgl= ob disabled"); } } + +TEST(brace_expansion, not_in_raw_string) +{ + interpreter walker; + + std::string script =3D "echo $ab{c,d}e"; + std::istringstream input(script); + bash_ast ast(input); + EXPECT_THROW(ast.interpret_with(walker), libbash::interpreter_exceptio= n); +}