1/*
2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package propertiesparser.parser;
25
26/**
27 * Common interface to all kinds of diagnostic argument types.
28 */
29public interface MessageType {
30
31    /**
32     * Visitor method.
33     */
34    <R, A> R accept(Visitor<R, A> v, A a);
35
36    /**
37     * The type as mentioned in the resource file.
38     */
39    String kindName();
40
41    /**
42     * A custom type is a type for which a predefined alternative does not exist. As such, it is an
43     * handy option when prototyping - but usages of custom types should be avoided in product-quality
44     * resource file comments.
45     *
46     * Example: 'com.sun.tools.javac.code.Flags.Flag'
47     */
48    public static class CustomType implements MessageType {
49
50        /** The string-based representation of this type. */
51        public String typeString;
52
53        public CustomType(String typeString) {
54            this.typeString = typeString;
55        }
56
57        @Override
58        public String kindName() {
59            return typeString;
60        }
61
62        @Override
63        public <R, A> R accept(Visitor<R, A> v, A a) {
64            return v.visitCustomType(this, a);
65        }
66    }
67
68    /**
69     * A predefined type. All common types mentioned in the resource file comments are meant to
70     * be included here.
71     */
72    public enum SimpleType implements MessageType {
73
74        BOOLEAN("boolean", "boolean", null),
75        FRAGMENT("fragment", "Fragment", null),
76        DIAGNOSTIC("diagnostic", "JCDiagnostic", "com.sun.tools.javac.util"),
77        MODIFIER("modifier", "Modifier", "javax.lang.model.element"),
78        FILE("file", "File", "java.io"),
79        FILE_OBJECT("file object", "JavaFileObject", "javax.tools"),
80        PATH("path", "Path", "java.nio.file"),
81        NAME("name", "Name", "com.sun.tools.javac.util"),
82        NUMBER("number", "int", null),
83        OPTION_NAME("option name", "Option", "com.sun.tools.javac.main"),
84        SOURCE_VERSION("source version", "Source", "com.sun.tools.javac.code"),
85        STRING("string", "String", null),
86        SYMBOL("symbol", "Symbol", "com.sun.tools.javac.code"),
87        SYMBOL_KIND("symbol kind", "Kind", "com.sun.tools.javac.code.Kinds"),
88        KIND_NAME("kind name", "KindName", "com.sun.tools.javac.code.Kinds"),
89        TOKEN("token", "TokenKind", "com.sun.tools.javac.parser.Tokens"),
90        TYPE("type", "Type", "com.sun.tools.javac.code"),
91        SET("set", "Set", "java.util"),
92        LIST("list", "List", "java.util"),
93        OBJECT("object", "Object", null),
94        UNUSED("unused", "Void", null),
95        UNKNOWN("<unknown>", "UnknownType", null);
96
97        /** name of the predefined type as mentioned in the resource file. */
98        public final String kindName;
99
100        /** string-based representation of the type */
101        public final String clazz;
102
103        /** type qualifier (might be null) */
104        public final String qualifier;
105
106        SimpleType(String kindName, String clazz, String qualifier) {
107            this.kindName = kindName;
108            this.clazz = clazz;
109            this.qualifier = qualifier;
110        }
111
112        @Override
113        public String kindName() {
114            return kindName;
115        }
116
117        @Override
118        public <R, A> R accept(Visitor<R, A> v, A a) {
119            return v.visitSimpleType(this, a);
120        }
121    }
122
123    /**
124     * A compound type is a collection of some element type.
125     *
126     * Example: list of string
127     */
128    public static class CompoundType implements MessageType {
129
130        /**
131         * Compound type kind.
132         */
133        public enum Kind {
134            LIST("list of", SimpleType.LIST),
135            SET("set of", SimpleType.SET);
136
137            public final String kindName;
138            public final SimpleType clazz;
139
140            Kind(String kindName, SimpleType clazz) {
141                this.kindName = kindName;
142                this.clazz = clazz;
143            }
144        }
145
146        /** The compound type kind. */
147        public final Kind kind;
148
149        /** The element type. */
150        public final MessageType elemtype;
151
152        public CompoundType(Kind kind, MessageType elemtype) {
153            this.kind = kind;
154            this.elemtype = elemtype;
155        }
156
157        @Override
158        public String kindName() {
159            return kind.kindName;
160        }
161
162        @Override
163        public <R, A> R accept(Visitor<R, A> v, A a) {
164            return v.visitCompoundType(this, a);
165        }
166    }
167
168    /**
169     * A union type represents an alternative between two (or more) types. It can be useful to
170     * define the type of an argument which can assume multiple (unrelated) values; union types
171     * are only meant to be used in cases where the alternative comes up frequently enough in the
172     * resource file comments - in order to avoid cluttered comments.
173     *
174     * Example: message segment
175     */
176    public static class UnionType implements MessageType {
177
178        /**
179         * Union type kind.
180         */
181        public enum Kind {
182            MESSAGE_SEGMENT("message segment", SimpleType.DIAGNOSTIC, SimpleType.FRAGMENT),
183            FILE_NAME("file name", SimpleType.FILE, SimpleType.FILE_OBJECT);
184
185            final String kindName;
186            final SimpleType[] choices;
187
188            Kind(String kindName, SimpleType... choices) {
189                this.kindName = kindName;
190                this.choices = choices;
191            }
192        }
193
194        /** The union type kind. */
195        public final Kind kind;
196
197        /** The union type alternatives. */
198        public final MessageType[] choices;
199
200        UnionType(Kind kind) {
201            this(kind, kind.choices);
202        }
203
204        protected UnionType(Kind kind, MessageType[] choices) {
205            this.choices = choices;
206            this.kind = kind;
207        }
208
209        @Override
210        public String kindName() {
211            return kind.kindName;
212        }
213
214        @Override
215        public <R, A> R accept(Visitor<R, A> v, A a) {
216            return v.visitUnionType(this, a);
217        }
218    }
219
220    /**
221     * A subclass of union type representing 'explicit' alternatives in the resource file comments.
222     * Note: as the token 'or' is parsed with lowest priority, it is not possible, for instance,
223     * to form a compound type out of an 'or' type. In such cases a plain union type should be used
224     * instead.
225     *
226     * Examples: symbol or type
227     */
228    public static class OrType extends UnionType {
229
230        public static final String OR_NAME = "or";
231
232        @Override
233        public String kindName() {
234            return OR_NAME;
235        }
236
237        public OrType(MessageType... choices) {
238            super(null, choices);
239        }
240    }
241
242    /**
243     * Visitor class.
244     */
245    public static abstract class Visitor<R, A> {
246        public abstract R visitCustomType(CustomType t, A a);
247        public abstract R visitSimpleType(SimpleType t, A a);
248        public abstract R visitCompoundType(CompoundType t, A a);
249        public abstract R visitUnionType(UnionType t, A a);
250    }
251}
252