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 1QK3iP-0003uH-4f for garchives@archives.gentoo.org; Wed, 11 May 2011 07:20:13 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 6663A1C0CF; Wed, 11 May 2011 07:19:36 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 200E41C0CE for ; Wed, 11 May 2011 07:19:36 +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 8E5E41B40B4 for ; Wed, 11 May 2011 07:19:35 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id F045C80505 for ; Wed, 11 May 2011 07:19:34 +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: <3dbf3ca1f5e0884b41844e3dd1fa8538f5c28e5c.betelgeuse@gentoo> Subject: [gentoo-commits] proj/libbash:master commit in: scripts/, bashast/ X-VCS-Repository: proj/libbash X-VCS-Files: bashast/libbashWalker.g scripts/compound_command.bash X-VCS-Directories: scripts/ bashast/ X-VCS-Committer: betelgeuse X-VCS-Committer-Name: Petteri Räty X-VCS-Revision: 3dbf3ca1f5e0884b41844e3dd1fa8538f5c28e5c Date: Wed, 11 May 2011 07:19:34 +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: b75890efdaedc721399133a76ebde985 commit: 3dbf3ca1f5e0884b41844e3dd1fa8538f5c28e5c Author: Mu Qiao gentoo org> AuthorDate: Sun May 8 11:24:11 2011 +0000 Commit: Petteri R=C3=A4ty gentoo org> CommitDate: Wed May 11 06:45:02 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a= =3Dcommit;h=3D3dbf3ca1 Walker: first support for basic pattern matching --- bashast/libbashWalker.g | 93 +++++++++++++++++++++++++++++------= ----- scripts/compound_command.bash | 6 +- 2 files changed, 70 insertions(+), 29 deletions(-) diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g index fb3add0..e328c21 100644 --- a/bashast/libbashWalker.g +++ b/bashast/libbashWalker.g @@ -27,9 +27,12 @@ options =20 @includes{ =20 + #include #include #include =20 + #include + class interpreter; void set_interpreter(interpreter* w); =20 @@ -86,6 +89,29 @@ options SEEK(INDEX() + index - 3); CONSUME(); } + + // The method is used to append a pattern with another one. Because it'= s not allowed to append an empty pattern, + // we need the argument 'do_append' to indicate whether the pattern is = empty. 'do_append' will be set to true after + // the first assignment. + inline void append(boost::xpressive::sregex& pattern, const boost::xpre= ssive::sregex& new_pattern, bool& do_append) + { + using namespace boost::xpressive; + if(do_append) + { + pattern =3D sregex(pattern >> new_pattern); + } + else + { + pattern =3D new_pattern; + do_append =3D true; + } + } + + bool match(const std::string& target, + const boost::xpressive::sregex& pattern) + { + return boost::xpressive::regex_match(target, pattern); + } } =20 start: list|EOF; @@ -160,6 +186,27 @@ string_expr returns[std::string libbash_value, bool = quoted] |(libbash_string=3Dany_string { $libbash_value +=3D libbash_string; $q= uoted =3D false; }) )+); =20 +bash_pattern[boost::xpressive::sregex& pattern] +@declarations { + using namespace boost::xpressive; + bool do_append =3D false; +} + :^(STRING ( + (DOUBLE_QUOTED_STRING) =3D> + ^(DOUBLE_QUOTED_STRING (libbash_string=3Ddouble_quoted_string { + append($pattern, as_xpr(libbash_string), do_append); + })*) + |(MATCH_ALL) =3D> MATCH_ALL { + append($pattern, *_, do_append); + } + |(MATCH_ONE) =3D> MATCH_ONE { + append($pattern, _, do_append); + } + |(libbash_string=3Dany_string { + append($pattern, as_xpr(libbash_string), do_append); + }) + )+); + //double quoted string rule, allows expansions double_quoted_string returns[std::string libbash_value] :(var_ref[true]) =3D> libbash_string=3Dvar_ref[true] { $libbash_value =3D= libbash_string; } @@ -209,50 +256,50 @@ var_expansion returns[std::string libbash_value] libbash_value =3D boost::lexical_cast(walker->get_array_= length(libbash_name)); } )) - |^(REPLACE_ALL var_name pattern=3Dstring_expr (replacement=3Dstring_exp= r)?) { + |^(REPLACE_ALL var_name replace_pattern=3Dstring_expr (replacement=3Dst= ring_expr)?) { libbash_value =3D walker->do_replace_expansion($var_name.libbash_value= , std::bind(&interpreter::replace_all, std::placeholders::_1, - pattern.libbash_value, + replace_pattern.libbash_value, replacement.libbash_value), $var_name.index); } - |^(REPLACE_AT_END var_name pattern=3Dstring_expr (replacement=3Dstring_= expr)?) { + |^(REPLACE_AT_END var_name replace_pattern=3Dstring_expr (replacement=3D= string_expr)?) { libbash_value =3D walker->do_replace_expansion($var_name.libbash_value= , std::bind(&interpreter::replace_at_end, std::placeholders::_1, - pattern.libbash_value, + replace_pattern.libbash_value, replacement.libbash_value), $var_name.index); } - |^(REPLACE_AT_START var_name pattern=3Dstring_expr (replacement=3Dstrin= g_expr)?) { + |^(REPLACE_AT_START var_name replace_pattern=3Dstring_expr (replacement= =3Dstring_expr)?) { libbash_value =3D walker->do_replace_expansion($var_name.libbash_value= , std::bind(&interpreter::replace_at_start, std::placeholders::_1, - pattern.libbash_value, + replace_pattern.libbash_value, replacement.libbash_value), $var_name.index); } - |^(REPLACE_FIRST var_name pattern=3Dstring_expr (replacement=3Dstring_e= xpr)?) { + |^(REPLACE_FIRST var_name replace_pattern=3Dstring_expr (replacement=3D= string_expr)?) { libbash_value =3D walker->do_replace_expansion($var_name.libbash_value= , std::bind(&interpreter::replace_first, std::placeholders::_1, - pattern.libbash_value, + replace_pattern.libbash_value, replacement.libbash_value), $var_name.index); } - |^(LAZY_REMOVE_AT_START var_name pattern=3Dstring_expr) { + |^(LAZY_REMOVE_AT_START var_name replace_pattern=3Dstring_expr) { libbash_value =3D walker->do_replace_expansion($var_name.libbash_value= , std::bind(&interpreter::lazy_remove_at_start, std::placeholders::_1, - pattern.libbash_value), + replace_pattern.libbash_value), $var_name.index); } - |^(LAZY_REMOVE_AT_END var_name pattern=3Dstring_expr) { + |^(LAZY_REMOVE_AT_END var_name replace_pattern=3Dstring_expr) { libbash_value =3D walker->do_replace_expansion($var_name.libbash_value= , std::bind(&interpreter::lazy_remove_at_end, std::placeholders::_1, - pattern.libbash_value), + replace_pattern.libbash_value), $var_name.index); }; =20 @@ -524,35 +571,29 @@ case_expr =20 case_clause[const std::string& target] returns[bool matched] @declarations { - std::vector patterns; + std::vector patterns; } - :^(CASE_PATTERN (libbash_string=3Dcase_pattern { patterns.push_back(lib= bash_string); })+ { + :^(CASE_PATTERN ( { patterns.push_back(boost::xpressive::sregex()); } b= ash_pattern[patterns.back()])+ { if(LA(1) =3D=3D CASE_COMMAND) { // omit CASE_COMMAND SEEK(INDEX() + 1); - matched =3D false; + $matched =3D false; + for(auto iter =3D patterns.begin(); iter !=3D patterns.end(); ++iter) { - // pattern matching should happen here in future - if(*iter =3D=3D "*" || *iter =3D=3D target) + if(match(target, *iter)) { command_list(ctx); $matched =3D true; - } - else - { - seek_to_next_tree(ctx); + break; } } + if(!$matched) + seek_to_next_tree(ctx); } }); =20 -case_pattern returns[std::string libbash_value] - :libbash_string=3Dcommand_substitution { $libbash_value =3D libbash_str= ing; } - |(^(STRING MATCH_ALL)) =3D> ^(STRING MATCH_ALL) { $libbash_value =3D "*= "; } - |string_expr { $libbash_value =3D $string_expr.libbash_value; }; - command_substitution returns[std::string libbash_value] @declarations { std::stringstream out; diff --git a/scripts/compound_command.bash b/scripts/compound_command.bas= h index 6365fbe..b521b77 100644 --- a/scripts/compound_command.bash +++ b/scripts/compound_command.bash @@ -85,13 +85,13 @@ fi =20 target=3D123 case $target in - bcd) + 1.3) echo "Shouldn't print this" ;; - abc) + \d+) echo "Shouldn't print this" ;; - 123) + 456|1?*|789) echo yep ;; 123)