langtools/make/tools/propertiesparser/parser/MessageType.java
changeset 28334 1633de6070ae
child 36526 3b41f1c69604
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/make/tools/propertiesparser/parser/MessageType.java	Thu Jan 08 14:43:05 2015 +0000
@@ -0,0 +1,250 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package propertiesparser.parser;
+
+/**
+ * Common interface to all kinds of diagnostic argument types.
+ */
+public interface MessageType {
+
+    /**
+     * Visitor method.
+     */
+    <R, A> R accept(Visitor<R, A> v, A a);
+
+    /**
+     * The type as mentioned in the resource file.
+     */
+    String kindName();
+
+    /**
+     * A custom type is a type for which a predefined alternative does not exist. As such, it is an
+     * handy option when prototyping - but usages of custom types should be avoided in product-quality
+     * resource file comments.
+     *
+     * Example: 'com.sun.tools.javac.code.Flags.Flag'
+     */
+    public static class CustomType implements MessageType {
+
+        /** The string-based representation of this type. */
+        public String typeString;
+
+        public CustomType(String typeString) {
+            this.typeString = typeString;
+        }
+
+        @Override
+        public String kindName() {
+            return typeString;
+        }
+
+        @Override
+        public <R, A> R accept(Visitor<R, A> v, A a) {
+            return v.visitCustomType(this, a);
+        }
+    }
+
+    /**
+     * A predefined type. All common types mentioned in the resource file comments are meant to
+     * be included here.
+     */
+    public enum SimpleType implements MessageType {
+
+        BOOLEAN("boolean", "boolean", null),
+        FRAGMENT("fragment", "Fragment", null),
+        DIAGNOSTIC("diagnostic", "JCDiagnostic", "com.sun.tools.javac.util"),
+        MODIFIER("modifier", "Modifier", "javax.lang.model.element"),
+        FILE("file", "File", "java.io"),
+        FILE_OBJECT("file object", "JavaFileObject", "javax.tools"),
+        NAME("name", "Name", "com.sun.tools.javac.util"),
+        NUMBER("number", "int", null),
+        OPTION_NAME("option name", "Option", "com.sun.tools.javac.main"),
+        SOURCE_VERSION("source version", "Source", "com.sun.tools.javac.code"),
+        STRING("string", "String", null),
+        SYMBOL("symbol", "Symbol", "com.sun.tools.javac.code"),
+        SYMBOL_KIND("symbol kind", "Kind", "com.sun.tools.javac.code.Kinds"),
+        KIND_NAME("kind name", "KindName", "com.sun.tools.javac.code.Kinds"),
+        TOKEN("token", "TokenKind", "com.sun.tools.javac.parser.Tokens"),
+        TYPE("type", "Type", "com.sun.tools.javac.code"),
+        SET("set", "Set", "java.util"),
+        LIST("list", "List", "java.util"),
+        OBJECT("object", "Object", null),
+        UNUSED("unused", "Void", null),
+        UNKNOWN("<unknown>", "UnknownType", null);
+
+        /** name of the predefined type as mentioned in the resource file. */
+        public final String kindName;
+
+        /** string-based representation of the type */
+        public final String clazz;
+
+        /** type qualifier (might be null) */
+        public final String qualifier;
+
+        SimpleType(String kindName, String clazz, String qualifier) {
+            this.kindName = kindName;
+            this.clazz = clazz;
+            this.qualifier = qualifier;
+        }
+
+        @Override
+        public String kindName() {
+            return kindName;
+        }
+
+        @Override
+        public <R, A> R accept(Visitor<R, A> v, A a) {
+            return v.visitSimpleType(this, a);
+        }
+    }
+
+    /**
+     * A compound type is a collection of some element type.
+     *
+     * Example: list of string
+     */
+    public static class CompoundType implements MessageType {
+
+        /**
+         * Compound type kind.
+         */
+        public enum Kind {
+            LIST("list of", SimpleType.LIST),
+            SET("set of", SimpleType.SET);
+
+            public final String kindName;
+            public final SimpleType clazz;
+
+            Kind(String kindName, SimpleType clazz) {
+                this.kindName = kindName;
+                this.clazz = clazz;
+            }
+        }
+
+        /** The compound type kind. */
+        public final Kind kind;
+
+        /** The element type. */
+        public final MessageType elemtype;
+
+        public CompoundType(Kind kind, MessageType elemtype) {
+            this.kind = kind;
+            this.elemtype = elemtype;
+        }
+
+        @Override
+        public String kindName() {
+            return kind.kindName;
+        }
+
+        @Override
+        public <R, A> R accept(Visitor<R, A> v, A a) {
+            return v.visitCompoundType(this, a);
+        }
+    }
+
+    /**
+     * A union type represents an alternative between two (or more) types. It can be useful to
+     * define the type of an argument which can assume multiple (unrelated) values; union types
+     * are only meant to be used in cases where the alternative comes up frequently enough in the
+     * resource file comments - in order to avoid cluttered comments.
+     *
+     * Example: message segment
+     */
+    public static class UnionType implements MessageType {
+
+        /**
+         * Union type kind.
+         */
+        public enum Kind {
+            MESSAGE_SEGMENT("message segment", SimpleType.DIAGNOSTIC, SimpleType.FRAGMENT),
+            FILE_NAME("file name", SimpleType.FILE, SimpleType.FILE_OBJECT);
+
+            final String kindName;
+            final SimpleType[] choices;
+
+            Kind(String kindName, SimpleType... choices) {
+                this.kindName = kindName;
+                this.choices = choices;
+            }
+        }
+
+        /** The union type kind. */
+        public final Kind kind;
+
+        /** The union type alternatives. */
+        public final MessageType[] choices;
+
+        UnionType(Kind kind) {
+            this(kind, kind.choices);
+        }
+
+        protected UnionType(Kind kind, MessageType[] choices) {
+            this.choices = choices;
+            this.kind = kind;
+        }
+
+        @Override
+        public String kindName() {
+            return kind.kindName;
+        }
+
+        @Override
+        public <R, A> R accept(Visitor<R, A> v, A a) {
+            return v.visitUnionType(this, a);
+        }
+    }
+
+    /**
+     * A subclass of union type representing 'explicit' alternatives in the resource file comments.
+     * Note: as the token 'or' is parsed with lowest priority, it is not possible, for instance,
+     * to form a compound type out of an 'or' type. In such cases a plain union type should be used
+     * instead.
+     *
+     * Examples: symbol or type
+     */
+    public static class OrType extends UnionType {
+
+        public static final String OR_NAME = "or";
+
+        @Override
+        public String kindName() {
+            return OR_NAME;
+        }
+
+        public OrType(MessageType... choices) {
+            super(null, choices);
+        }
+    }
+
+    /**
+     * Visitor class.
+     */
+    public static abstract class Visitor<R, A> {
+        public abstract R visitCustomType(CustomType t, A a);
+        public abstract R visitSimpleType(SimpleType t, A a);
+        public abstract R visitCompoundType(CompoundType t, A a);
+        public abstract R visitUnionType(UnionType t, A a);
+    }
+}