1 |
commit: 3b3d66ad8eccbdafc155aa065e96d50fbf7fa050 |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Wed Apr 20 09:10:03 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Wed Apr 20 13:44:30 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=3b3d66ad |
7 |
|
8 |
Walker: first support for command substitution |
9 |
|
10 |
A simple command substitution is supported. Currently the commands |
11 |
are executed within the current process. |
12 |
|
13 |
--- |
14 |
bashast/libbashWalker.g | 12 ++++++++++++ |
15 |
scripts/command_execution.ebuild | 2 ++ |
16 |
scripts/command_execution.ebuild.result | 2 ++ |
17 |
src/core/interpreter.cpp | 6 ++++++ |
18 |
src/core/interpreter.h | 13 +++++++++++++ |
19 |
5 files changed, 35 insertions(+), 0 deletions(-) |
20 |
|
21 |
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g |
22 |
index 72fbf73..b6123d9 100644 |
23 |
--- a/bashast/libbashWalker.g |
24 |
+++ b/bashast/libbashWalker.g |
25 |
@@ -39,6 +39,7 @@ options |
26 |
@postinclude{ |
27 |
|
28 |
#include <iostream> |
29 |
+ #include <sstream> |
30 |
|
31 |
#include <boost/format.hpp> |
32 |
|
33 |
@@ -144,6 +145,7 @@ string_expr returns[std::string libbash_value, bool quoted] |
34 |
$libbash_value += boost::lexical_cast<std::string>(value); $quoted = false; |
35 |
}) |
36 |
|(var_ref[false]) => libbash_string=var_ref[false] { $libbash_value += libbash_string; $quoted = false; } |
37 |
+ |libbash_string=command_substitution { $libbash_value += libbash_string; $quoted = false; } |
38 |
|(libbash_string=any_string { $libbash_value += libbash_string; $quoted = false; }) |
39 |
)+); |
40 |
|
41 |
@@ -308,6 +310,16 @@ command_list: ^(LIST logic_command_list+); |
42 |
|
43 |
compound_command: ^(CURRENT_SHELL command_list); |
44 |
|
45 |
+command_substitution returns[std::string libbash_value] |
46 |
+@declarations { |
47 |
+ std::stringstream out; |
48 |
+} |
49 |
+ :^(COMMAND_SUB{ walker->set_output_stream(&out); } command_list) { |
50 |
+ walker->restore_output_stream(); |
51 |
+ $libbash_value = out.str(); |
52 |
+ walker->trim_trailing_eols($libbash_value); |
53 |
+ }; |
54 |
+ |
55 |
function_def returns[int placeholder] |
56 |
:^(FUNCTION ^(STRING name) { |
57 |
// Define the function with current index |
58 |
|
59 |
diff --git a/scripts/command_execution.ebuild b/scripts/command_execution.ebuild |
60 |
index d76c230..0f7cd03 100644 |
61 |
--- a/scripts/command_execution.ebuild |
62 |
+++ b/scripts/command_execution.ebuild |
63 |
@@ -6,3 +6,5 @@ hi |
64 |
echo hello world |
65 |
true |
66 |
false |
67 |
+FOO001=$(echo hello) |
68 |
+FOO002=$(hi) |
69 |
|
70 |
diff --git a/scripts/command_execution.ebuild.result b/scripts/command_execution.ebuild.result |
71 |
index 984ab54..4330a65 100644 |
72 |
--- a/scripts/command_execution.ebuild.result |
73 |
+++ b/scripts/command_execution.ebuild.result |
74 |
@@ -1,2 +1,4 @@ |
75 |
Hello World |
76 |
hello world |
77 |
+FOO001=hello |
78 |
+FOO002=Hello World |
79 |
|
80 |
diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp |
81 |
index a5a3307..f7e8f64 100644 |
82 |
--- a/src/core/interpreter.cpp |
83 |
+++ b/src/core/interpreter.cpp |
84 |
@@ -30,6 +30,7 @@ |
85 |
#include <boost/algorithm/string/join.hpp> |
86 |
#include <boost/algorithm/string/replace.hpp> |
87 |
#include <boost/algorithm/string/split.hpp> |
88 |
+#include <boost/algorithm/string/trim.hpp> |
89 |
|
90 |
#include "libbashWalker.h" |
91 |
|
92 |
@@ -157,3 +158,8 @@ void interpreter::lazy_remove_at_end(std::string& value, |
93 |
{ |
94 |
replace_at_end(value, pattern, ""); |
95 |
} |
96 |
+ |
97 |
+void interpreter::trim_trailing_eols(std::string& value) |
98 |
+{ |
99 |
+ boost::trim_right_if(value, boost::is_any_of("\n")); |
100 |
+} |
101 |
|
102 |
diff --git a/src/core/interpreter.h b/src/core/interpreter.h |
103 |
index 0656299..af81c49 100644 |
104 |
--- a/src/core/interpreter.h |
105 |
+++ b/src/core/interpreter.h |
106 |
@@ -132,6 +132,16 @@ public: |
107 |
return members.end(); |
108 |
} |
109 |
|
110 |
+ void set_output_stream(std::ostream* stream) |
111 |
+ { |
112 |
+ out = stream; |
113 |
+ } |
114 |
+ |
115 |
+ void restore_output_stream() |
116 |
+ { |
117 |
+ out = &std::cout; |
118 |
+ } |
119 |
+ |
120 |
/// \brief parse the text value of a tree to integer |
121 |
/// \param the target tree |
122 |
/// \return the parsed value |
123 |
@@ -684,5 +694,8 @@ public: |
124 |
static void lazy_remove_at_end(std::string& value, |
125 |
const std::string& pattern); |
126 |
|
127 |
+ /// \brief remove trailing EOLs from the value |
128 |
+ /// \param[in, out] the target |
129 |
+ static void trim_trailing_eols(std::string& value); |
130 |
}; |
131 |
#endif |