Better formatting, fix special values (true, false, null), fix -bash, ... parsing
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@792047 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Closure.java b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Closure.java
index 8b8d0f7..116b1a8 100644
--- a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Closure.java
+++ b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Closure.java
@@ -212,25 +212,33 @@
StringBuilder sb = null;
Parser p = new Parser(seq);
int start = p.current;
- while (!p.eof()) {
+ while (!p.eof())
+ {
char c = p.peek();
- if (!p.escaped) {
- if (c == '$' || c == '<' || c == '\'' || c == '"' || c == '[' || c == '{') {
- if (start != p.current || res != null) {
- if (sb == null) {
+ if (!p.escaped)
+ {
+ if (c == '$' || c == '<' || c == '\'' || c == '"' || c == '[' || c == '{')
+ {
+ if (start != p.current || res != null)
+ {
+ if (sb == null)
+ {
sb = new StringBuilder();
- if (res != null) {
+ if (res != null)
+ {
sb.append(res);
res = null;
}
}
- if (start != p.current) {
+ if (start != p.current)
+ {
sb.append(new Parser(p.text.subSequence(start, p.current)).unescape());
start = p.current;
continue;
}
}
- switch (c) {
+ switch (c)
+ {
case '\'':
p.next();
p.quote(c);
@@ -269,25 +277,45 @@
}
p.next();
}
- if (start != p.current) {
- if (sb == null) {
+ if (start != p.current)
+ {
+ if (sb == null)
+ {
sb = new StringBuilder();
- if (res != null) {
+ if (res != null)
+ {
sb.append(res);
res = null;
}
}
sb.append(new Parser(p.text.subSequence(start, p.current)).unescape());
}
- if (sb != null) {
- if (res != null) {
+ if (sb != null)
+ {
+ if (res != null)
+ {
sb.append(res);
}
- return sb.toString();
+ res = sb;
}
- if (res instanceof CharSequence) {
- return res.toString();
+ if (res instanceof CharSequence)
+ {
+ String r = res.toString();
+ if ("null".equals(r))
+ {
+ return null;
+ }
+ else if ("false".equals(r))
+ {
+ return false;
+ }
+ else if ("true".equals(r))
+ {
+ return true;
+ }
+ return r;
}
+
return res;
}
diff --git a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Parser.java b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Parser.java
index a2d639b..e705335 100644
--- a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Parser.java
+++ b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Parser.java
@@ -372,7 +372,7 @@
return text.subSequence(start, end);
}
- if (Character.isJavaIdentifierStart(c))
+ if (Character.isJavaIdentifierPart(c))
{
while (c == '$') {
c = next();
diff --git a/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/shell/TestParser.java b/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/shell/TestParser.java
index adc9802..25dfc0d 100644
--- a/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/shell/TestParser.java
+++ b/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/shell/TestParser.java
@@ -35,6 +35,13 @@
{
int beentheredonethat = 0;
+ public void testSpecialValues() throws Exception {
+ Context c = new Context();
+ assertEquals(false, c.execute("false"));
+ assertEquals(true, c.execute("true"));
+ assertEquals(null, c.execute("null"));
+ }
+
public void testQuotes() throws Exception {
Context c = new Context();
c.addCommand("echo", this);
@@ -107,12 +114,16 @@
{
Context c = new Context();
c.addCommand("echo", this);
+ c.addCommand("capture", this);
+
assertEquals("http://www.aqute.biz?com=2&biz=1", c.execute("['http://www.aqute.biz?com=2&biz=1'] get 0").toString());
assertEquals("{a=2, b=3}", c.execute("[a=2 b=3]").toString());
assertEquals("3", c.execute("[a=2 <b>=<3>] get b").toString());
assertEquals("[3, 4]", c.execute("[1 2 [3 4] 5 6] get 2").toString());
assertEquals(5, c.execute("[1 2 [3 4] 5 6] size"));
-
+ assertEquals("a", c.execute("e = { echo $0 } ; <e a b | capture>"));
+ assertEquals("b", c.execute("e = { echo $1 } ; <e a b | capture>"));
+ assertEquals("a b", c.execute("e = { echo $args } ; <e a b | capture>"));
}
public void testArray() throws Exception