public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Petteri Räty" <betelgeuse@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/libbash:master commit in: scripts/, bashast/
Date: Wed, 11 May 2011 07:19:34 +0000 (UTC)	[thread overview]
Message-ID: <3dbf3ca1f5e0884b41844e3dd1fa8538f5c28e5c.betelgeuse@gentoo> (raw)

commit:     3dbf3ca1f5e0884b41844e3dd1fa8538f5c28e5c
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Sun May  8 11:24:11 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Wed May 11 06:45:02 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=3dbf3ca1

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
 
 @includes{
 
+	#include <memory>
 	#include <string>
 	#include <vector>
 
+	#include <boost/xpressive/xpressive.hpp>
+
 	class interpreter;
 	void set_interpreter(interpreter* w);
 
@@ -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::xpressive::sregex& new_pattern, bool& do_append)
+	{
+		using namespace boost::xpressive;
+		if(do_append)
+		{
+			pattern = sregex(pattern >> new_pattern);
+		}
+		else
+		{
+			pattern = new_pattern;
+			do_append = true;
+		}
+	}
+
+	bool match(const std::string& target,
+			   const boost::xpressive::sregex& pattern)
+	{
+	  return boost::xpressive::regex_match(target, pattern);
+	}
 }
 
 start: list|EOF;
@@ -160,6 +186,27 @@ string_expr returns[std::string libbash_value, bool quoted]
 		|(libbash_string=any_string { $libbash_value += libbash_string; $quoted = false; })
 	)+);
 
+bash_pattern[boost::xpressive::sregex& pattern]
+@declarations {
+	using namespace boost::xpressive;
+	bool do_append = false;
+}
+	:^(STRING (
+		(DOUBLE_QUOTED_STRING) =>
+			^(DOUBLE_QUOTED_STRING (libbash_string=double_quoted_string {
+				append($pattern, as_xpr(libbash_string), do_append);
+			})*)
+		|(MATCH_ALL) => MATCH_ALL {
+			append($pattern, *_, do_append);
+		}
+		|(MATCH_ONE) => MATCH_ONE {
+			append($pattern, _, do_append);
+		}
+		|(libbash_string=any_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]) => libbash_string=var_ref[true] { $libbash_value = libbash_string; }
@@ -209,50 +256,50 @@ var_expansion returns[std::string libbash_value]
 			libbash_value = boost::lexical_cast<std::string>(walker->get_array_length(libbash_name));
 		}
 	))
-	|^(REPLACE_ALL var_name pattern=string_expr (replacement=string_expr)?) {
+	|^(REPLACE_ALL var_name replace_pattern=string_expr (replacement=string_expr)?) {
 		libbash_value = 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=string_expr (replacement=string_expr)?) {
+	|^(REPLACE_AT_END var_name replace_pattern=string_expr (replacement=string_expr)?) {
 		libbash_value = 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=string_expr (replacement=string_expr)?) {
+	|^(REPLACE_AT_START var_name replace_pattern=string_expr (replacement=string_expr)?) {
 		libbash_value = 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=string_expr (replacement=string_expr)?) {
+	|^(REPLACE_FIRST var_name replace_pattern=string_expr (replacement=string_expr)?) {
 		libbash_value = 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=string_expr) {
+	|^(LAZY_REMOVE_AT_START var_name replace_pattern=string_expr) {
 		libbash_value = 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=string_expr) {
+	|^(LAZY_REMOVE_AT_END var_name replace_pattern=string_expr) {
 		libbash_value = 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);
 	};
 
@@ -524,35 +571,29 @@ case_expr
 
 case_clause[const std::string& target] returns[bool matched]
 @declarations {
-	std::vector<std::string> patterns;
+	std::vector<boost::xpressive::sregex> patterns;
 }
-	:^(CASE_PATTERN (libbash_string=case_pattern { patterns.push_back(libbash_string); })+ {
+	:^(CASE_PATTERN ( { patterns.push_back(boost::xpressive::sregex()); } bash_pattern[patterns.back()])+ {
 		if(LA(1) == CASE_COMMAND)
 		{
 			// omit CASE_COMMAND
 			SEEK(INDEX() + 1);
-			matched = false;
+			$matched = false;
+
 			for(auto iter = patterns.begin(); iter != patterns.end(); ++iter)
 			{
-				// pattern matching should happen here in future
-				if(*iter == "*" || *iter == target)
+				if(match(target, *iter))
 				{
 					command_list(ctx);
 					$matched = true;
-				}
-				else
-				{
-					seek_to_next_tree(ctx);
+					break;
 				}
 			}
+			if(!$matched)
+				seek_to_next_tree(ctx);
 		}
 	});
 
-case_pattern returns[std::string libbash_value]
-	:libbash_string=command_substitution { $libbash_value = libbash_string; }
-	|(^(STRING MATCH_ALL)) => ^(STRING MATCH_ALL) { $libbash_value = "*"; }
-	|string_expr { $libbash_value = $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.bash
index 6365fbe..b521b77 100644
--- a/scripts/compound_command.bash
+++ b/scripts/compound_command.bash
@@ -85,13 +85,13 @@ fi
 
 target=123
 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)



             reply	other threads:[~2011-05-11  7:20 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-11  7:19 Petteri Räty [this message]
  -- strict thread matches above, loose matches on Subject: below --
2012-08-19 15:23 [gentoo-commits] proj/libbash:master commit in: scripts/, bashast/ Petteri Räty
2012-08-19 14:54 Petteri Räty
2012-08-19 14:54 Petteri Räty
2012-08-19 14:29 Petteri Räty
2012-08-07  2:38 Mu Qiao
2012-08-07  2:38 Mu Qiao
2012-08-07  2:38 Mu Qiao
2012-07-29 12:46 Petteri Räty
2012-07-29 12:45 Petteri Räty
2012-07-08  9:44 Petteri Räty
2012-07-08  9:31 Petteri Räty
2012-06-03  9:08 Petteri Räty
2012-06-03  9:08 Petteri Räty
2012-06-03  9:08 Petteri Räty
2012-06-03  9:08 Petteri Räty
2012-06-03  9:08 Petteri Räty
2012-06-03  9:08 Petteri Räty
2011-08-04 13:53 Petteri Räty
2011-08-04 13:53 Petteri Räty
2011-08-04 13:53 Petteri Räty
2011-08-04 13:53 Petteri Räty
2011-08-04 13:53 Petteri Räty
2011-08-04 13:53 Petteri Räty
2011-07-20 13:08 Petteri Räty
2011-06-25 10:30 Petteri Räty
2011-06-25 10:30 Petteri Räty
2011-06-25 10:30 Petteri Räty
2011-06-25 10:05 Petteri Räty
2011-06-25 10:05 Petteri Räty
2011-06-19 19:15 Petteri Räty
2011-06-18  9:55 Petteri Räty
2011-06-16 16:53 Petteri Räty
2011-06-16 16:53 Petteri Räty
2011-06-11  8:52 Petteri Räty
2011-06-11  8:52 Petteri Räty
2011-06-09 11:46 Petteri Räty
2011-06-09  8:15 Petteri Räty
2011-06-09  7:27 Petteri Räty
2011-06-03 12:43 Petteri Räty
2011-06-01 12:19 Petteri Räty
2011-05-29 11:20 Petteri Räty
2011-05-29 11:20 Petteri Räty
2011-05-29 11:20 Petteri Räty
2011-05-28 13:11 Petteri Räty
2011-05-28 13:11 Petteri Räty
2011-05-24 14:50 Petteri Räty
2011-05-24 14:50 Petteri Räty
2011-05-24 14:50 Petteri Räty
2011-05-23 14:39 Petteri Räty
2011-05-22 21:00 Petteri Räty
2011-05-12 14:06 Petteri Räty
2011-05-12 14:06 Petteri Räty
2011-05-11  7:19 Petteri Räty
2011-05-11  7:19 Petteri Räty
2011-05-11  7:19 Petteri Räty
2011-05-06 10:29 Petteri Räty
2011-04-27 15:11 Petteri Räty
2011-04-27 15:11 Petteri Räty
2011-04-20 14:04 Petteri Räty
2011-04-14  4:50 Petteri Räty
2011-04-12 18:29 Petteri Räty
2011-04-12 18:29 Petteri Räty
2011-04-04 16:09 Petteri Räty
2011-04-03 13:09 Petteri Räty
2011-04-03 10:16 Petteri Räty

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3dbf3ca1f5e0884b41844e3dd1fa8538f5c28e5c.betelgeuse@gentoo \
    --to=betelgeuse@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox