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/25 07:41:09 UTC

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

Author: jdillon
Date: Wed May 24 22:41:08 2006
New Revision: 409304

URL: http://svn.apache.org/viewvc?rev=409304&view=rev
Log:
The very beggining of command line syntax parsing ala JavaCC

Added:
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/grammar/
    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/
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTArgumentSupport.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTPlainArgument.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTQuotedArgument.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserSupport.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserTest.java
Modified:
    geronimo/sandbox/gshell/trunk/gshell-core/pom.xml

Modified: geronimo/sandbox/gshell/trunk/gshell-core/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/pom.xml?rev=409304&r1=409303&r2=409304&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/pom.xml (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/pom.xml Wed May 24 22:41:08 2006
@@ -61,6 +61,82 @@
     </dependencies>
 
     <build>
+        <plugins>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>javacc-maven-plugin</artifactId>
+                <version>2.0</version>
+                <executions>
+                    <execution>
+                        <id>jjtree</id>
+                        <phase>generate-sources</phase>
+                        <goals>
+                            <goal>jjtree</goal>
+                        </goals>
+                        <configuration>
+                            <sourceDirectory>${pom.basedir}/src/main/grammar</sourceDirectory>
+                            <outputDirectory>${pom.basedir}/target/generated-sources/jjtree</outputDirectory>
+                        </configuration>
+                    </execution>
+
+                    <execution>
+                        <id>javacc</id>
+                        <phase>generate-sources</phase>
+                        <goals>
+                            <goal>javacc</goal>
+                        </goals>
+                        <configuration>
+                            <sourceDirectory>${pom.basedir}/target/generated-sources/jjtree</sourceDirectory>
+                            <outputDirectory>${pom.basedir}/target/generated-sources/javacc</outputDirectory>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+
+            <!-- Delete generated/overwritten classes to prevent poluting the src tree -->
+            <plugin>
+                <artifactId>maven-antrun-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <phase>generate-sources</phase>
+                        <configuration>
+                            <tasks>
+                                <delete>
+                                    <fileset dir="${pom.basedir}/target/generated-sources/jjtree">
+                                        <include name="ASTPlainArgument.java"/>
+                                        <include name="ASTQuotedArgument.java"/>
+                                    </fileset>
+                                </delete>
+                            </tasks>
+                        </configuration>
+                        <goals>
+                            <goal>run</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>build-helper-maven-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>add-source</id>
+                        <phase>generate-sources</phase>
+                        <goals>
+                            <goal>add-source</goal>
+                        </goals>
+                        <configuration>
+                            <sources>
+                                <source>${pom.basedir}/target/generated-sources/jjtree</source>
+                                <source>${pom.basedir}/target/generated-sources/javacc</source>
+                            </sources>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+
         <resources>
             <resource>
                 <directory>${pom.basedir}/src/main/resources</directory>

Added: 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=409304&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/grammar/CommandLineParser.jjt (added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/grammar/CommandLineParser.jjt Wed May 24 22:41:08 2006
@@ -0,0 +1,141 @@
+/*
+ * 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.
+ */
+
+options {
+    STATIC = false;
+    UNICODE_INPUT = true;
+    OPTIMIZE_TOKEN_MANAGER = true;
+    ERROR_REPORTING = true;
+    MULTI = true;
+    VISITOR = true;
+    NODE_DEFAULT_VOID = true;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+PARSER_BEGIN(CommandLineParser)
+
+package org.apache.geronimo.gshell.commandline.parser;
+
+/**
+ * Command line parser.
+ *
+ * @version $Id$
+ */
+public class CommandLineParser
+    extends CommandLineParserSupport
+{
+    //
+    // TODO: ???
+    //
+}
+
+PARSER_END(CommandLineParser)
+
+///////////////////////////////////////////////////////////////////////////////
+
+//
+// WHITE SPACE
+//
+
+SKIP :
+{
+  " "
+| "\t"
+| "\n"
+| "\r"
+| "\f"
+}
+
+//
+// COMMENTS
+//
+
+SPECIAL_TOKEN :
+{
+  <SINGLE_LINE_COMMENT: "#" (~["\n", "\r"])* ("\n" | "\r" | "\r\n")?>
+}
+
+//
+// LITERALS
+//
+
+TOKEN :
+{
+  < PLAIN_ARGUMENT:
+      (   (~["\"", "\\", " ", "\t", "\n", "\r", "\f"])
+        | ("\\"
+            ( ["n","t","b","r","f","\\","'","\""]
+            | ["0"-"7"] ( ["0"-"7"] )?
+            | ["0"-"3"] ["0"-"7"] ["0"-"7"]
+            )
+          )
+      )+
+  >
+|
+  < QUOTED_ARGUMENT:
+      "\""
+      (   (~["\"","\\","\n","\r"])
+        | ("\\"
+            ( ["n","t","b","r","f","\\","'","\""]
+            | ["0"-"7"] ( ["0"-"7"] )?
+            | ["0"-"3"] ["0"-"7"] ["0"-"7"]
+            )
+          )
+      )*
+      "\""
+  >
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+ASTCommandLine CommandLine() #CommandLine: {}
+{
+    Expression()
+    { return jjtThis; }
+}
+
+void Expression() #Expression: {}
+{
+    ( Argument() )*
+}
+
+void Argument() : {}
+{
+    QuotedArgument() | PlainArgument()
+}
+
+void QuotedArgument() #QuotedArgument:
+{
+    Token t;
+}
+{
+    t=<QUOTED_ARGUMENT>
+    {
+        jjtThis.setToken(t);
+    }
+}
+
+void PlainArgument() #PlainArgument:
+{
+    Token t;
+}
+{
+    t=<PLAIN_ARGUMENT>
+    {
+        jjtThis.setToken(t);
+    }
+}

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTArgumentSupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTArgumentSupport.java?rev=409304&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTArgumentSupport.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTArgumentSupport.java Wed May 24 22:41:08 2006
@@ -0,0 +1,54 @@
+/*
+ * 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;
+
+/**
+ * Support for argument types.
+ *
+ * @version $Id$
+ */
+public abstract class ASTArgumentSupport
+    extends SimpleNode
+{
+    protected Token token;
+
+    public ASTArgumentSupport(int id) {
+        super(id);
+    }
+
+    public ASTArgumentSupport(CommandLineParser p, int id) {
+        super(p, id);
+    }
+
+    public void setToken(final Token token) {
+        assert token != null;
+
+        this.token = token;
+    }
+
+    public Token getToken() {
+        return token;
+    }
+
+    public Object jjtAccept(CommandLineParserVisitor visitor, Object data) {
+        return visitor.visit(this, data);
+    }
+
+    public String toString() {
+        return super.toString() + "{ " + getToken() + " }";
+    }
+}
\ No newline at end of file

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTPlainArgument.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTPlainArgument.java?rev=409304&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTPlainArgument.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTPlainArgument.java Wed May 24 22:41:08 2006
@@ -0,0 +1,34 @@
+/*
+ * 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;
+
+/**
+ * Represents a <em>plain</em> unquoted argument.
+ *
+ * @version $Id$
+ */
+public class ASTPlainArgument
+    extends ASTArgumentSupport
+{
+    public ASTPlainArgument(int id) {
+        super(id);
+    }
+
+    public ASTPlainArgument(CommandLineParser p, int id) {
+        super(p, id);
+    }
+}
\ No newline at end of file

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTQuotedArgument.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTQuotedArgument.java?rev=409304&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTQuotedArgument.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/ASTQuotedArgument.java Wed May 24 22:41:08 2006
@@ -0,0 +1,34 @@
+/*
+ * 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;
+
+/**
+ * Represents a quoted (with double quotes) argument.
+ *
+ * @version $Id$
+ */
+public class ASTQuotedArgument
+    extends ASTArgumentSupport
+{
+    public ASTQuotedArgument(int id) {
+        super(id);
+    }
+
+    public ASTQuotedArgument(CommandLineParser p, int id) {
+        super(p, id);
+    }
+}

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserSupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserSupport.java?rev=409304&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserSupport.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserSupport.java Wed May 24 22:41:08 2006
@@ -0,0 +1,29 @@
+/*
+ * 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;
+
+/**
+ * Support for {@link CommandLineParser}.
+ *
+ * @version $Id: Variables.java 404908 2006-05-08 03:42:51Z jdillon $
+ */
+public abstract class CommandLineParserSupport
+{
+    //
+    // TODO: ???
+    //
+}

Added: 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=409304&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserTest.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserTest.java Wed May 24 22:41:08 2006
@@ -0,0 +1,101 @@
+/*
+ * 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 CommandLineParser} class.
+ *
+ * @version $Id$
+ */
+public class CommandLineParserTest
+    extends TestCase
+{
+    private ASTCommandLine parse(final String input) throws ParseException {
+        assert input != null;
+
+        Reader reader = new StringReader(input);
+        CommandLineParser parser = new CommandLineParser(reader);
+        ASTCommandLine cl = parser.CommandLine();
+
+        cl.dump("");
+
+        assertNotNull(cl);
+
+        return cl;
+    }
+
+    //
+    // Comments
+    //
+
+    public void testSingleComment1() throws Exception {
+        String input = "# this should be completly ignored";
+
+        ASTCommandLine cl = parse(input);
+    }
+
+    public void testSingleComment2() throws Exception {
+        String input = "####";
+
+        ASTCommandLine cl = parse(input);
+    }
+
+    public void testSingleComment3() throws Exception {
+        String input = "# ignored; this too";
+
+        ASTCommandLine cl = parse(input);
+    }
+
+    //
+    // Arguments
+    //
+
+    public void testArguments1() throws Exception {
+        String input = "a b c";
+
+        ASTCommandLine cl = parse(input);
+
+        //
+        // TODO: Verify 3 plain arguments
+        //
+    }
+
+    public void testArguments2() throws Exception {
+        String input = "a -b --c d";
+
+        ASTCommandLine cl = parse(input);
+
+        //
+        // TODO: Verify 4 plain arguments
+        //
+    }
+
+    public void testQuotedArguments1() throws Exception {
+        String input = "a \"b -c\" d";
+
+        ASTCommandLine cl = parse(input);
+
+        //
+        // TODO: Verify 2 plain arguments + 1 quoted
+        //
+    }
+}