You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by pa...@apache.org on 2015/06/07 14:55:15 UTC

incubator-groovy git commit: refactor: replace GroovyInternalPosixParser with DefaultParser

Repository: incubator-groovy
Updated Branches:
  refs/heads/master 81f0523db -> 2740896d3


refactor: replace GroovyInternalPosixParser with DefaultParser


Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/2740896d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/2740896d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/2740896d

Branch: refs/heads/master
Commit: 2740896d3d4acbec63c399a7e6f28142964c5091
Parents: 81f0523
Author: Paul King <pa...@asert.com.au>
Authored: Sun Jun 7 22:54:59 2015 +1000
Committer: Paul King <pa...@asert.com.au>
Committed: Sun Jun 7 22:54:59 2015 +1000

----------------------------------------------------------------------
 src/main/groovy/ui/GroovyMain.java              |  85 ++----
 .../commons/cli/GroovyInternalPosixParser.java  | 274 -------------------
 .../groovy/antlr/java/Java2GroovyMain.java      |   4 +-
 .../groovy/tools/FileSystemCompiler.java        |  22 +-
 .../org/codehaus/groovy/tools/GrapeMain.groovy  |  11 +-
 .../java/org/codehaus/groovy/ant/Groovyc.java   |   4 +-
 6 files changed, 44 insertions(+), 356 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/2740896d/src/main/groovy/ui/GroovyMain.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/ui/GroovyMain.java b/src/main/groovy/ui/GroovyMain.java
index 294d91d..6b73f71 100644
--- a/src/main/groovy/ui/GroovyMain.java
+++ b/src/main/groovy/ui/GroovyMain.java
@@ -27,7 +27,7 @@ import groovy.lang.MissingMethodException;
 import groovy.lang.Script;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.CommandLineParser;
-import org.apache.commons.cli.GroovyInternalPosixParser;
+import org.apache.commons.cli.DefaultParser;
 import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
@@ -164,7 +164,7 @@ public class GroovyMain {
      * @throws ParseException if there was a problem.
      */
     private static CommandLine parseCommandLine(Options options, String[] args) throws ParseException {
-        CommandLineParser parser = new GroovyInternalPosixParser();
+        CommandLineParser parser = new DefaultParser();
         return parser.parse(options, args, true);
     }
 
@@ -177,7 +177,6 @@ public class GroovyMain {
         Options options = new Options();
         options.addOption(Option.builder("classpath").hasArg().argName("path").desc("Specify where to find the class files - must be first argument").build());
         options.addOption(Option.builder("cp").longOpt("classpath").hasArg().argName("path").desc("Aliases for '-classpath'").build());
-
         options.addOption(Option.builder("D").longOpt("define").desc("define a system property").hasArg().argName("name=value").build());
         options.addOption(
             Option.builder().longOpt("disableopt")
@@ -185,72 +184,20 @@ public class GroovyMain {
                   "optlist can be a comma separated list with the elements: " +
                   "all (disables all optimizations), " +
                   "int (disable any int based optimizations)")
-            .hasArg()
-            .argName("optlist")
-            .build());
-
-        options.addOption(
-            Option.builder("h").hasArg(false)
-            .desc("usage information")
-            .longOpt("help")
-            .build());
-        options.addOption(
-            Option.builder("d").hasArg(false)
-            .desc("debug mode will print out full stack traces")
-            .longOpt("debug")
-            .build());
-        options.addOption(
-            Option.builder("v").hasArg(false)
-            .desc("display the Groovy and JVM versions")
-            .longOpt("version")
-            .build());
-        options.addOption(
-            Option.builder("c").argName("charset")
-            .hasArg()
-            .desc("specify the encoding of the files")
-            .longOpt("encoding")
-            .build());
-        options.addOption(
-            Option.builder("e").argName("script")
-            .hasArg()
-            .desc("specify a command line script")
-            .build());
-        options.addOption(
-            Option.builder("i").argName("extension")
-            .optionalArg(true)
-            .desc("modify files in place; create backup if extension is given (e.g. \'.bak\')")
-            .build());
-        options.addOption(
-            Option.builder("n").hasArg(false)
-            .desc("process files line by line using implicit 'line' variable")
-            .build());
-        options.addOption(
-            Option.builder("p").hasArg(false)
-            .desc("process files line by line and print result (see also -n)")
-            .build());
-        options.addOption(
-            Option.builder("l").argName("port")
-            .optionalArg(true)
-            .desc("listen on a port and process inbound lines (default: 1960)")
-            .build());
-        options.addOption(
-            Option.builder("a").argName("splitPattern")
-            .optionalArg(true)
-            .desc("split lines using splitPattern (default '\\s') using implicit 'split' variable")
-            .longOpt("autosplit")
-            .build());
-        options.addOption(
-            Option.builder().longOpt("indy")
-            .desc("enables compilation using invokedynamic")
-            .build());
-        options.addOption(
-            Option.builder().longOpt("configscript")
-            .hasArg().desc("A script for tweaking the configuration options")
-            .build());
-        options.addOption(
-            Option.builder("b").longOpt("basescript")
-            .hasArg().argName("class").desc("Base class name for scripts (must derive from Script)")
-            .build());
+            .hasArg().argName("optlist").build());
+        options.addOption(Option.builder("h").hasArg(false).desc("usage information").longOpt("help").build());
+        options.addOption(Option.builder("d").hasArg(false).desc("debug mode will print out full stack traces").longOpt("debug").build());
+        options.addOption(Option.builder("v").hasArg(false).desc("display the Groovy and JVM versions").longOpt("version").build());
+        options.addOption(Option.builder("c").argName("charset").hasArg().desc("specify the encoding of the files").longOpt("encoding").build());
+        options.addOption(Option.builder("e").argName("script").hasArg().desc("specify a command line script").build());
+        options.addOption(Option.builder("i").argName("extension").optionalArg(true).desc("modify files in place; create backup if extension is given (e.g. \'.bak\')").build());
+        options.addOption(Option.builder("n").hasArg(false).desc("process files line by line using implicit 'line' variable").build());
+        options.addOption(Option.builder("p").hasArg(false).desc("process files line by line and print result (see also -n)").build());
+        options.addOption(Option.builder("l").argName("port").optionalArg(true).desc("listen on a port and process inbound lines (default: 1960)").build());
+        options.addOption(Option.builder("a").argName("splitPattern").optionalArg(true).desc("split lines using splitPattern (default '\\s') using implicit 'split' variable").longOpt("autosplit").build());
+        options.addOption(Option.builder().longOpt("indy").desc("enables compilation using invokedynamic").build());
+        options.addOption(Option.builder().longOpt("configscript").hasArg().desc("A script for tweaking the configuration options").build());
+        options.addOption(Option.builder("b").longOpt("basescript").hasArg().argName("class").desc("Base class name for scripts (must derive from Script)").build());
         return options;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/2740896d/src/main/org/apache/commons/cli/GroovyInternalPosixParser.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/commons/cli/GroovyInternalPosixParser.java b/src/main/org/apache/commons/cli/GroovyInternalPosixParser.java
deleted file mode 100644
index 1cab9ee..0000000
--- a/src/main/org/apache/commons/cli/GroovyInternalPosixParser.java
+++ /dev/null
@@ -1,274 +0,0 @@
-/*
- *  Licensed to the Apache Software Foundation (ASF) under one
- *  or more contributor license agreements.  See the NOTICE file
- *  distributed with this work for additional information
- *  regarding copyright ownership.  The ASF licenses this file
- *  to you 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.commons.cli;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
-
-/**
- * DO NOT USE. Hacked version until Commons CLI 1.3 is released.
- * NOTE: this is a mirror copy of org.codehaus.groovy.cli.GroovyPosixParser
- * DON'T MAKE CHANGES without keeping the other file in sync!
- * The class GroovyPosixParser provides an implementation of the
- * {@link Parser#flatten(Options,String[],boolean) flatten} method.
- *
- * @author John Keyes (john at integralsource.com)
- * @author Paul King (Groovy hacks/fixes)
- */
-public class GroovyInternalPosixParser extends Parser
-{
-    /** holder for flattened tokens */
-    private List tokens = new ArrayList();
-
-    /** specifies if bursting should continue */
-    private boolean eatTheRest;
-
-    /** holder for the current option */
-    private Option currentOption;
-
-    /** the command line Options */
-    private Options options;
-
-    /**
-     * Resets the members to their original state i.e. remove
-     * all of <code>tokens</code> entries and set <code>eatTheRest</code>
-     * to false.
-     */
-    private void init()
-    {
-        eatTheRest = false;
-        tokens.clear();
-    }
-
-    /**
-     * An implementation of {@link Parser}'s abstract
-     * {@link Parser#flatten(Options,String[],boolean) flatten} method.
-     * <p>
-     * The following are the rules used by this flatten method.
-     * <ol>
-     *  <li>if <code>stopAtNonOption</code> is <b>true</b> then do not
-     *  burst anymore of <code>arguments</code> entries, just add each
-     *  successive entry without further processing.  Otherwise, ignore
-     *  <code>stopAtNonOption</code>.</li>
-     *  <li>if the current <code>arguments</code> entry is "<b>--</b>"
-     *  just add the entry to the list of processed tokens</li>
-     *  <li>if the current <code>arguments</code> entry is "<b>-</b>"
-     *  just add the entry to the list of processed tokens</li>
-     *  <li>if the current <code>arguments</code> entry is two characters
-     *  in length and the first character is "<b>-</b>" then check if this
-     *  is a valid {@link Option} id.  If it is a valid id, then add the
-     *  entry to the list of processed tokens and set the current {@link Option}
-     *  member.  If it is not a valid id and <code>stopAtNonOption</code>
-     *  is true, then the remaining entries are copied to the list of
-     *  processed tokens.  Otherwise, the current entry is ignored.</li>
-     *  <li>if the current <code>arguments</code> entry is more than two
-     *  characters in length and the first character is "<b>-</b>" then
-     *  we need to burst the entry to determine its constituents.  For more
-     *  information on the bursting algorithm see
-     *  {@link GroovyInternalPosixParser#burstToken(String, boolean) burstToken}.</li>
-     *  <li>if the current <code>arguments</code> entry is not handled
-     *  by any of the previous rules, then the entry is added to the list
-     *  of processed tokens.</li>
-     * </ol>
-     *
-     * @param options The command line {@link Options}
-     * @param arguments The command line arguments to be parsed
-     * @param stopAtNonOption Specifies whether to stop flattening when an non option is found.
-     * @return The flattened <code>arguments</code> String array.
-     */
-    protected String[] flatten(Options options, String[] arguments, boolean stopAtNonOption) {
-        init();
-        this.options = options;
-
-        // an iterator for the command line tokens
-        Iterator iter = Arrays.asList(arguments).iterator();
-
-        // process each command line token
-        while (iter.hasNext())
-        {
-            // get the next command line token
-            String token = (String) iter.next();
-
-            // handle long option --foo or --foo=bar
-            if (token.startsWith("--"))
-            {
-                int pos = token.indexOf('=');
-                String opt = pos == -1 ? token : token.substring(0, pos); // --foo
-
-                if (!options.hasOption(opt))
-                {
-                    processNonOptionToken(token, stopAtNonOption);
-                }
-                else
-                {
-                    tokens.add(opt);
-                    if (pos != -1)
-                    {
-                        tokens.add(token.substring(pos + 1));
-                    } else {
-                        currentOption = options.getOption(opt);
-                    }
-                }
-            }
-
-            // single hyphen
-            else if ("-".equals(token))
-            {
-                tokens.add(token);
-            }
-            else if (token.startsWith("-"))
-            {
-                if (token.length() == 2 || options.hasOption(token))
-                {
-                    processOptionToken(token, stopAtNonOption);
-                }
-                // requires bursting
-                else
-                {
-                    burstToken(token, stopAtNonOption);
-                }
-            }
-            else
-            {
-                processNonOptionToken(token, stopAtNonOption);
-            }
-
-            gobble(iter);
-        }
-
-        return (String[]) tokens.toArray(new String[tokens.size()]);
-    }
-
-    /**
-     * Adds the remaining tokens to the processed tokens list.
-     *
-     * @param iter An iterator over the remaining tokens
-     */
-    private void gobble(Iterator iter)
-    {
-        if (eatTheRest)
-        {
-            while (iter.hasNext())
-            {
-                tokens.add(iter.next());
-            }
-        }
-    }
-
-    /**
-     * Add the special token "<b>--</b>" and the current <code>value</code>
-     * to the processed tokens list. Then add all the remaining
-     * <code>argument</code> values to the processed tokens list.
-     *
-     * @param value The current token
-     */
-    private void processNonOptionToken(String value, boolean stopAtNonOption)
-    {
-        if (stopAtNonOption && (currentOption == null || !currentOption.hasArg()))
-        {
-            eatTheRest = true;
-            tokens.add("--");
-        }
-
-        tokens.add(value);
-        currentOption = null;
-    }
-
-    /**
-     * If an {@link Option} exists for <code>token</code> then add the token to the processed list.
-     * <p>
-     * If an {@link Option} does not exist and <code>stopAtNonOption</code>
-     * is set then add the remaining tokens to the processed tokens list directly.
-     *
-     * @param token The current option token
-     * @param stopAtNonOption Specifies whether flattening should halt at the first non option.
-     */
-    private void processOptionToken(String token, boolean stopAtNonOption)
-    {
-        if (stopAtNonOption && !options.hasOption(token))
-        {
-            eatTheRest = true;
-        }
-
-        if (options.hasOption(token)) {
-            currentOption = options.getOption(token);
-        } else {
-            currentOption = null;
-        }
-
-        tokens.add(token);
-    }
-
-    /**
-     * Breaks <code>token</code> into its constituent parts
-     * using the following algorithm.
-     *
-     * <ul>
-     *  <li>ignore the first character ("<b>-</b>")</li>
-     *  <li>foreach remaining character check if an {@link Option}
-     *  exists with that id.</li>
-     *  <li>if an {@link Option} does exist then add that character
-     *  prepended with "<b>-</b>" to the list of processed tokens.</li>
-     *  <li>if the {@link Option} can have an argument value and there
-     *  are remaining characters in the token then add the remaining
-     *  characters as a token to the list of processed tokens.</li>
-     *  <li>if an {@link Option} does <b>NOT</b> exist <b>AND</b>
-     *  <code>stopAtNonOption</code> <b>IS</b> set then add the special token
-     *  "<b>--</b>" followed by the remaining characters and also
-     *  the remaining tokens directly to the processed tokens list.</li>
-     *  <li>if an {@link Option} does <b>NOT</b> exist <b>AND</b>
-     *  <code>stopAtNonOption</code> <b>IS NOT</b> set then add that
-     *  character prepended with "<b>-</b>".</li>
-     * </ul>
-     *
-     * @param token The current token to be <b>burst</b>
-     * @param stopAtNonOption Specifies whether to stop processing
-     * at the first non-Option encountered.
-     */
-    protected void burstToken(String token, boolean stopAtNonOption) {
-        for (int i = 1; i < token.length(); i++)
-        {
-            String ch = String.valueOf(token.charAt(i));
-
-            if (options.hasOption(ch))
-            {
-                tokens.add("-" + ch);
-                currentOption = options.getOption(ch);
-
-                if (currentOption.hasArg() && (token.length() != (i + 1)))
-                {
-                    tokens.add(token.substring(i + 1));
-                    break;
-                }
-            }
-            else if (stopAtNonOption)
-            {
-                processNonOptionToken(token.substring(i), true);
-                break;
-            }
-            else
-            {
-                tokens.add(token);
-                break;
-            }
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/2740896d/src/main/org/codehaus/groovy/antlr/java/Java2GroovyMain.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/antlr/java/Java2GroovyMain.java b/src/main/org/codehaus/groovy/antlr/java/Java2GroovyMain.java
index 43ac2d6..59d52f8 100644
--- a/src/main/org/codehaus/groovy/antlr/java/Java2GroovyMain.java
+++ b/src/main/org/codehaus/groovy/antlr/java/Java2GroovyMain.java
@@ -20,7 +20,7 @@ package org.codehaus.groovy.antlr.java;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.CommandLineParser;
-import org.apache.commons.cli.GroovyInternalPosixParser;
+import org.apache.commons.cli.DefaultParser;
 import org.apache.commons.cli.Options;
 import java.util.Arrays;
 
@@ -29,7 +29,7 @@ public class Java2GroovyMain {
     public static void main(String[] args) {
         try {
             Options options = new Options();
-            CommandLineParser cliParser = new GroovyInternalPosixParser();
+            CommandLineParser cliParser = new DefaultParser();
             CommandLine cli = cliParser.parse(options, args);
             String[] filenames = cli.getArgs();
             if (filenames.length == 0) {

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/2740896d/src/main/org/codehaus/groovy/tools/FileSystemCompiler.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/tools/FileSystemCompiler.java b/src/main/org/codehaus/groovy/tools/FileSystemCompiler.java
index 0aa2a88..47761cc 100644
--- a/src/main/org/codehaus/groovy/tools/FileSystemCompiler.java
+++ b/src/main/org/codehaus/groovy/tools/FileSystemCompiler.java
@@ -21,7 +21,13 @@ package org.codehaus.groovy.tools;
 import groovy.lang.Binding;
 import groovy.lang.GroovyResourceLoader;
 import groovy.lang.GroovyShell;
-import org.apache.commons.cli.*;
+import groovy.lang.GroovySystem;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
 import org.codehaus.groovy.control.CompilationUnit;
 import org.codehaus.groovy.control.CompilerConfiguration;
 import org.codehaus.groovy.control.ConfigurationException;
@@ -29,12 +35,16 @@ import org.codehaus.groovy.control.customizers.ImportCustomizer;
 import org.codehaus.groovy.runtime.DefaultGroovyStaticMethods;
 import org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit;
 
-import groovy.lang.GroovySystem;
-
-import java.io.*;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * Command-line compiler (aka. <tt>groovyc</tt>).
@@ -116,7 +126,7 @@ public class FileSystemCompiler {
     public static void commandLineCompile(String[] args, boolean lookupUnnamedFiles) throws Exception {
         Options options = createCompilationOptions();
 
-        CommandLineParser cliParser = new GroovyInternalPosixParser();
+        CommandLineParser cliParser = new DefaultParser();
 
         CommandLine cli;
         cli = cliParser.parse(options, args);

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/2740896d/src/main/org/codehaus/groovy/tools/GrapeMain.groovy
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/tools/GrapeMain.groovy b/src/main/org/codehaus/groovy/tools/GrapeMain.groovy
index 0620c6c..19e0e43 100644
--- a/src/main/org/codehaus/groovy/tools/GrapeMain.groovy
+++ b/src/main/org/codehaus/groovy/tools/GrapeMain.groovy
@@ -20,9 +20,14 @@ package org.codehaus.groovy.tools
 
 import groovy.grape.Grape
 import groovy.transform.Field
+import org.apache.commons.cli.CommandLine
+import org.apache.commons.cli.DefaultParser
+import org.apache.commons.cli.HelpFormatter
+import org.apache.commons.cli.Option
+import org.apache.commons.cli.OptionGroup
+import org.apache.commons.cli.Options
 import org.apache.ivy.util.DefaultMessageLogger
 import org.apache.ivy.util.Message
-import org.apache.commons.cli.*
 
 //commands
 
@@ -120,7 +125,7 @@ import org.apache.commons.cli.*
     options.addOption(Option.builder("d").hasArg(false).longOpt("dos").build());
     options.addOption(Option.builder("s").hasArg(false).longOpt("shell").build());
     options.addOption(Option.builder("i").hasArg(false).longOpt("ivy").build());
-    CommandLine cmd2 = new GroovyInternalPosixParser().parse(options, arg[1..-1] as String[], true);
+    CommandLine cmd2 = new DefaultParser().parse(options, arg[1..-1] as String[], true);
     arg = cmd2.args
 
     // set the instance so we can re-set the logger
@@ -274,7 +279,7 @@ options.addOption(Option.builder("v").hasArg(false).desc("display the Groovy and
 
 @Field CommandLine cmd
 
-cmd = new GroovyInternalPosixParser().parse(options, args, true);
+cmd = new DefaultParser().parse(options, args, true);
 
 if (cmd.hasOption('h')) {
     grapeHelp()

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/2740896d/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovyc.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovyc.java b/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovyc.java
index 594dd40..e956c90 100644
--- a/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovyc.java
+++ b/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovyc.java
@@ -39,7 +39,7 @@ import java.util.StringTokenizer;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.CommandLineParser;
-import org.apache.commons.cli.GroovyInternalPosixParser;
+import org.apache.commons.cli.DefaultParser;
 import org.apache.commons.cli.Options;
 import org.apache.tools.ant.AntClassLoader;
 import org.apache.tools.ant.BuildException;
@@ -1139,7 +1139,7 @@ public class Groovyc extends MatchingTask {
         try {
             Options options = FileSystemCompiler.createCompilationOptions();
 
-            CommandLineParser cliParser = new GroovyInternalPosixParser();
+            CommandLineParser cliParser = new DefaultParser();
 
             CommandLine cli;
             cli = cliParser.parse(options, commandLine);