You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2006/05/29 08:20:48 UTC

svn commit: r410028 - in /geronimo/sandbox/gshell/trunk/gshell-core/src: main/grammar/ main/java/org/apache/geronimo/gshell/commandline/parser/ test/java/org/apache/geronimo/gshell/commandline/parser/

Author: jdillon
Date: Sun May 28 23:20:47 2006
New Revision: 410028

URL: http://svn.apache.org/viewvc?rev=410028&view=rev
Log:
Added vistor bits to node impls + simple test to verify it works
Fixed problem with plain string eating quotes + tests to keep it from happing again

Added:
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserVisitorTest.java   (with props)
Modified:
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/grammar/CommandLineParser.jjt
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTOpaqueString.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTPlainString.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTQuotedString.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/StringSupport.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserTest.java

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/grammar/CommandLineParser.jjt
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/grammar/CommandLineParser.jjt?rev=410028&r1=410027&r2=410028&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/grammar/CommandLineParser.jjt (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/grammar/CommandLineParser.jjt Sun May 28 23:20:47 2006
@@ -60,7 +60,7 @@
 // WHITE SPACE
 //
 
-SKIP :
+<DEFAULT> SKIP :
 {
     " "
 |   "\t"
@@ -73,7 +73,7 @@
 // COMMENTS
 //
 
-SPECIAL_TOKEN :
+<DEFAULT> SPECIAL_TOKEN :
 {
     < COMMENT: "#" (~["\n", "\r"])* ("\n" | "\r" | "\r\n")? >
 }
@@ -82,10 +82,10 @@
 // STRINGS
 //
 
-TOKEN :
+<DEFAULT> TOKEN :
 {
     < STRING:
-      (   (~["\"","\\"," ","\t","\n","\r","\f",";"])
+      (   (~["\"","\\"," ","\t","\n","\r","\f",";","\"","'"])
         | ("\\"
             ( ["n","t","b","r","f","\\","'","\"",";"]
             | ["0"-"7"] ( ["0"-"7"] )?
@@ -126,7 +126,7 @@
 // SEPARATORS
 //
 
-TOKEN :
+<DEFAULT> TOKEN :
 {
     < SEMICOLON: ";" >
 }
@@ -137,7 +137,7 @@
 ASTCommandLine commandLine() #CommandLine: {}
 {
     (
-        expression() ( ";" ( expression() )? )* | <EOF>
+        expression() ( ";" [ expression() ] )* | <EOF>
     )
     {
         return jjtThis;

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTOpaqueString.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTOpaqueString.java?rev=410028&r1=410027&r2=410028&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTOpaqueString.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTOpaqueString.java Sun May 28 23:20:47 2006
@@ -31,4 +31,13 @@
     public ASTOpaqueString(CommandLineParser p, int id) {
         super(p, id);
     }
+
+    public String getValue() {
+        return unquote(super.getValue());
+    }
+
+    /** Accept the visitor. **/
+    public Object jjtAccept(final CommandLineParserVisitor visitor, final Object data) {
+        return visitor.visit(this, data);
+    }
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTPlainString.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTPlainString.java?rev=410028&r1=410027&r2=410028&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTPlainString.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTPlainString.java Sun May 28 23:20:47 2006
@@ -31,4 +31,9 @@
     public ASTPlainString(CommandLineParser p, int id) {
         super(p, id);
     }
+
+    /** Accept the visitor. **/
+    public Object jjtAccept(final CommandLineParserVisitor visitor, final Object data) {
+        return visitor.visit(this, data);
+    }
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTQuotedString.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTQuotedString.java?rev=410028&r1=410027&r2=410028&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTQuotedString.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTQuotedString.java Sun May 28 23:20:47 2006
@@ -24,11 +24,20 @@
 public class ASTQuotedString
     extends StringSupport
 {
-    public ASTQuotedString(int id) {
+    public ASTQuotedString(final int id) {
         super(id);
     }
 
-    public ASTQuotedString(CommandLineParser p, int id) {
+    public ASTQuotedString(final CommandLineParser p, final int id) {
         super(p, id);
+    }
+
+    public String getValue() {
+        return unquote(super.getValue());
+    }
+
+    /** Accept the visitor. **/
+    public Object jjtAccept(final CommandLineParserVisitor visitor, final Object data) {
+        return visitor.visit(this, data);
     }
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/StringSupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/StringSupport.java?rev=410028&r1=410027&r2=410028&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/StringSupport.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/StringSupport.java Sun May 28 23:20:47 2006
@@ -26,11 +26,11 @@
 {
     protected Token token;
 
-    public StringSupport(int id) {
+    public StringSupport(final int id) {
         super(id);
     }
 
-    public StringSupport(CommandLineParser p, int id) {
+    public StringSupport(final CommandLineParser p, final int id) {
         super(p, id);
     }
 
@@ -45,10 +45,28 @@
     }
 
     public String getValue() {
-        return getToken().image;
+        Token t = getToken();
+        if (t == null) {
+            throw new IllegalStateException("Token not set");
+        }
+
+        return t.image;
     }
 
     public String toString() {
         return super.toString() + "( " + getToken() + " )";
+    }
+
+    /**
+     * Returns an unquoted value.
+     *
+     * @param value     String to unquote, must not be null; length must be at least 2
+     * @return          Unquoted value
+     */
+    protected String unquote(String value) {
+        assert value != null;
+        assert value.length() >= 2;
+
+        return value.substring(1, value.length() - 1);
     }
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserTest.java?rev=410028&r1=410027&r2=410028&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserTest.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserTest.java Sun May 28 23:20:47 2006
@@ -144,7 +144,8 @@
 
         node = child.jjtGetChild(1);
         assertEquals(ASTQuotedString.class, node.getClass());
-        assertEquals("\"b -c\"", ((StringSupport)node).getValue());
+        assertEquals("b -c", ((StringSupport)node).getValue());
+        assertEquals("\"b -c\"", ((StringSupport)node).getToken().image);
 
         node = child.jjtGetChild(2);
         assertEquals(ASTPlainString.class, node.getClass());
@@ -171,9 +172,72 @@
 
         node = child.jjtGetChild(1);
         assertEquals(ASTOpaqueString.class, node.getClass());
-        assertEquals("'b -c'", ((StringSupport)node).getValue());
+        assertEquals("b -c", ((StringSupport)node).getValue());
+        assertEquals("'b -c'", ((StringSupport)node).getToken().image);
 
         node = child.jjtGetChild(2);
+        assertEquals(ASTPlainString.class, node.getClass());
+    }
+
+    public void testMoreStrings1() throws Exception {
+        String input = "a 'b -c' \"d\" e";
+
+        ASTCommandLine cl = parse(input);
+
+        // One expression
+        assertEquals(1, cl.jjtGetNumChildren());
+
+        Node child = cl.jjtGetChild(0);
+        assertEquals(4, child.jjtGetNumChildren());
+
+        Node node;
+
+        node = child.jjtGetChild(0);
+        assertEquals(ASTPlainString.class, node.getClass());
+        assertEquals("a", ((StringSupport)node).getValue());
+
+        node = child.jjtGetChild(1);
+        assertEquals(ASTOpaqueString.class, node.getClass());
+        assertEquals("b -c", ((StringSupport)node).getValue());
+        assertEquals("'b -c'", ((StringSupport)node).getToken().image);
+
+        node = child.jjtGetChild(2);
+        assertEquals(ASTQuotedString.class, node.getClass());
+        assertEquals("d", ((StringSupport)node).getValue());
+        assertEquals("\"d\"", ((StringSupport)node).getToken().image);
+
+        node = child.jjtGetChild(3);
+        assertEquals(ASTPlainString.class, node.getClass());
+    }
+
+    public void testMoreStrings2() throws Exception {
+        String input = "a \"b -c\" 'd' e";
+
+        ASTCommandLine cl = parse(input);
+
+        // One expression
+        assertEquals(1, cl.jjtGetNumChildren());
+
+        Node child = cl.jjtGetChild(0);
+        assertEquals(4, child.jjtGetNumChildren());
+
+        Node node;
+
+        node = child.jjtGetChild(0);
+        assertEquals(ASTPlainString.class, node.getClass());
+        assertEquals("a", ((StringSupport)node).getValue());
+
+        node = child.jjtGetChild(1);
+        assertEquals(ASTQuotedString.class, node.getClass());
+        assertEquals("b -c", ((StringSupport)node).getValue());
+        assertEquals("\"b -c\"", ((StringSupport)node).getToken().image);
+
+        node = child.jjtGetChild(2);
+        assertEquals(ASTOpaqueString.class, node.getClass());
+        assertEquals("d", ((StringSupport)node).getValue());
+        assertEquals("'d'", ((StringSupport)node).getToken().image);
+
+        node = child.jjtGetChild(3);
         assertEquals(ASTPlainString.class, node.getClass());
     }
 

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserVisitorTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserVisitorTest.java?rev=410028&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserVisitorTest.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserVisitorTest.java Sun May 28 23:20:47 2006
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.geronimo.gshell.commandline.parser;
+
+import junit.framework.TestCase;
+
+import java.io.Reader;
+import java.io.StringReader;
+
+/**
+ * Unit tests for the {@link CommandLineParserVisitor} usage.
+ *
+ * @version $Id$
+ */
+public class CommandLineParserVisitorTest
+    extends TestCase
+{
+    private ASTCommandLine parse(final String input) throws ParseException {
+        assert input != null;
+
+        Reader reader = new StringReader(input);
+        CommandLineParser parser = new CommandLineParser();
+        ASTCommandLine cl = parser.parse(reader);
+
+        //
+        // TODO: Remove eventually, may want to make nodes use logging to dump too
+        //
+
+        cl.dump("> ");
+
+        assertNotNull(cl);
+
+        return cl;
+    }
+
+    public void testVisitor1() throws Exception {
+        String input = "a \"b\" 'c' d";
+
+        ASTCommandLine cl = parse(input);
+
+        MockCommandLineVisitor v = new MockCommandLineVisitor();
+
+        Object result = cl.jjtAccept(v, null);
+    }
+
+    private static class MockCommandLineVisitor
+        implements CommandLineParserVisitor
+    {
+        public Object visit(SimpleNode node, Object data) {
+            System.out.println("SimpleNode: " + node + "; data: " + data);
+
+            data = node.childrenAccept(this, data);
+
+            return data;
+        }
+
+        public Object visit(ASTCommandLine node, Object data) {
+            System.out.println("CommandLine: " + node + "; data: " + data);
+
+            data = node.childrenAccept(this, data);
+
+            return data;
+        }
+
+        public Object visit(ASTExpression node, Object data) {
+            System.out.println("Expression: " + node + "; data: " + data);
+
+            data = node.childrenAccept(this, data);
+
+            return data;
+        }
+
+        public Object visit(ASTQuotedString node, Object data) {
+            System.out.println("QuotedString: " + node + "; data: " + data);
+
+            data = node.childrenAccept(this, data);
+
+            return data;
+        }
+
+        public Object visit(ASTOpaqueString node, Object data) {
+            System.out.println("OpaqueString: " + node + "; data: " + data);
+
+            data = node.childrenAccept(this, data);
+
+            return data;
+        }
+
+        public Object visit(ASTPlainString node, Object data) {
+            System.out.println("PlainString: " + node + "; data: " + data);
+
+            data = node.childrenAccept(this, data);
+
+            return data;
+        }
+    }
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserVisitorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserVisitorTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserVisitorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain